日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HttpClient通过Post上传文件(转)

發布時間:2023/12/19 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HttpClient通过Post上传文件(转) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在之前一段的項目中,使用Java模仿Http Post方式發送參數以及文件,單純的傳遞參數或者文件可以使用URLConnection進行相應的處理。

????????? 但是項目中涉及到既要傳遞普通參數,也要傳遞多個文件(不是單純的傳遞XML文件)。在網上尋找之后,發現是使用HttClient來進行響應的操作,起初嘗試多次依然不能傳遞參數和傳遞文件,后來發現時因為當使用HttpClient時,不能使用request.getParameter()對普通參數進行獲取,而要在服務器端使用Upload來進行操作。

????????? HttpClient4.2 jar下載 :http://download.csdn.net/detail/just_szl/4370574

???????? 客戶端代碼:

?

[java]?view plaincopy
  • import?java.io.ByteArrayOutputStream;??
  • import?java.io.File;??
  • import?java.io.IOException;??
  • import?java.io.InputStream;??
  • import?org.apache.http.HttpEntity;??
  • import?org.apache.http.HttpResponse;??
  • import?org.apache.http.HttpStatus;??
  • import?org.apache.http.ParseException;??
  • import?org.apache.http.client.HttpClient;??
  • import?org.apache.http.client.methods.HttpPost;??
  • import?org.apache.http.entity.mime.MultipartEntity;??
  • import?org.apache.http.entity.mime.content.FileBody;??
  • import?org.apache.http.impl.client.DefaultHttpClient;??
  • import?org.apache.http.util.EntityUtils;??
  • ??
  • /**?
  • ?*??
  • ?*?@author?<a?href="mailto:just_szl@hotmail.com">?Geray</a>?
  • ?*?@version?1.0,2012-6-12??
  • ?*/??
  • public?class?HttpPostArgumentTest2?{??
  • ??
  • ????//file1與file2在同一個文件夾下?filepath是該文件夾指定的路徑??????
  • ????public?void?SubmitPost(String?url,String?filename1,String?filename2,?String?filepath){??
  • ??????????
  • ????????HttpClient?httpclient?=?new?DefaultHttpClient();??
  • ??????????
  • ????????try?{??
  • ??????
  • ????????????HttpPost?httppost?=?new?HttpPost(url);??
  • ??????????????
  • ????????????FileBody?bin?=?new?FileBody(new?File(filepath?+?File.separator?+?filename1));??
  • ????????????????
  • ????????????FileBody?bin2?=?new?FileBody(new?File(filepath?+?File.separator?+?filename2));??
  • ??????????????
  • ????????????StringBody?comment?=?new?StringBody(filename1);??
  • ??
  • ????????????MultipartEntity?reqEntity?=?new?MultipartEntity();??
  • ??????????????
  • ????????????reqEntity.addPart("file1",?bin);//file1為請求后臺的File?upload;屬性??????
  • ?????????????reqEntity.addPart("file2",?bin2);//file2為請求后臺的File?upload;屬性??
  • ?????????????reqEntity.addPart("filename1",?comment);//filename1為請求后臺的普通參數;屬性?????
  • ????????????httppost.setEntity(reqEntity);??
  • ??????????????
  • ????????????HttpResponse?response?=?httpclient.execute(httppost);??
  • ??????????????
  • ??????????????????
  • ????????????int?statusCode?=?response.getStatusLine().getStatusCode();??
  • ??????????????
  • ??????????????????
  • ????????????if(statusCode?==?HttpStatus.SC_OK){??
  • ??????????????????????
  • ????????????????System.out.println("服務器正常響應.....");??
  • ??????????????????
  • ????????????????HttpEntity?resEntity?=?response.getEntity();??
  • ??????????????????
  • ??????????????????
  • ????????????????System.out.println(EntityUtils.toString(resEntity));//httpclient自帶的工具類讀取返回數據??
  • ??????????????????
  • ??????????????????
  • ??????????????????
  • ????????????????System.out.println(resEntity.getContent());?????
  • ??
  • ????????????????EntityUtils.consume(resEntity);??
  • ????????????}??
  • ??????????????????
  • ????????????}?catch?(ParseException?e)?{??
  • ????????????????//?TODO?Auto-generated?catch?block??
  • ????????????????e.printStackTrace();??
  • ????????????}?catch?(IOException?e)?{??
  • ????????????????//?TODO?Auto-generated?catch?block??
  • ????????????????e.printStackTrace();??
  • ????????????}?finally?{??
  • ????????????????try?{???
  • ????????????????????httpclient.getConnectionManager().shutdown();???
  • ????????????????}?catch?(Exception?ignore)?{??
  • ??????????????????????
  • ????????????????}??
  • ????????????}??
  • ????????}??
  • ??????
  • ??????
  • ????/**?
  • ?????*?@param?args?
  • ?????*/??
  • ????public?static?void?main(String[]?args)?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ??????????
  • ????????HttpPostArgumentTest2?httpPostArgumentTest2?=?new?HttpPostArgumentTest2();??
  • ??????????
  • ????????httpPostArgumentTest2.SubmitPost("http://127.0.0.1:8080/demo/receiveData.do",??
  • ????????????????"test.xml","test.zip","D://test");??
  • ????}??
  • ??????
  • }??
  • 服務端代碼:

    ?

    ?

    [java]?view plaincopy
  • public?void?receiveData(HttpServletRequest?request,?HttpServletResponse?response)?throws?AppException{??
  • ??
  • ????????PrintWriter?out?=?null;??
  • ????????response.setContentType("text/html;charset=UTF-8");??
  • ??????????
  • ????????Map?map?=?new?HashMap();??
  • ????????FileItemFactory?factory?=?new?DiskFileItemFactory();??
  • ????????ServletFileUpload?upload?=?new?ServletFileUpload(factory);??
  • ????????File?directory?=?null;????
  • ????????List<FileItem>?items?=?new?ArrayList();??
  • ????????try?{??
  • ????????????items?=?upload.parseRequest(request);??
  • ????????????//?得到所有的文件??
  • ????????????Iterator<FileItem>?it?=?items.iterator();??
  • ????????????while?(it.hasNext())?{??
  • ????????????????FileItem?fItem?=?(FileItem)?it.next();??
  • ????????????????String?fName?=?"";??
  • ????????????????Object?fValue?=?null;??
  • ????????????????if?(fItem.isFormField())?{?//?普通文本框的值??
  • ????????????????????fName?=?fItem.getFieldName();??
  • //??????????????????fValue?=?fItem.getString();??
  • ????????????????????fValue?=?fItem.getString("UTF-8");??
  • ????????????????????map.put(fName,?fValue);??
  • ????????????????}?else?{?//?獲取上傳文件的值??
  • ????????????????????fName?=?fItem.getFieldName();??
  • ????????????????????fValue?=?fItem.getInputStream();??
  • ????????????????????map.put(fName,?fValue);??
  • ????????????????????String?name?=?fItem.getName();??
  • ????????????????????if(name?!=?null?&&?!("".equals(name)))?{??
  • ????????????????????????name?=?name.substring(name.lastIndexOf(File.separator)?+?1);??
  • ??????????????????????????
  • //??????????????????????String?stamp?=?StringUtils.getFormattedCurrDateNumberString();??
  • ????????????????????????String?timestamp_Str?=?TimeUtils.getCurrYearYYYY();??
  • ????????????????????????directory?=?new?File("d://test");????
  • ?????????????????????????????directory.mkdirs();??
  • ??????????????????????????
  • ????????????????????????String?filePath?=?("d://test")+?timestamp_Str+?File.separator?+?name;??
  • ????????????????????????map.put(fName?+?"FilePath",?filePath);??
  • ??????????????????????????
  • ????????????????????????InputStream?is?=?fItem.getInputStream();??
  • ????????????????????????FileOutputStream?fos?=?new?FileOutputStream(filePath);??
  • ????????????????????????byte[]?buffer?=?new?byte[1024];??
  • ????????????????????????while?(is.read(buffer)?>?0)?{??
  • ????????????????????????????fos.write(buffer,?0,?buffer.length);??
  • ????????????????????????}??
  • ????????????????????????fos.flush();??
  • ????????????????????????fos.close();??
  • ????????????????????????map.put(fName?+?"FileName",?name);??
  • ????????????????????}??
  • ????????????????}??
  • ????????????}??
  • ????????}?catch?(Exception?e)?{??
  • ????????????System.out.println("讀取http請求屬性值出錯!");??
  • //??????????e.printStackTrace();??
  • ????????????logger.error("讀取http請求屬性值出錯");??
  • ????????}??
  • ??????????
  • ????????//?數據處理??
  • ??????????
  • ??????????
  • ??????????
  • ??????????
  • ????????try?{??
  • ????????????out?=?response.getWriter();??
  • ????????????out.print("{success:true,?msg:'接收成功'}");??
  • ????????????out.close();??
  • ????????}?catch?(IOException?e)?{??
  • ????????????e.printStackTrace();??
  • ????????}??
  • ??
  • ??
  • ????}??


  • http://blog.csdn.net/Just_szl/article/details/7659347

    總結

    以上是生活随笔為你收集整理的HttpClient通过Post上传文件(转)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。