@PostMapping("upload") public ResponseVo upload(@RequestParam("files[]") MultipartFile[] files) { Map<String, Object> resultMap = new HashMap<>(); if (ArrayUtils.isEmpty(files)) { //resultMap.put("message", MessageUtil.getMessage("upload.error.no_files")); return new ResponseVo(ErrorCode.ACTION_ERROR).setMsg("没有上传文件"); } MultipartFile multipartFile = files[0]; File tmpFile = new File(System.currentTimeMillis() + "_" + multipartFile.getOriginalFilename()); try (BufferedOutputStream outBuff = new BufferedOutputStream(new FileOutputStream(tmpFile)); BufferedInputStream inBuff = new BufferedInputStream(multipartFile.getInputStream())) { // Buffered arrays byte[] b = new byte[1024 * 5]; int len; while ((len = inBuff.read(b)) != -1) { outBuff.write(b, 0, len); } // Flush the output stream of this buffer outBuff.flush(); resultMap.put("message", MessageUtil.getMessage("upload.success")); resultMap.put("data", tmpFile); } catch (Exception e) { log.error(e.getMessage(), e); return new ResponseVo().setMsg("upload failed"); } finally { FileUtils.deleteQuietly(tmpFile); } return new ResponseVo().setData(resultMap); }
Result of postman call:
</ div>
Read More:
- SpringCloud Use openFeign Multipartfile to Upload Files Error: Current request is not a multipart request
- JAVA: How to Use Minio to upload pictures
- After asynchronous file import and springboot multipartfile upload, the @async asynchronous processing reports an error: nosuchfileexception
- How to Solve Springboot Upload Files Error: The field XXX exceeds its maximum permitted size of 1048576 bytes
- [Solved] Spring upload file Error: Multipartfile Transferto() reported an error FileNotFoundException
- How to Use filechannel to copy files
- Multipartfiletofileutils (multipartfile to file)
- How to Solve IntelliJ IDEA Error: Cannot determine path to ‘tools.jar‘ library for 17 (C:\Program Files\Java\jd…
- Java: How to use itext to export PDF text absolute positioning (implementation method)
- How to Solve Image Upload Error: Uncaught (in promise) DOMException: Failed to execute ‘put‘ on ‘IDBObjectStore‘
- [Solved] Pycharm Failed to Upload: Upload to *** failed. Could not list the contents of folder “sftp
- Springboot Files Upload Limit Error: The field file exceeds its maximum permitted size of 1048576 bytes
- How to Upload Docker Image to docker hub
- [Solved] Upload Files Error: Request processing failed;nested exception is org.springframework.web.multipart.MultipartExcepti
- JAVA 8: How to Convert List to Map
- How to Solve RAR Files Extract Error with sevenzipjbinding (Multithread Processing)
- JAVA: How to use the third-party font library method (using the third-party TTF / TTC font library method)
- How to Open Files by Dragging and Dropping in C#
- How to Solve java server error (java application Run Normally)
- JAVA: How to Convert PDF from Color to Grayscale