文件上传

时间 2021/5/25 9:02:30 加载中...

文件上传

程序调用接口上传文件

java语言
使用 HttpHelper.uploadFile 调用接口
HttpHelper

  1. public static void testUpload() throws Exception {
  2. String serverUrl = "http://localhost:8182//normalValue/fileImport";
  3. String localFilePath = "e:/abc.png";
  4. //附带其他信息
  5. Map<String, String> map = new HashMap<>();
  6. map.put("key", "value");
  7. String result = HttpHelper.uploadFile(serverUrl, localFilePath, "file", map, null);
  8. System.out.println(result);
  9. Reulst theResult = JSON.parseObject(result, Reulst.class);
  10. if (theResult.getStatus() != 0) {
  11. throw new Exception("上传失败了");
  12. }
  13. }

Vue 前端调用接口上传文件

  1. <form action="saveFile" enctype="multipart/form-data" method="post" id="fileUpload1">
  2. <el-upload
  3. class="upload-demo"
  4. action="saveFile"
  5. id="filename1"
  6. name="file"
  7. ref="upload"
  8. :auto-upload="false"
  9. :on-change="fileChange"
  10. accept=".xlsx,.xls"
  11. >
  12. <span class="btn-line">
  13. <svg class="icon" aria-hidden="true">
  14. <use xlink:href="#icon-upload" />
  15. </svg>
  16. <span>上传文件</span>
  17. </span>
  18. <span class="cg9">支持xls,xlsx格式文件</span>
  19. </el-upload>
  20. </form>
  1. let form = document.getElementById('fileUpload1')
  2. let postData = new FormData(form)
  3. this.post('/normalValue/fileImport', postData, {})

fiddler 捕捉到得报文信息

  1. POST http://localhost:8088/normalValue/fileImport HTTP/1.1
  2. Host: localhost:8088
  3. Connection: keep-alive
  4. Content-Length: 169100
  5. Accept: application/json, text/plain, */*
  6. X-Requested-With: XMLHttpRequest
  7. User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36
  8. Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryYATxcz9sXQ9VnKfw
  9. Origin: http://localhost:8088
  10. Sec-Fetch-Site: same-origin
  11. Sec-Fetch-Mode: cors
  12. Sec-Fetch-Dest: empty
  13. Accept-Encoding: gzip, deflate, br
  14. Accept-Language: zh-CN,zh;q=0.9
  15. Cookie: showcode=; TABLE_SESSION=ecc438bb-c36b-46fa-a54d-59f5c6a2cbc0
  16. ------WebKitFormBoundaryYATxcz9sXQ9VnKfw
  17. Content-Disposition: form-data; name="file"; filename="QQ图片20200827141856.jpg"
  18. Content-Type: image/jpeg
  19. JFIF    C  
  20.  等等信息
  21. ------WebKitFormBoundaryYATxcz9sXQ9VnKfw--

文件接收

java语言

  1. @PostMapping("/normalValue/fileImport")
  2. @ResponseBody
  3. public BaseResponse<String> fileImportForClassStandard(HttpServletRequest request) {
  4. BaseResponse<String> result = new BaseResponse<String>();
  5. MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
  6. //获取附带其他信息
  7. Map<String, String[]> map = multipartHttpServletRequest.getParameterMap();
  8. MultipartFile file = multipartHttpServletRequest.getFile("file");
  9. String count = "";
  10. byte[] content = file.getBytes();
  11. String fileName = file.getOriginalFilename();
  12. String extName = fileName.substring(fileName.lastIndexOf("."));
  13. if (!extName.equals(".xls") && !extName.equals(".xlsx")) {
  14. result.setCode(500);
  15. result.setMsg("请上传 xls 或 xlsx 类型的文件");
  16. return result;
  17. }
  18. String filePath = getFilePath(file);
  19. FileOutputStream outputStream = new FileOutputStream(filePath);
  20. outputStream.write(content);
  21. outputStream.close();
  22. //然后对 filePath 得文件进行处理
扫码分享
版权说明
作者:SQBER
文章来源:http://www.sqber.com/articles/file-upload.html
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。