Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem if you can't find the temporary directory for uploading SpringBoot files?

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "SpringBoot file upload temporary directory can not find how to solve", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "SpringBoot file upload temporary directory can not find how to solve" it!

Problem with temporary directory for uploading SpringBoot files

I believe everyone will encounter this problem when deploying the project to the server:

Org.springframework.web.multipart.MultipartException: Could not parse multipart servlet

Request;nested exception is java.io.IOException: The temporary upload location

[/ tmp/tomcat.1337767218595042057.80/work/Tomcat/localhost/ROOT] is not valid

Org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest (StandardMultipart

HttpServletRequest.java:112) org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.

This problem is also puzzling me for a long time. I found no solution on the Internet. The only solution is to specify a temporary directory to the server on ok. No nonsense. Put the code on.

1. The front desk must be uploaded with the component multipart/form-data springMvc.

2. Just refer to the background code. I use SFTP to upload files to the CVM.

File upload tool refer to this article

/ / www.jb51.net/article/216456.htm

@ Log ("File upload") @ ResponseBody@PostMapping ("/ upload") @ RequiresPermissions ("common:companyremittance:upload") R upload (@ RequestParam ("file") MultipartFile file,@RequestParam ("arrys") String arrys, @ RequestParam ("commpanyname") String commpanyname,@RequestParam ("remittancedate") String remittancedate,HttpServletRequest request) throws ParseException, IOException {/ / upload to the image server first and then read the information int prostatusId=0 If (StringUtils.isEmpty (arrys)) {return R.error ("Import failed");} String oldName = file.getOriginalFilename (); / / use the IDUtils utility class to generate a new file name, new file name = newName + file suffix String newName = IDUtils.genImageName (); newName = newName + oldName.substring (oldName.lastIndexOf (".")); SftpUtils ft = new SftpUtils () / / pass parameters through SFtoInfo parameter connection: Session s = ft.getSession (SFTPInfo.SFTP_REQ_HOST,SFTPInfo.SFTP_DEFAULT_PORT, SFTPInfo.SFTP_REQ_USERNAME,SFTPInfo.SFTP_REQ_PASSWORD); Channel channel = ft.getChannel (s); ChannelSftp sftp = (ChannelSftp) channel; Boolean upload = ft.uploadFile (sftp,SFTPInfo.basePath, file.getInputStream (), newName); SaveDO saveDO=new SaveDO () If (upload) {/ / upload successful shutdown message saveDO.setFileurl (SFTPInfo.baseUrl+ "/" + newName); saveDO.setFilename (oldName); saveDO.setFilesize (String.valueOf (file.getSize (); saveDO.setImportdate (new Date ()); saveDO.setIpaddr (IPUtils.getIpAddr (request)); saveDO.setUsername (ShiroUtils.getUser (). GetUsername ()) SaveDO.setSftpupurl ("/ usr/fjl/file"); / / here is the file path upload server path remember chomd 777saveDO.setNewfilename (newName); saveService.save (saveDO); / / query the status of the project based on proentId CompanyremittanceDO companyremittanceDO1=new CompanyremittanceDO (); companyremittanceDO1.setProparentid (Long.valueOf (arrys)); CompanyremittanceDO aDo = companyremittanceService.getProjectStatus (arrys) If (aDo! = null) {companyremittanceDO1.setProstatus (aDo.getProstatus ());} else {/ / self-increasing status + + 1 prostatusId = companyremittanceService.getMaxProStatus (); prostatusId++; companyremittanceDO1.setProstatus (prostatusId);} companyremittanceDO1.setRemittancename (commpanyname); companyremittanceDO1.setRemittancedate (getDate (remittancedate)); String fileName = file.getOriginalFilename () FileName = FileUtil.renameToUUID (fileName); try {boolean flag= ExcelUtil.batchImport (fileName,file,companyremittanceService,companyremittanceDO1); if (flag) {if (ft.delete (saveDO.getSftpupurl (), saveDO.getNewfilename (), sftp)) {ft.closeAll (sftp, channel, s); / / close connection return R.ok ("Import successful") } else {return R.ok ("data import succeeded! The server data file was not deleted! ") ;} catch (Exception e) {e.printStackTrace (); return R.error ();} return R.error ();} else {return R.error ("upload error");}}

Where / usr/fjl/file is the address of the server path

3. So here comes the problem. Now this certainly won't solve the upload problem.

(key configuration) add a new configuration: this code is very critical, this is to specify the temporary address of the temporary upload path (local error will also be reported)

@ BeanMultipartConfigElement multipartConfigElement () {MultipartConfigFactory factory = new MultipartConfigFactory (); / / factory.setLocation ("D:/tmp"); factory.setLocation for local use ("/ home/temp"); / / server for return factory.createMultipartConfig ();}

Add this and the file can be uploaded successfully. If you cross-domain, you can also get the configuration and code configuration in nginx.

SpringBoot changes the temporary directory of uploaded files @ Bean MultipartConfigElement multipartConfigElement () {MultipartConfigFactory factory = new MultipartConfigFactory (); factory.setLocation (tmepPath); return factory.createMultipartConfig ();}

Add the above code to Application.java.

Thank you for your reading, the above is the "SpringBoot file upload temporary directory can not find how to solve" the content, after the study of this article, I believe you can not find how to solve this problem of SpringBoot file upload temporary directory has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report