httpclient通过POST来上传文件,而不是通过流的形式,并在服务端进行解析(通过httpmime.jar来操作)

1. 首先需要对应的JAR包 导入 httpmime-4.1.1.jar。

[java] view plain copy print ?
  1. package url;
  2. import io.IoStreamUtil;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import org.apache.http.HttpEntity;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.client.ClientProtocolException;
  9. import org.apache.http.client.HttpClient;
  10. import org.apache.http.client.methods.HttpPost;
  11. import org.apache.http.entity.mime.MultipartEntity;
  12. import org.apache.http.entity.mime.content.FileBody;
  13. import org.apache.http.entity.mime.content.StringBody;
  14. import org.apache.http.impl.client.DefaultHttpClient;
  15. /**
  16. * httpclient上传文件
  17. * @author linwei
  18. *
  19. */
  20. public class MultipartEntityUtil {
  21. public static String postFile(File file,String url) throws ClientProtocolException, IOException {
  22. FileBody bin = null;
  23. HttpClient httpclient = new DefaultHttpClient();
  24. HttpPost httppost = new HttpPost(url);
  25. if(file != null) {
  26. bin = new FileBody(file);
  27. }
  28. StringBody username = new StringBody("13696900475");
  29. StringBody password = new StringBody("13696900475");
[java] view plain copy print ?
  1. //请记住,这边传递汉字会出现乱码,解决方法如下,设置好编码格式就好
[java] view plain copy print ?
  1. //new StringBody("汉字",Charset.forName("UTF-8")));
  2. MultipartEntity reqEntity = new MultipartEntity();
  3. reqEntity.addPart("username", username);
  4. reqEntity.addPart("password", password);
  5. reqEntity.addPart("data", bin);
  6. httppost.setEntity(reqEntity);
  7. System.out.println("执行: " + httppost.getRequestLine());
  8. HttpResponse response = httpclient.execute(httppost);
  9. System.out.println("statusCode is " + response.getStatusLine().getStatusCode());
  10. HttpEntity resEntity = response.getEntity();
  11. System.out.println("----------------------------------------");
  12. System.out.println(response.getStatusLine());
  13. if (resEntity != null) {
  14. System.out.println("返回长度: " + resEntity.getContentLength());
  15. System.out.println("返回类型: " + resEntity.getContentType());
  16. InputStream in = resEntity.getContent();
  17. System.out.println("in is " + in);
  18. System.out.println(IoStreamUtil.getStringFromInputStream(in));
  19. }
  20. if (resEntity != null) {
  21. resEntity.consumeContent();
  22. }
  23. return null;
  24. }
  25. public static void main(String[] args) throws ClientProtocolException, IOException {
  26. File file = new File("d:/rss.xml");
  27. String url = "http://localhost:8080/webtest/servlet/URLTest";
  28. postFile(file,url);
  29. }


2. 服务端的接收解析代码(同样的,服务端也需要导入上面提到的JAR包) (特别注释:在servlet中直接获取的request是可以正常解析的,但是,在ACTION中获取的request则是通过ACTION内部的类进行了封装,因此,在ACTION中执行以下代码,upload.parseRequest(request)方法执行返回的是空的,暂没找到解决办法。)

[java] view plain copy print ?
  1. //commons-fileupload.jar commons-io
  2. <SPAN style="WHITE-SPACE: pre"> </SPAN>request.setCharacterEncoding("UTF-8");
  3. <SPAN style="WHITE-SPACE: pre"> </SPAN>boolean isMultipart = ServletFileUpload.isMultipartContent(request);
  4. <SPAN style="WHITE-SPACE: pre"> </SPAN>
  5. <SPAN style="WHITE-SPACE: pre"> </SPAN>if (isMultipart) {
  6. FileItemFactory factory = new DiskFileItemFactory();
  7. ServletFileUpload upload = new ServletFileUpload(factory);
  8. try {
  9. List items = upload.parseRequest(request);
  10. Iterator iter = items.iterator();
  11. while (iter.hasNext()) {
  12. FileItem item = (FileItem) iter.next();
  13. if (item.isFormField()) {
  14. //普通文本信息处理
  15. String paramName = item.getFieldName();
  16. String paramValue = item.getString();
  17. System.out.println(paramName + ":" + paramValue);
  18. } else {
  19. //上传文件信息处理
  20. String fileName = item.getName();
  21. byte[] data = item.get();
  22. String filePath = "d:/ssssss.txt";
  23. FileOutputStream fos = new FileOutputStream(filePath);
  24. fos.write(data);
  25. fos.close();
  26. }
  27. }
  28. } catch (FileUploadException e) {
  29. e.printStackTrace();
  30. }
  31. }

你可能感兴趣的:(httpclient通过POST来上传文件,而不是通过流的形式,并在服务端进行解析(通过httpmime.jar来操作))