FileUploadService.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package nckd.yjk.yj.plugin.docLibrary.utils;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.UnsupportedEncodingException;
  7. import java.nio.charset.Charset;
  8. import java.util.Iterator;
  9. import java.util.Map;
  10. import org.apache.http.HttpEntity;
  11. import org.apache.http.HttpResponse;
  12. import org.apache.http.client.ClientProtocolException;
  13. import org.apache.http.client.ResponseHandler;
  14. import org.apache.http.client.methods.HttpPost;
  15. import org.apache.http.entity.mime.FormBodyPart;
  16. import org.apache.http.entity.mime.HttpMultipartMode;
  17. import org.apache.http.entity.mime.MultipartEntity;
  18. import org.apache.http.entity.mime.content.InputStreamBody;
  19. import org.apache.http.entity.mime.content.StringBody;
  20. import org.apache.http.util.EntityUtils;
  21. import com.alibaba.fastjson.JSONObject;
  22. public class FileUploadService {
  23. private static final FileUploadService service = new FileUploadService();
  24. private static final String BOUNDARY = "----WebKitFormBoundaryBHp47SjGMxpHX0X6--";
  25. private FileUploadService() {}
  26. //静态工厂方法
  27. public static FileUploadService getService() {
  28. return service;
  29. }
  30. public String postFile(String url, Map<String, Object> param, File file, String accessToken) throws Exception {
  31. HttpPost httppost = new HttpPost(url);
  32. httppost.addHeader("api", "true");
  33. httppost.addHeader("accessToken", accessToken);
  34. //设置boundary,文件解析用
  35. httppost.addHeader("Content-Type", "multipart/form-data; boundary="+BOUNDARY);
  36. MultipartEntity entry = this.getMutipartEntry(param, file);
  37. httppost.setEntity(entry);
  38. return HttpService.getService().doExecuteByHttpClient(httppost, new ResponseHandler<String>() {
  39. @Override
  40. public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
  41. HttpEntity entity = response.getEntity();
  42. String rtn = EntityUtils.toString(entity, "UTF-8");
  43. JSONObject json = JSONObject.parseObject(rtn);
  44. String url = json.getString("url");
  45. return url;
  46. }
  47. });
  48. }
  49. private MultipartEntity getMutipartEntry(Map<String, Object> param, File file)
  50. throws UnsupportedEncodingException, FileNotFoundException {
  51. if (file == null) {
  52. throw new IllegalArgumentException("文件不能为空");
  53. }
  54. MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, BOUNDARY, Charset.forName("UTF-8"));
  55. multipartEntity.addPart("file", new InputStreamBody(new FileInputStream(file), file.getName()));
  56. if (param != null) {
  57. Iterator<String> iterator = param.keySet().iterator();
  58. while (iterator.hasNext()) {
  59. String key = iterator.next();
  60. FormBodyPart field = new FormBodyPart(key, new StringBody((String) param.get(key)));
  61. multipartEntity.addPart(field);
  62. }
  63. }
  64. return multipartEntity;
  65. }
  66. }