一、配置文件
spring:
servlet:
multipart:
enabled: true
max-file-size: 10MB
max-request-size: 100MB
resolve-lazily: false
二、web层
@PostMapping("updateReporting")
public R updateReporting(@ApiParam(name = "id") Integer id, @ApiParam(name = "reportingResult") String reportingResult, @RequestParam("files") MultipartFile[] files) {
return manageReportingService.updateReporting(id, reportingResult, files);
}
三、业务层
public R updateReporting(Integer id, String reportingResult, MultipartFile[] files) {
Reporting byId = getById(id);
byId.setReportingResult(reportingResult);
//获取图片上传的目录
File uploadDirectory = new File(fileUploadProperties.getReporting());
if (!uploadDirectory.exists()) {
uploadDirectory.mkdir();
}
try {
if (files != null && files.length >= 1) {
//循环上传图片或者视频
for (MultipartFile file : files) {
String fileName = file.getOriginalFilename();
//判断是否有文件且是否为图片文件
if (fileName != null && !"".equalsIgnoreCase(fileName.trim()) && isImageFile(fileName)) {
if (null != byId.getReportingResultPic()) {
byId.setReportingResultPic(byId.getReportingResultPic() + "," + fileUploadProperties.getReportingPath() + fileName);
} else {
byId.setReportingResultPic(fileUploadProperties.getReportingPath() + fileName);
}
//上传图片
//创建输出文件对象
File outFile = new File(fileUploadProperties.getReporting() + fileName);
//第一种 直接上传
// file.transferTo(outFile);
//第二种 拷贝文件到输出文件对象
FileUtils.copyInputStreamToFile(file.getInputStream(), outFile);
}
//判断是否有文件且是否为视频文件
if (fileName != null && !"".equalsIgnoreCase(fileName.trim()) && isVideoFile(fileName)) {
if (null != byId.getReportingResultVideo()) {
byId.setReportingResultVideo(byId.getReportingResultVideo() + "," + fileUploadProperties.getReportingPath() + fileName);
} else {
byId.setReportingResultVideo(fileUploadProperties.getReportingPath() + fileName);
}
//上传视频
//创建输出文件对象
File outFile = new File(fileUploadProperties.getReporting() + fileName);
//第一种 直接上传
// file.transferTo(outFile);
//第二种 拷贝文件到输出文件对象
FileUtils.copyInputStreamToFile(file.getInputStream(), outFile);
}
}
}
} catch (IOException e) {
e.printStackTrace();
return R.fail(R.Code.FAIL, R.Msg.FAIL, "上传失败");
}
//设置状态为已处理
byId.setIsClose(2);
//更新投诉数据
saveOrUpdate(byId);
return R.success("上传成功");
}
四、工具类
package com.dmo.parkingServer.common.util;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
public class FileUtils {
//链接url下载图片
public static void downloadFile(String urlList, String path, String picName) {
URL url;
try {
url = new URL(urlList);
DataInputStream dataInputStream = new DataInputStream(url.openStream());
File filePath = new File(path);
if (!filePath.exists()) filePath.mkdirs();
File picFile = new File(path + "/" + picName);
FileOutputStream fileOutputStream = new FileOutputStream(picFile);
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
fileOutputStream.write(output.toByteArray());
dataInputStream.close();
fileOutputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* @Author jt
* @Description //是否是图片
* @Date 2020/8/7
* @Param [fileName]
* @return java.lang.Boolean
**/
public static Boolean isImageFile(String fileName) {
String[] img_type = new String[]{".jpg", ".jpeg", ".png"};
if (fileName == null) {
return false;
}
fileName = fileName.toLowerCase();
for (String type : img_type) {
if (fileName.endsWith(type)) {
return true;
}
}
return false;
}
/*
* @Author jt
* @Description //是否是视频
* @Date 2020/8/7
* @Param [fileName]
* @return java.lang.Boolean
**/
public static Boolean isVideoFile(String fileName) {
String[] video_type = new String[]{".mp4"};
if (fileName == null) {
return false;
}
fileName = fileName.toLowerCase();
for (String type : video_type) {
if (fileName.endsWith(type)) {
return true;
}
}
return false;
}
}