CMA端接收图片的思想和proxy中相似,接收字节数组,然后截取转换成图片及所需数据,大致代码如下:
1 package com.freelynet.meta.webapp.actions.kjava;
2
3 import java.io.BufferedInputStream;
4 import java.io.File;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.UnsupportedEncodingException;
9 import java.text.ParseException;
10 import java.text.SimpleDateFormat;
11 import java.text.Format;
12 import java.text.DecimalFormat;
13 import java.util.ArrayList;
14 import java.util.Date;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20 import javax.servlet.http.HttpServletRequest;
21
22 import org.apache.struts2.ServletActionContext;
23 import org.apache.struts2.config.ParentPackage;
24 import org.apache.struts2.config.Result;
25 import org.apache.struts2.config.Results;
26 import org.apache.struts2.dispatcher.ServletDispatcherResult;
27
28 import com.freelynet.bboss.soap.bean.SoapDataContainer;
29 import com.freelynet.bboss.soap.bean.SoapUserInfo;
30 import com.freelynet.bboss.soap.service.IGroupCustomerService;
31 import com.freelynet.meta.model.Department;
32 import com.freelynet.meta.model.SysUser;
33 import com.freelynet.meta.service.ApplicationException;
34 import com.freelynet.meta.service.BizService;
35 import com.freelynet.meta.service.BizServiceBean;
36 import com.freelynet.meta.service.BossService;
37 import com.freelynet.meta.service.UserManager;
38 import com.freelynet.meta.util.BossQuery;
39 import com.freelynet.meta.webapp.action.BaseAction;
40 import com.freelynet.meta.webapp.actions.admin.BaseAdminAction;
41 import com.opensymphony.xwork2.ActionContext;
42 import com.freelynet.sxcma.interfaces.helper.CmaHelper;
43
44 /**
45 * @author moderndai
46 * @company fancy 2008-12-15
47 */
48 @ParentPackage("meta-package")
49 @Results( {
50 @Result(name = "home", type = ServletDispatcherResult.class, value = "/kjava/home.jsp"),
51 @Result(name = "getStaffOrderList-home", type = ServletDispatcherResult.class, value = "/kjava/stafforder.home.jsp"),
52 @Result(name = "getQueryOrderApply-input", type = ServletDispatcherResult.class, value = "/kjava/stafforder.query.input.jsp"),
53 @Result(name = "getQueryOrderApply-success", type = ServletDispatcherResult.class, value = "/kjava/stafforder.query.success.jsp"),
54 @Result(name = "getAddOrderApply-input", type = ServletDispatcherResult.class, value = "/kjava/stafforder.add.input.jsp"),
55 @Result(name = "getAddOrderApply-success", type = ServletDispatcherResult.class, value = "/kjava/stafforder.add.success.jsp")
56 })
57 public class FileAction extends BaseKjavaAction {
58
59 public String home(){
60 return "home";
61 }
62
63 public String getStaffOrderList(){
64 return "getStaffOrderList-home";
65 }
66
67 /**
68 * 把收到的数据转换成字节数组
69 * @param request
70 * @return
71 */
72 private byte[] getRequestData(HttpServletRequest request) {
73 try {
74 BufferedInputStream in = new BufferedInputStream(request
75 .getInputStream());
76 byte[] buffer = new byte[1024 * 1024 + 3]; // 请求内容最大200K
77 int pos = 0;
78 int total = 0;
79 // 获取请求头长度
80 while (true) {
81 int n = in.read();
82 if (n == -1) {
83 break;
84 }
85 buffer[pos] = (byte) n;
86 ++pos;
87 ++total;
88 }
89 if (total > 0) {
90 byte[] data = new byte[total];
91 for (int i = 0; i < data.length; ++i) {
92 data[i] = buffer[i];
93 }
94 return data;
95 } else {
96 return null;
97 }
98 } catch (Exception e) {
99 //log.error("error occurs when fetching the request data!", e);
100 System.out.println("error occurs when fetching the request data!" + e);
101 return null;
102 }
103 }
104 /**
105 * 把收到的字节数组中的参数和图片分开
106 */
107 public String execute() throws Exception {
108
109 byte[] reqData = getRequestData(ServletActionContext.getRequest());
110
111 String urlParameter = "";//url里的参数
112 byte[] picData = null; //图片字节数组
113
114 byte[] urlParameterData = null; //字节数组形式的url参数
115 byte[] mark = new byte[2]; //标志位字节数组
116
117 //把标志位 “&#” 的字节数组位置找到,还原成字符串,以此分开url数组和图片数组
118 for (int i=0; i< reqData.length; i++){
119 System.arraycopy(reqData, i, mark, 0, 1);
120 String word = new String(mark, "UTF-8");
121
122 //问号出现的位置
123 if (word.trim().equals("&")){
124 //再判断后面是不是#号
125 System.arraycopy(reqData, i+1, mark, 0, 1);
126 String wordNext = new String(mark, "UTF-8");
127 if (wordNext.trim().equals("#")){
128 //分开url数组和图片数组
129 urlParameterData = new byte[i];
130 picData = new byte[reqData.length - (i+2)];
131
132 System.arraycopy(reqData, 0, urlParameterData, 0, i);//url字节数组
133 System.arraycopy(reqData, (i+2), picData, 0, reqData.length - (i+2) );//图片字节数组
134
135 urlParameter = new String(urlParameterData, "UTF-8");
136 urlParameter = "&"+ urlParameter + "&";
137 //System.out.println(urlParameter);
138 break;
139 }
140 }
141 }
142
143 if (picData != null){
144 File file = new File("E:\\cmaPic.png");
145 FileOutputStream fos = new FileOutputStream(file);
146 fos.write(picData);
147 fos.flush();
148 fos.close();
149 }
150
151 StaffOrderAction stafforder=new StaffOrderAction();
152 stafforder.getAddOrderApply(urlParameter,picData);
153 return "";
154 }
155 }
156