SpringBoot integrates MarkDown to implement file upload and echo (upload before echo).
The following configuration is performed in the application.yml file
- If you don’t qualify for the following, you’ll need to specify where you want to upload your files when you upload them
- If you configure the following configuration and do not specify the upload path, your entire project file upload will be saved in the following location.
- If you have configured the following configuration and specified the file upload path, use the one you specified when uploading the file
Spring: Servlet: multipart: location: E:/cache/Copy the code
Perform the following configuration in the application.properties file
# set the picture size # single file size spring. The servlet. Multipart. Max - file - size = more than 10 MB # file. The size of the largest spring servlet. Multipart. Max - request - size = 100 MB # if specified path when uploading can configure this configuration, and then use the @ Value annotation for the file. The upload. EditpicturePath = E: / cache/editPicture /Copy the code
The Controller class
package cn.laochou.markdown.controller; import cn.laochou.markdown.pojo.Article; import cn.laochou.markdown.service.ArticleService; import cn.laochou.markdown.utils.FileUtils; import com.alibaba.fastjson.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; import java.io.IOException; @Controller @RequestMapping("/article") public class ArticleController { @Value("${file.upload.editpicturePath}") private String filePath; private final ArticleService articleService; @Autowired public ArticleController(ArticleService articleService) { this.articleService = articleService; } @RequestMapping("/image/upload") @ResponseBody public JSONObject imageUpload(@RequestParam("editormd-image-file") MultipartFile image){ JSONObject jsonObject = new JSONObject(); if(image ! = null) { String path = FileUtils.uploadFile(image, filePath); System.out.println(path); jsonObject.put("url", path); jsonObject.put("success", 1); jsonObject.put("message", "upload success!" ); return jsonObject; } jsonObject.put("success", 0); jsonObject.put("message", "upload error!" ); return jsonObject; }Copy the code
Utility class
package cn.laochou.markdown.utils; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.util.UUID; /** * public class FileUtils {/** * upload file * @param file * @return Return file path */ public static String uploadFile(MultipartFile file, String filePath) { if(file.isEmpty()) { return ""; String originFileName = file.getoriginalfilename (); String uid = uuid.randomuuid ().toString(); originFileName = uid + originFileName; String returnPath =filePath + originFileName; File newFile = new File(returnPath); if(newFile.getParentFile() ! = null && ! Newfile.getparentfile ().exists()) {system.out.println (" create directory ing"); // newfile.getparentFile () is not null. If (newfile.getparentFile ().mkdirs()) {system.out.println (" directory created successfully "); }else {system.out.println (" failed to create directory "); return ""; }} try {// Start uploading file.transferto (newFile); } catch (IOException e) { e.printStackTrace(); return ""; } return returnPath; }}Copy the code
Because when the file is uploaded, some configuration is always not clear, what the net says is not very clear, so oneself summarized once, afraid to forget, record, convenient later consult. If you find it helpful, give it a “like” and let more people see it.
You can also pay attention to my public number, we learn together.