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 realize the function of uploading single File and multiple Files by SpringBoot

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to achieve single-file and multi-file upload functions in SpringBoot. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. Single file upload

First create a Spring Boot project and add spring-boot-starter-web dependencies

Then create a upload.jsp file and upload a simple file to the page, as shown in the following code:

Title

The upload API is wjsc/upload. Note that the request method is post,enctype and multipart/form-data.

Then create an API for uploading files:

@ RequestMapping ("/ wjsc") @ RestControllerpublic class UploadController {/ * File upload * / @ PostMapping ("/ upload") / / MultipartFile accepts the file public String upload (MultipartFile uploadFile,HttpServletRequest req) {SimpleDateFormat sdf = new SimpleDateFormat ("yyyy/MM/dd/") sent from the front desk / / set the save path of the uploaded file to the uploadFile folder String realPath = req.getSession (). GetServletContext (). GetRealPath ("/ uploadFile/") under the project running directory; / / get the current date to sort and save the uploaded files String format = sdf.format (new Date ()); File folder = new File (realPath+format) If (! folder.isDirectory ()) {/ / determine whether it is a directory folder.mkdirs (); / / create a directory if it is not a directory} / / rename the uploaded file String oldName = uploadFile.getOriginalFilename (); / / get the file name String newName = UUID.randomUUID (). ToString () + oldName.substring (oldName.lastIndexOf ("."), oldName.length ()) / / set the new file name try {/ / File save operation uploadFile.transferTo (new File (folder,newName)); / / generate the file access path String filePath = req.getScheme () + ": / /" + req.getServerName () + ":" + req.getServerPort () + "/ uploadFile/" + format+newName; return filePath } catch (Exception e) {e.printStackTrace ();} return "upload failed";}}

Note: the MultipartFile variable name should be the same as the name of the file uploaded in jsp, otherwise the file will not be received

Final test:

Run the project and access the upload.jsp page in the browser for file upload

After a successful upload, the file access path will be returned, and the image you just uploaded can be accessed with this path.

You can also see the success of the picture you just uploaded in the project.

At this point, a simple single file upload is completed.

two。 Multiple file upload

Uploading multiple files is basically the same as uploading a single file. Modify the jsp file first, as follows:

Title

Then modify the API for uploading files:

/ * multiple file upload * / @ PostMapping ("/ uploads") public String uploads (MultipartFile [] uploadFiles,HttpServletRequest req) {/ / traversing the uploadFiles array to store respectively}

The core logic in the controller is the same as single file upload, only one more traversal step.

On "SpringBoot how to achieve single-file and multi-file upload function" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

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