图片文章关联实体
@Entity
@Table(name = "article_picture")
public class ArticlePictureEntity extends BaseEntity implements Serializable {
/**
* 图片地址
*/
private String url;
/**
* 文章id
*/
private String contentId;
//===========忽略getter setter和构造函数==============
}
service和dao忽略
我的项目中是用了springdatajpa框架写dao
就是一个保存实体的save方法
工具类
文件上传工具类
/**
* @description: 文件上传工具类
* @author: yuxiang
* @create: 2019-12-02 15:54
**/
public class FileUtils {
/**
*
* @param file 文件
* @param path 文件存放路径
* @param fileName 原文件名
* @return
*/
public static boolean upload(MultipartFile file, String path, String fileName){
// 生成新的文件名
String realPath = path + "/" + FileNameUtils.getFileName(fileName);
//使用原文件名
// String realPath = path + "/" + fileName;
File dest = new File(realPath);
//判断文件父目录是否存在
if(!dest.getParentFile().exists()){
dest.getParentFile().mkdir();
}
try {
//保存文件
file.transferTo(dest);
return true;
} catch (IllegalStateException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
生成文件名字工具类
/**
* @description: 生成文件名工具类
* @author: yuxiang
* @create: 2019-12-02 15:53
**/
public class FileNameUtils {
/**
* 获取文件后缀
* @param fileName
* @return
*/
public static String getSuffix(String fileName){
return fileName.substring(fileName.lastIndexOf("."));
}
/**
* 生成新的文件名
* @param fileOriginName 源文件名
* @return
*/
public static String getFileName(String fileOriginName){
return UUIDUtils.getUUID() + FileNameUtils.getSuffix(fileOriginName);
}
}
/**
* @description:
* @author: yuxiang
* @create: 2019-12-02 15:54
**/
public class UUIDUtils {
public static String getUUID(){
return UUID.randomUUID().toString().replace("-", "");
}
}
上传文件controller
首先要在application.yml配置,也就是下面的@Value("${web.upload-path}")
来源
# 配置外部访问文件
web:
upload-path: D:/image/
/**
* @description: 文件上传
* @author: yuxiang
* @create: 2019-12-02 15:40
**/
@RequestMapping("file")
@Controller
public class FileController {
private final ResourceLoader resourceLoader;
@Autowired
public FileController(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
@Value("${web.upload-path}")
private String path;
@Autowired
private ArticlePictureService articlePictureService;
/**
* 上传图片文件
*
* @param file
* @return
*/
@PostMapping("imgUpload")
@ResponseBody
public String imgUpload(@RequestParam("img") MultipartFile file,
@RequestParam("articleId") String id) {
String response = " ";
if (file.isEmpty()) {
return new ApiResponse(-1, "文件为空!");
}
try {
//1.定义上传的文件
String localPath = path;
//2.获得文件名字
String fileName = file.getOriginalFilename();
//3.上传
//3.1 生成新的文件名
String realPath = localPath + "/" + FileNameUtils.getFileName(fileName);
//3.2 保存文件
File dest = new File(realPath);
//判断文件目目录是否存在,不存在则新建
if (!dest.getParentFile().exists()){
dest.getParentFile().mkdir();
}
file.transferTo(dest);
//保存路径到数据库
ArticlePictureEntity entity = new ArticlePictureEntity(id, realPath);
articlePictureService.save(entity);
response = "上传成功";
} catch (Exception e) {
e.printStackTrace();
response = "服务器出现错误,上传失败";
}
return response;
}
更新
源码在我的资源库里面可以下载,不需要币