Spring boot allows you to upload and download images and spring images.
This blog briefly introduces the problems that spring boot has encountered in uploading and downloading. First, create a spring boot project.
1. core controller code
Package com. qwrt. station. websocket. controller; import com. alibaba. fastjson. JSONObject; import com. qwrt. station. common. util. jsonUtil; import org. slf4j. logger; import org. slf4j. loggerFactory; import org. springframework. beans. factory. annotation. value; import org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. bind. annotation. requestMethod; import org. springframework. Web. bind. annotation. requestParam; import org. springframework. web. bind. annotation. restController; import org. springframework. web. multipart. multipartFile; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import java. io. *;/*** Created by jack on April /10/30. * // @ RestController @ RequestMapping ("v1/uploadDownload") public class UploadDownloadController {pri Vate static final Logger logger = LoggerFactory. getLogger (UploadDownloadController. class); @ Value ("$ {uploadDir}") private String uploadDir; @ RequestMapping (value = "/uploadImage", method = RequestMethod. POST) public JSONObject uploadImage (@ RequestParam (value = "file") MultipartFile file) throws RuntimeException {if (file. isEmpty () {return JsonUtil. getFailJsonObject ("file cannot be 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 (" the uploaded suffix is: "+ suffixName); // the uploaded file path String filePath = uploadDir; // solve the Chinese problem. The Chinese path under liunx is, image Display problem // fileName = UUID. randomUUID () + suffixName; File dest = new File (filePath + fileName); // check whether the directory if (! Dest. getParentFile (). exists () {dest. getParentFile (). mkdirs ();} try {file. transferTo (dest); logger.info ("the file path after the upload is successful is not:" + filePath + fileName); return JsonUtil. getSuccessJsonObject (fileName);} catch (IllegalStateException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} return JsonUtil. getFailJsonObject ("File Upload Failed");} // file download code @ RequestMapping (value = "/downloadImage", me Thod = RequestMethod. GET) public String downloadImage (String imageName, HttpServletRequest request, HttpServletResponse response) {// String fileName = "123.JPG"; logger. debug ("the imageName is:" + imageName); String fileUrl = uploadDir + imageName; if (fileUrl! = Null) {// The current File is retrieved from the WEB-INF of the project // File // (the directory can be configured in the following line of code) and then downloaded to C: \ users \ downloads is the default download directory of the Local Machine/* String realPath = request. getServletContext (). getRealPath ("// WEB-INF //"); * // * File file = new File (realPath, fileName); */File file File = new File (fileUrl ); if (file. exists () {response. setContentType ("application/force-download"); // set to force download. response is not enabled. addHeader ("Content-Disposition", "attachment; fileName = "+ ImageName); // 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 ;}}
The above code has two methods: the above method is the Image Upload method, the following method is the image download method. To download an image, you need to input the image file name, which can be tested on ios, android phones, and Google browsers. There is no problem with uploading and downloading images.
2. The core code of the tested html is as follows:
<! DOCTYPE html>
The above code has nothing to do with uploading and downloading images. you can remove it as needed. You need to introduce jquery to view the core code for uploading and downloading images.
3. spring boot attribute Configuration:
1. Solve the Problem of uploading large images:
Spring: http: multipart: max-file-size: 100 Mb # file upload size max-request-size: 200 Mb # maximum request size
This is the new version of spring boot to solve the problem of uploading images or files too much. The boss does not solve this problem. You can query the information by yourself.
2. Configuration File Upload and storage location:
# Upload location uploadDir: F: \ mystudy \ pic \
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.
Lantern Festival benefits: