119 lines
4.3 KiB
Java
119 lines
4.3 KiB
Java
package com.xkrs.controller;
|
|
|
|
import com.xkrs.common.encapsulation.PromptMessageEnum;
|
|
import com.xkrs.dao.FilePathDao;
|
|
import com.xkrs.model.entity.FilePath;
|
|
import com.xkrs.utils.FileFastDfs;
|
|
import com.xkrs.utils.FileUtil;
|
|
import org.springframework.context.i18n.LocaleContextHolder;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import javax.annotation.Resource;
|
|
import java.io.File;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Locale;
|
|
import java.util.Map;
|
|
|
|
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;
|
|
import static com.xkrs.utils.FileUtil.getUploadInfo;
|
|
|
|
/**
|
|
* @author XinYi Song
|
|
* 文件上传
|
|
*/
|
|
@RestController
|
|
public class FileController {
|
|
|
|
@Resource
|
|
private FileFastDfs fileFastDfs;
|
|
|
|
@Resource
|
|
private FilePathDao filePathDao;
|
|
|
|
/**
|
|
* 上传多张图片
|
|
* @param files
|
|
* @return
|
|
*/
|
|
@PostMapping("/uploadFileMore")
|
|
public String uploadFileMore(MultipartFile[] files) throws Exception {
|
|
Locale locale = LocaleContextHolder.getLocale();
|
|
if (null == files || files.length == 0){
|
|
return outputEncapsulationObject(PromptMessageEnum.PARAM_NULL, "result or fileType is null", locale);
|
|
}
|
|
List list = new ArrayList();
|
|
for (MultipartFile file :
|
|
files) {
|
|
boolean m = FileUtil.checkFileSize(file.getSize(), 100, "M");
|
|
if(m == false){
|
|
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "图片大小不能超过100M", locale);
|
|
}
|
|
File file1 = fileFastDfs.multipartFileToFile(file);
|
|
String dir = "fire_point";
|
|
String info = fileFastDfs.uploadFile(file1, dir);
|
|
Map<String, String> map = getUploadInfo(info);
|
|
file1.delete();
|
|
FilePath filePath = new FilePath();
|
|
filePath.setFilePath(map.get("path"));
|
|
filePath.setFileName(map.get("fileName"));
|
|
filePath.setSize(map.get("size"));
|
|
filePath.setMtime(map.get("mtime"));
|
|
filePath.setUrl(map.get("url"));
|
|
filePath.setMd5(map.get("md5"));
|
|
filePath.setScene(map.get("scene"));
|
|
filePathDao.save(filePath);
|
|
}
|
|
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "上传成功!", locale);
|
|
}
|
|
|
|
/**
|
|
* 上传单张图片
|
|
* @param file
|
|
* @return
|
|
*/
|
|
@PostMapping("/uploadFile")
|
|
public String uploadFile(MultipartFile file) throws Exception {
|
|
Locale locale = LocaleContextHolder.getLocale();
|
|
if (null == file){
|
|
return outputEncapsulationObject(PromptMessageEnum.PARAM_NULL, "result or fileType is null", locale);
|
|
}
|
|
boolean m = FileUtil.checkFileSize(file.getSize(), 100, "M");
|
|
if(m == false){
|
|
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "图片大小不能超过100M", locale);
|
|
}
|
|
File file1 = fileFastDfs.multipartFileToFile(file);
|
|
String dir = "fire_point";
|
|
String info = fileFastDfs.uploadFile(file1, dir);
|
|
Map<String, String> map = getUploadInfo(info);
|
|
file1.delete();
|
|
FilePath filePath = new FilePath();
|
|
filePath.setFilePath(map.get("path"));
|
|
filePath.setFileName(map.get("fileName"));
|
|
filePath.setSize(map.get("size"));
|
|
filePath.setMtime(map.get("mtime"));
|
|
filePath.setUrl(map.get("url"));
|
|
filePath.setMd5(map.get("md5"));
|
|
filePath.setScene(map.get("scene"));
|
|
filePathDao.save(filePath);
|
|
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "上传成功!", locale);
|
|
}
|
|
|
|
/**
|
|
* 查询火点图片路径
|
|
* @return
|
|
*/
|
|
@GetMapping("/findPath")
|
|
public String findPath(){
|
|
Locale locale = LocaleContextHolder.getLocale();
|
|
List<Map<String, String>> path = filePathDao.findPath();
|
|
if(path == null || path.size() == 0){
|
|
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "还未上传任何图片信息!", locale);
|
|
}
|
|
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, path, locale);
|
|
}
|
|
}
|