採用HTTP協議上傳檔案實現(java)
阿新 • • 發佈:2019-01-15
{
37
38 publicfinalstatic String DEFAULT_ENCODING ="ISO8859_1";
39
40 publicfinalstatic String CHINESE_ENCODING ="GBK";
41
42 publicfinalstatic String SIGN_BOUNDARY ="boundary=";
43
44 publicfinalstatic String SIGN_FORMELEMENT ="name=";
45
46 publicfinalstatic String SIGN_FORMFILE ="filename=";
47
48 publicfinalstatic String SIGN_NOTFILE ="application/octet-stream";
49
50 publicfinalstatic String SIGN_MULTIDATA ="multipart/form-data";
51
52 publicfinalstatic String CHINESE_CONTENTTYPE ="text/html; charset=GBK";
53
54 private Hashtable paratable =new Hashtable();
55
56 private Hashtable filetable =new Hashtable();
57
58 private String strBoundary ="";
59
60 private String strSavePath="";
61
62
63 privatestaticvoid println(String s) {
64 System.out.println(s);
65 } 66
67
68
69
70 /** 71 * 增加資料到對應的Hashtable中
72 * 說明:如果Hashtable中已存在該鍵值,則將新增加的和原來的都封裝到列表中。
73 * @param table
74 * @param paraName
75 * @param paraValue
76 */ 77 privatestaticvoid addElement(Hashtable table, String paraName,
78 Object paraValue) {
79 ArrayList list =new ArrayList();
80 if (table.containsKey(paraName)) {
81 Object o = table.get(paraName);
82 if (o instanceof List) {
83 ((List) o).add(paraValue);
84 }else{
85 list.add(o);
86 list.add(paraValue);
87 o = list;
88 } 89 table.put(paraName, o);
90 }else{
91 table.put(paraName, paraValue);
92 } 93 } 94
95 publicstatic String getHashInfo(Hashtable paratable){
96 StringBuffer sb=new StringBuffer();
97 Set keySet=paratable.keySet();
98 Iterator it=keySet.iterator();
99 while(it.hasNext()){
100
101 Object keyobj=it.next();
102 Object valueobj=paratable.get(keyobj);
103
104 sb.append("<tr>");
105 sb.append("<td>"+keyobj.toString()+"</td>");
106 if(valueobj instanceof List){
107 sb.append("<td>");
108 int isize=((List)valueobj).size();
109 for(int i=0;i<isize;i++){
110 Object tempobj=((List)valueobj).get(i);
111 if(i<isize-1){
112 sb.append(tempobj.toString()+",");
113 }114 else{
115 sb.append(tempobj.toString());
116 }117 }118
119 sb.append("</td>");
120 }121 else{
122 sb.append("<td>"+valueobj.toString()+"</td>");
123 }124 sb.append("</tr>");
125 }126 return sb.toString();
127 }128
129
130 privatestaticbyte[] getfileBytes(InputStream is) {
131 List byteList =new ArrayList();
132 byte[] filebyte =null;
133 int readbyte =0;
134 try{
135 while ((readbyte = is.read()) !=-1) {
136 byteList.add(new Byte((byte) readbyte));
137 }138 }catch (FileNotFoundException e) {
139 e.printStackTrace();
140 }catch (IOException e) {
141 e.printStackTrace();
142 }143 filebyte =newbyte[byteList.size()];
144 for (int i =0; i < byteList.size(); i++) {
145 filebyte[i] = ((Byte) byteList.get(i)).byteValue();
146 }147 return filebyte;
148
149 }150
151
152
153
154 protectedvoid doGet(HttpServletRequest request,
155 HttpServletResponse response) throws ServletException, IOException {
156 doPost(request, response);
157 }158
159 protectedvoid doPost(HttpServletRequest request,
160 HttpServletResponse response) throws ServletException, IOException {
161 paratable =new Hashtable();
162 filetable =new Hashtable();
163 strSavePath=this.getInitParameter("savepath");
164 File file=new File(strSavePath);
165 if(!file.exists()){
166 file.mkdirs();
167 }168 String contentType = request.getContentType();
169 strBoundary = getBoundary(contentType);
170 ServletInputStream sis = request.getInputStream();
171 BufferedInputStream bis =new BufferedInputStream(sis);
172 parseInputStream(bis);
173 appendPara(request.getParameterMap()); /*追加url對應傳遞的引數*/174 response.setContentType(CHINESE_CONTENTTYPE);
175
176// response.getWriter().write(getOutPutInfo());
177// response.getWriter().write(new String(getfileBytes(sis),"GBK"));178 bis.close();
179 sis.close();
180 request.setAttribute("para",paratable);
181 request.setAttribute("file",filetable);
182
183 this.getServletContext().getRequestDispatcher("/result.jsp").
184 forward(request,response);
185
186 }187
188
189 /**190 * 不用Hashtable對應的put方法,目的避免覆蓋重複的鍵值
191 * @return192 */193 privatevoid appendPara(Map map){
194
195 if(map!=null){
196 Set keySet=map.keySet();
197 Iterator it=keySet.iterator();
198 while(it.hasNext()){
199 Object keyobj=it.next();
200 String[] valueobj=(String[])map.get(keyobj);
201 println("keyobj===="+keyobj);
202 println("valueobj===="+valueobj);
203 for(int i=0;i<valueobj.length;i++){
204 addElement(paratable,(String)keyobj,valueobj[i]);
205 }206 }207 }<
37
38 publicfinalstatic String DEFAULT_ENCODING ="ISO8859_1";
39
40 publicfinalstatic String CHINESE_ENCODING ="GBK";
41
42 publicfinalstatic String SIGN_BOUNDARY ="boundary=";
43
44 publicfinalstatic String SIGN_FORMELEMENT ="name=";
45
46 publicfinalstatic String SIGN_FORMFILE
47
48 publicfinalstatic String SIGN_NOTFILE ="application/octet-stream";
49
50 publicfinalstatic String SIGN_MULTIDATA ="multipart/form-data";
51
52 publicfinalstatic String CHINESE_CONTENTTYPE ="text/html; charset=GBK";
53
54 private Hashtable paratable =new Hashtable();
56 private Hashtable filetable =new Hashtable();
57
58 private String strBoundary ="";
59
60 private String strSavePath="";
61
62
63 privatestaticvoid println(String s) {
64 System.out.println(s);
65 } 66
67
68
69
70 /** 71 * 增加資料到對應的Hashtable中
73 * @param table
74 * @param paraName
75 * @param paraValue
76 */ 77 privatestaticvoid addElement(Hashtable table, String paraName,
78 Object paraValue) {
79 ArrayList list =new ArrayList();
80 if (table.containsKey(paraName)) {
81 Object o = table.get(paraName);
82 if (o instanceof List) {
83 ((List) o).add(paraValue);
84 }else{
85 list.add(o);
86 list.add(paraValue);
87 o = list;
88 } 89 table.put(paraName, o);
90 }else{
91 table.put(paraName, paraValue);
92 } 93 } 94
95 publicstatic String getHashInfo(Hashtable paratable){
96 StringBuffer sb=new StringBuffer();
97 Set keySet=paratable.keySet();
98 Iterator it=keySet.iterator();
99 while(it.hasNext()){
100
101 Object keyobj=it.next();
102 Object valueobj=paratable.get(keyobj);
103
104 sb.append("<tr>");
105 sb.append("<td>"+keyobj.toString()+"</td>");
106 if(valueobj instanceof List){
107 sb.append("<td>");
108 int isize=((List)valueobj).size();
109 for(int i=0;i<isize;i++){
110 Object tempobj=((List)valueobj).get(i);
111 if(i<isize-1){
112 sb.append(tempobj.toString()+",");
113 }114 else{
115 sb.append(tempobj.toString());
116 }117 }118
119 sb.append("</td>");
120 }121 else{
122 sb.append("<td>"+valueobj.toString()+"</td>");
123 }124 sb.append("</tr>");
125 }126 return sb.toString();
127 }128
129
130 privatestaticbyte[] getfileBytes(InputStream is) {
131 List byteList =new ArrayList();
132 byte[] filebyte =null;
133 int readbyte =0;
134 try{
135 while ((readbyte = is.read()) !=-1) {
136 byteList.add(new Byte((byte) readbyte));
137 }138 }catch (FileNotFoundException e) {
139 e.printStackTrace();
140 }catch (IOException e) {
141 e.printStackTrace();
142 }143 filebyte =newbyte[byteList.size()];
144 for (int i =0; i < byteList.size(); i++) {
145 filebyte[i] = ((Byte) byteList.get(i)).byteValue();
146 }147 return filebyte;
148
149 }150
151
152
153
154 protectedvoid doGet(HttpServletRequest request,
155 HttpServletResponse response) throws ServletException, IOException {
156 doPost(request, response);
157 }158
159 protectedvoid doPost(HttpServletRequest request,
160 HttpServletResponse response) throws ServletException, IOException {
161 paratable =new Hashtable();
162 filetable =new Hashtable();
163 strSavePath=this.getInitParameter("savepath");
164 File file=new File(strSavePath);
165 if(!file.exists()){
166 file.mkdirs();
167 }168 String contentType = request.getContentType();
169 strBoundary = getBoundary(contentType);
170 ServletInputStream sis = request.getInputStream();
171 BufferedInputStream bis =new BufferedInputStream(sis);
172 parseInputStream(bis);
173 appendPara(request.getParameterMap()); /*追加url對應傳遞的引數*/174 response.setContentType(CHINESE_CONTENTTYPE);
175
176// response.getWriter().write(getOutPutInfo());
177// response.getWriter().write(new String(getfileBytes(sis),"GBK"));178 bis.close();
179 sis.close();
180 request.setAttribute("para",paratable);
181 request.setAttribute("file",filetable);
182
183 this.getServletContext().getRequestDispatcher("/result.jsp").
184 forward(request,response);
185
186 }187
188
189 /**190 * 不用Hashtable對應的put方法,目的避免覆蓋重複的鍵值
191 * @return192 */193 privatevoid appendPara(Map map){
194
195 if(map!=null){
196 Set keySet=map.keySet();
197 Iterator it=keySet.iterator();
198 while(it.hasNext()){
199 Object keyobj=it.next();
200 String[] valueobj=(String[])map.get(keyobj);
201 println("keyobj===="+keyobj);
202 println("valueobj===="+valueobj);
203 for(int i=0;i<valueobj.length;i++){
204 addElement(paratable,(String)keyobj,valueobj[i]);
205 }206 }207 }<