OkHttp 通过post请求上传文件

举报
小米粒-biubiubiu 发表于 2020/12/02 23:09:53 2020/12/02
【摘要】 一、添加pom 依赖 <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp --><dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</...

一、添加pom 依赖


  
  1. <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
  2. <dependency>
  3. <groupId>com.squareup.okhttp3</groupId>
  4. <artifactId>okhttp</artifactId>
  5. <version>3.10.0</version>
  6. </dependency>
二、上产文件接口编写

  
  1. @ApiOperation("上传文件(移动云)")
  2. @PostMapping("/uploadFileToOos")
  3. public ResultJson uploadFileToOos(@RequestParam("file") MultipartFile file) throws IOException {
  4. String fileName = file.getOriginalFilename();
  5. File sendFile = new File("/tmp/tmp-" + fileName + "");
  6. try {
  7. file.transferTo(sendFile);
  8. OkHttpClient httpClient = new OkHttpClient();
  9. MediaType mediaType = okhttp3.MediaType.parse(org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE);
  10. String policy = "{ \"expiration\": \"" + OosCommonUtil.getExpireTime() + "\",\n" +
  11. " \"conditions\": [\n" +
  12. " {\"bucket\": \"" + haimen_bucket + "\" },\n" +
  13. "{\"key\":\"" + fileName + "\"}" +
  14. " ]\n" +
  15. "}";
  16. RequestBody fileBody = RequestBody.create(mediaType, sendFile);
  17. String base64Policy = OosCommonUtil.getBase64Policy(policy);
  18. String signature = OosCommonUtil.signWithHmacSha1(secretAccessKey, base64Policy);
  19. RequestBody requestBody = new MultipartBody.Builder().
  20. setType(MultipartBody.FORM)
  21. .addFormDataPart("key", fileName)
  22. .addFormDataPart("AccessKeyId", accessKeyId)
  23. .addFormDataPart("policy", base64Policy)
  24. .addFormDataPart("signature", signature)
  25. .addFormDataPart("file", fileName, fileBody).build();
  26. Request request = new Request.Builder()
  27. .post(requestBody)
  28. .url(obsUri)
  29. .addHeader("Date", OosCommonUtil.getObsHeaderDate(System.currentTimeMillis()))
  30. .addHeader("Host", host)
  31. .addHeader("Content-Type", org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE)
  32. .addHeader("Origin", origin)
  33. .build();
  34. Response response = httpClient.newCall(request).execute();
  35. if (response != null) {
  36. if (response.code() == 204) {
  37. AliOssResponse aliOssResponse = new AliOssResponse();
  38. aliOssResponse.setSize(file.getSize());
  39. aliOssResponse.setUrl(obsUri + "/" + fileName);
  40. return ResultJson.getInstance().setData(aliOssResponse).setCode(200).setMsg("success");
  41. } else {
  42. return ResultJson.getInstance().setData(null).setCode(200).setMsg("error");
  43. }
  44. }
  45. return ResultJson.getInstance().setData(null).setCode(200).setMsg("error");
  46. } finally {
  47. if (sendFile.exists()) {
  48. sendFile.delete();
  49. }
  50. }
  51. }

 

文章来源: blog.csdn.net,作者:血煞风雨城2018,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq_31905135/article/details/90369100

推荐

华为开发者空间发布

让每位开发者拥有一台云主机

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。