SpringBoot background implements file upload and download, and springboot File Upload
How does the SpringBoot background upload and download files?
A recent project involves file upload and download. The front-end upload adopts the Baidu webUploader plug-in. The usage of this plug-in is still being studied. Record it later. This article describes how to upload and download files in the SpringBoot background.
Upload a single file
// Single file upload @ RequestMapping (value = "/upload") @ ResponseBodypublic String upload (@ RequestParam ("file") MultipartFile file) {try {if (file. isEmpty () {return "file is blank";} // get the file name String fileName = file. getOriginalFilename (); logger.info ("the uploaded file name is:" + fileName); // get the file suffix String suffixName = fileName. substring (fileName. lastIndexOf (". "); logger.info (" file Suffix: "+ suffixName); // set the file storage path String filePath = "D: // aim //"; String path = filePath + fileName + suffixName; File dest = new File (path); // check whether a directory exists if (! Dest. getParentFile (). exists () {dest. getParentFile (). mkdirs (); // create a folder} file. transferTo (dest); // return "uploaded successfully";} catch (IllegalStateException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} return "Upload Failed ";}
To modify the file path and file name, modify filePath and fileName.
Multi-File Upload
// Multifile upload @ RequestMapping (value = "/uploadMore", method = RequestMethod. POST) @ ResponseBodypublic String handleFileUpload (HttpServletRequest request) {List <MultipartFile> files = (MultipartHttpServletRequest) request ). getFiles ("file"); MultipartFile file = null; BufferedOutputStream stream = null; for (int I = 0; I <files. size (); ++ I) {file = files. get (I); String filePath = "D: // aim //"; if (! File. isEmpty () {try {byte [] bytes = file. getBytes (); stream = new BufferedOutputStream (new FileOutputStream (new File (filePath + file. getOriginalFilename (); // sets the file path and name stream. write (bytes); // write stream. close () ;}catch (Exception e) {stream = null; return "no." + I + "File Upload Failed ==>" + e. getMessage () ;}} else {return "+" + I + "File Upload failed because the file is empty" ;}} return "uploaded successfully ";}
File Download
// File download code @ RequestMapping ("/download") public String downloadFile (HttpServletRequest request, HttpServletResponse response) {String fileName = "aim_test.txt"; // set the file name, replace it with the file name to be downloaded if (fileName! = Null) {// set the File path String realPath = "D: // aim //" file File = new file (realPath, fileName); if (File. exists () {response. setContentType ("application/force-download"); // set to force download. response is not enabled. addHeader ("Content-Disposition", "attachment; fileName =" + fileName); // set the file name byte [] buffer = new byte [1024]; FileInputStream FCM = null; bufferedInputStream bis = null; try {FD = new FileInputStream (file); bis = New BufferedInputStream (FCM); OutputStream OS = response. getOutputStream (); int I = bis. read (buffer); while (I! =-1) {OS. write (buffer, 0, I); I = bis. read (buffer);} System. out. println ("success");} catch (Exception e) {e. printStackTrace ();} finally {if (bis! = Null) {try {bis. close () ;}catch (IOException e) {e. printStackTrace () ;}} if (FS! = Null) {try {fs. close ();} catch (IOException e) {e. printStackTrace () ;}}} return null ;}
MultipartConfig Configuration
You can use the MultipartConfig configuration class to globally control file uploads.
@ Configurationpublic class MultipartConfig {@ Bean public MultipartConfigElement multipartConfigElement () {MultipartConfigFactory factory = new MultipartConfigFactory (); // if the file size limit is exceeded, an exception message factory is thrown. setMaxFileSize ("2 MB"); // KB, MB // set the total size of uploaded data factory. setMaxRequestSize ("20 MB"); // sets the path of the temporary file folder // factory. setLocation ("E: // test //"); // if the file is greater than this value, it will be stored as a file. If the file is smaller than this value, it will be stored in the memory, the default value is 0 // factory. setMaxRequestSize (0); return factory. createMultipartConfig ();}}
Notes
The format of front-end and back-end file transmission should be multipart/form-data
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.