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;
}
}
因为在文件上传时,有些配置老是弄不清楚,网上说的也不是很清楚,所以就自己总结了一下,怕忘记了,就记录一下,方便以后查阅。如果觉得有帮助,给个赞吧,让更多的人看到。
也可以关注一下我的公众号,大家一起学习。