I plan to introduce distributed file systems for storing images, Word, Excel, AND PDF files of applications. Before I begin my introduction to distributed file systems, LET me introduce you to the use of native storage to store file resources. The core implementation process is the same for both:

  • Upload files and save files (local disks in this section)
  • Return the HTTP access service path of the file to the front end to display the effect after uploading

A review,

What static resource directories can SpringBoot use to provide file access?

  • classpath:/META-INF/resources/ ,
  • classpath:/static/ ,
  • classpath:/public/ ,
  • classpath:/resources/

This is what we showed you before, and you can see that the static resources are all in the classpath. Then there is the problem:

  • Application file resources cannot be stored separately from project code (have you ever seen github upload code with project file data?)
  • Project packaging is difficult. As more and more files are uploaded, the package jar of the project becomes larger and larger.
  • Code and file data cannot be stored separately, which means that backing up file data becomes complicated

2. Customize the file upload directory

How to solve the above problems? Don’t forget that Spring Boot provides us with locations to configure custom static files using spring.resources.static-locations.

web:
  upload-path: D:/data/

spring:
  resources:
    static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${web.upload-path}
Copy the code
  • configurationweb.upload-pathStatic resource path separate from project code, i.e., file upload save root path
  • configurationspring.resources.static-locationsAdd file:${web.upload-path} to the external file resource upload path. Static resources in this path can directly provide HTTP access services.

Iii. Controller implementation of file uploading

See the code comment for details

@restController Public Class FileUploadController {// Bind file upload path to uploadPath@value ("${web.upload-path}") private String uploadPath; SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/"); @PostMapping("/upload") public String upload(MultipartFile uploadFile, HttpServletRequest Request) {// Upload files to uploadPath by date // for example: /2019/06/06/cf13891e-4b95-4000-81eb-b6d70ae44930.png String format = sdf.format(new Date()); File folder = new File(uploadPath + format); if (! folder.isDirectory()) { folder.mkdirs(); } / / to upload file renaming, avoid file moniker String oldName = uploadFile. GetOriginalFilename (); String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length()); Try {// Save uploadfile.transferto (new File(folder, newName)); String filePath = request.getScheme() + "://" + Request.getServername () + ":" + Request.getServerPort () +  format + newName; return filePath; } catch (IOException e) { throw new CustomException(CustomExceptionType.SYSTEM_ERROR); }}}Copy the code

Four, write a simulated file upload page, test

Put the upload. HTML file in the classpath:public directory for external access.

<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/upload" Method ="post" enctype="multipart/form-data"> <input type="file" name="uploadFile" value=" please select uploadFile" > <input type="submit" </form> </body> </ HTML >Copy the code

Access the test, click Select File, and save

The file is saved to the resource directory specified by web.upload-path on the server

The browser responds with an HTTP access path to the file:

Using the HTTP access path, the browser access effect is as follows. To prove that our file has been successfully uploaded to the server, we need to access the image through the HTTP URL.

Welcome to my blog, where there are many fine collections

  • This article is reprinted with a credit (must be accompanied by a link, not only the text) : Antetokounmpo blog.

Feel helpful to you, help me like, share! Your support is my inexhaustible creative power! . In addition, the author recently a period of time output as follows boutique content, looking forward to your attention.

  • Spring Boot2.0 by Hand
  • Spring Security- JWT-OAUTH2
  • RBAC Authority Management System for Actual Combat Front-end and Back-end Separation
  • “Actual SpringCloud Micro-service from Bronze to King”
  • VUE Series