In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use Java SpringBoot to achieve file upload function, I hope you will gain something after reading this article, let's discuss it together!
Test code
Pom.xml:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.2 jar com.kaven springboot 0.0.1-SNAPSHOT springboot springboot 1.8 org.springframework.boot spring-boot-starter-web org.projectlombok lombok Org.springframework.boot spring-boot-maven-plugin
Application.properties (profile):
Spring.servlet.multipart.max-file-size=500MB
Spring.servlet.multipart.max-request-size=500MB
Max-file-size: specifies the maximum size of files allowed to be uploaded. The default is 1MB.
Max-request-size: specifies the maximum size allowed for multipart/form-data requests. The default value is 10MB.
Upload API:
Package com.kaven.springboot.controller;import org.apache.tomcat.util.http.fileupload.IOUtils;import org.springframework.http.HttpStatus;import org.springframework.util.StringUtils;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletResponse;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream @ RestControllerpublic class FilesController {@ PostMapping (value= "/ upload", headers= "content-type=multipart/form-data") public String upload (@ RequestParam (value= "file") MultipartFile file, HttpServletResponse response) throws IOException, InterruptedException {System.out.println ("file upload request has come in"); FileOutputStream fileOutputStream = null; InputStream inputStream = null Try {/ / whether there is an uploaded file if (file! = null & &! file.isEmpty ()) {/ / if the uploaded file exists, get its original file name String fileName = file.getOriginalFilename () If (StringUtils.hasText (fileName)) {/ / store the uploaded file under the E disk of the server (Windows) java.io.File outFile = new java.io.File ("E:\" + fileName); / / create a file output stream instance fileOutputStream = new FileOutputStream (outFile) based on outFile / / get the input stream of the uploaded file inputStream = file.getInputStream () / * * copy bytes from the input stream to the output stream * this method buffers the input internally, so there is no need to use BufferedInputStream * large streams (more than 2GB) will return a byte replication value of-1 after the copy is complete Because the correct number of bytes cannot be returned as int * for large streams, you need to use the copyLarge (InputStream, OutputStream) method * parameter: * input-InputStream * output to read-OutputStream to write * * / IOUtils.copy (inputStream, fileOutputStream) }} else {/ / File does not exist response.setStatus (HttpStatus.BAD_REQUEST.value ()); return "File does not exist";}} catch (Exception e) {/ / File upload error e.printStackTrace () Response.setStatus (HttpStatus.INTERNAL_SERVER_ERROR.value ()); return "File upload error";} finally {if (fileOutputStream! = null) {fileOutputStream.flush (); fileOutputStream.close ();}} / File uploaded successfully response.setStatus (HttpStatus.OK.value ()) Return "File uploaded successfully";}}
Startup class:
Package com.kaven.springboot;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class SpringbootApplication {public static void main (String [] args) {SpringApplication application = new SpringApplication (SpringbootApplication.class); application.run (args);}}
Use Postman for testing.
The uploaded file is complete and can be played (video file).
The uploaded file does not exist.
Output from the console.
After reading this article, I believe you have a certain understanding of "how to use Java SpringBoot to achieve file upload function". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.