1. 程式人生 > 其它 >SpringBoot整合MarkDown實現圖片上傳和回顯

SpringBoot整合MarkDown實現圖片上傳和回顯

SpringBoot整合MarkDown實現檔案上傳和回顯(採用先上傳後回顯).

application.yml檔案中進行如下配置
這裡說明一下

  • 如果你此處不配下面這些的話,你就需要在你檔案上傳時指明你要上傳的路徑
  • 如果你配了下面的配置的話,並且檔案上傳時不指定上傳路徑,你整個專案的檔案上傳的儲存位置就是如下的位置。
  • 如果你配了下面的配置,同時檔案上傳時又指定檔案上傳的路徑,以你檔案上傳時指定的為準
#檔案上傳的路徑         需要你在電腦中有對應的目錄
spring:
  servlet:
    multipart:
      location: E:/cache/

application.properties檔案中進行如下配置

# 設定圖片大小
# 單檔案大小
spring.servlet.multipart.max-file-size=10MB
# 多檔案最大的大小
spring.servlet.multipart.max-request-size=100MB

#如果是在上傳時指定路徑的話可以這配置中配置,然後使用@Value註解獲取
file.upload.editpicturePath=E:/cache/editPicture/

Controller類

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;
    }

工具類

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 {
    /**
     * 上傳檔案
     * @param file
     * @return 返回檔案路徑(以相對路徑放回)
     */
    public static String uploadFile(MultipartFile file, String filePath) {
        if(file.isEmpty()) {
            return "";
        }
        // 獲取原檔名
        String originFileName = file.getOriginalFilename();
        // 我們通過UUID 來重新重組檔名
        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("建立目錄ing");
            // 上面的 newFile.getParentFile() 已經保證了不為null.
            if(newFile.getParentFile().mkdirs()) {
                System.out.println("建立目錄成功");
            }else {
                System.out.println("建立目錄失敗");
                return "";
            }
        }
        try {
            //開始上傳檔案
            file.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
        return returnPath;
    }

}

因為在檔案上傳時,有些配置老是弄不清楚,網上說的也不是很清楚,所以就自己總結了一下,怕忘記了,就記錄一下,方便以後查閱。如果覺得有幫助,給個贊吧,讓更多的人看到。
也可以關注一下我的公眾號,大家一起學習。