Commit 183e710d authored by liqin's avatar liqin 💬

bug fixed

parent 7b3c1d86
......@@ -25,7 +25,10 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@Slf4j
@Component
......@@ -51,10 +54,19 @@ public class FastDFSUtils {
FastDFSUtils.imageConfig = thumbImageConfig;
}
public static String uploadFile(InputStream inputStream, long size, String fileName) {
final Set<MetaData> metaDataSet = new HashSet<>();
metaDataSet.add(new MetaData("fileName", "fileName"));
metaDataSet.add(new MetaData("fileSize", size + ""));
metaDataSet.add(new MetaData("fileExtName", FilenameUtils.getExtension(fileName)));
final StorePath storePath = storageClient.uploadFile(inputStream, size, FilenameUtils.getExtension(fileName), metaDataSet);
return dfsFileAccessBasePath + "/" + storePath.getFullPath();
}
/**
* 字节流方式上传
*/
public static Map<String, Object> uploadImage(InputStream inputStream, long fileSize, String fileName) {
public static Map<String, Object> uploadUeImage(InputStream inputStream, long fileSize, String fileName) {
boolean isImage = FileTypeUtil.isImageByExtension(fileName);
if (isImage) {
String mimeType = FileUtil.getMimeType(fileName);
......@@ -71,12 +83,12 @@ public class FastDFSUtils {
throw new InterfaceException("400", "文件不是图片类型");
}
public static String uploadFile(InputStream inputStream, long size, String fileName) {
public static String uploadVideo(InputStream inputStream, long size, String fileName) {
Map<String, Object> map = CopyStreamUtils.copyInputStream(inputStream);
String md5 = (String) map.get("md5");
InputStream is = (InputStream) map.get("inputStream");
final LinkedHashSet<MetaData> metaDataSet = new LinkedHashSet<>();
final Set<MetaData> metaDataSet = new HashSet<>();
metaDataSet.add(new MetaData("fileName", "fileName"));
metaDataSet.add(new MetaData("fileSize", size + ""));
metaDataSet.add(new MetaData("fileExtName", FilenameUtils.getExtension(fileName)));
......
......@@ -2,7 +2,7 @@ package cn.wisenergy.chnmuseum.party.common.enums;
public enum FileTypeEnum {
DOC("Doc", "文档"),
DATUM("Datum", "资料"),
IMAGE("Image", "图片"),
AUDIO("Audio", "音频"),
VIDEO("Video", "视频");
......
......@@ -95,8 +95,6 @@ public class FileTypeUtil {
is.read(b);
fileType = getFileTypeByStream(b);
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
......@@ -140,9 +138,6 @@ public class FileTypeUtil {
ImageReader reader = iter.next();
iis.close();
return reader.getFormatName();
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
......
......@@ -45,6 +45,9 @@ public class FileUploadController {
// 允许上传的格式
private static final String[] VIDEO_TYPE = new String[]{"MP4", "FLV"};
// 允许上传的格式
private static final String[] DATUM_TYPE = new String[]{"PDF", "DOC", "DOCX", "XLS", "XLSX", "PPT", "PPTX", "JPG", "JPEG", "PNG", "BMP", "WBMP", "MP4", "FLV"};
// @RequestMapping(value = "/upload", method = RequestMethod.POST)
// public ResponseEntity<PicUploadResult> upload(@RequestParam(value = "bc_cover", required = true) MultipartFile uploadFile, HttpServletResponse response) throws Exception {
// // 校验文件扩展名
......@@ -95,9 +98,67 @@ public class FileUploadController {
// return ResponseEntity.status(HttpStatus.CREATED).body(fileUploadResult);
// }
@PostMapping(value = "/upload", headers = "content-type=multipart/form-data", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ApiImplicitParams({
@ApiImplicitParam(name = "files", value = "资料", paramType = "form", dataType = "__file", collectionFormat = "array", allowMultiple = true)
})
@PostMapping(value = "/file/upload", headers = "content-type=multipart/form-data", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ApiOperation(value = "资料上传", notes = "资料上传")
public ResponseEntity<BatchUploadResVO> uploadFile(@RequestPart(value = "files", required = false) MultipartFile[] files) throws IOException {
if (files.length == 0) {
throw new InterfaceException(RESPONSE_CODE_ENUM.REQUEST_PARAMS_ERROR.getCode(), "没有文件可供上传");
}
int successCount = 0;
int failureCount = 0;
List<BatchUploadResVO.HandleResult> handleList = new ArrayList<>();
List<String> videoUrlList = new ArrayList<>();
for (MultipartFile file : files) {
// 当前维度表下线结果
BatchUploadResVO.HandleResult handleResult = new BatchUploadResVO.HandleResult();
// 原始文件名
String originalFilename = file.getOriginalFilename();
if (StringUtils.isBlank(originalFilename)) {
handleResult.setFileName("");
handleResult.setFileType(FileTypeEnum.DATUM.getName());
handleResult.setFileUrl("");
handleResult.setHandleResult(HANDLE_STATUS_ENUM.FAILURE.getName());
handleResult.setDescription("文件名为空");
failureCount++;
continue;
}
handleResult.setFileName(originalFilename);
handleResult.setFileType(FileTypeEnum.DATUM.getName());
handleResult.setHandleResult(HANDLE_STATUS_ENUM.SUCCESS.getName());
boolean anyMatch = Arrays.stream(DATUM_TYPE).anyMatch(s -> Objects.equals(s, FilenameUtils.getExtension(originalFilename).toUpperCase()));
if (anyMatch) {
String url = FastDFSUtils.uploadFile(file.getInputStream(), file.getSize(), originalFilename);
handleResult.setFileUrl(url);
handleResult.setDescription("操作成功");
videoUrlList.add(url);
successCount++;
} else {
handleResult.setFileUrl("");
handleResult.setDescription("文件格式不支持");
failureCount++;
}
// 设置处理的业务表信息
handleList.add(handleResult);
}
BatchUploadResVO batchUploadResVO = new BatchUploadResVO();
batchUploadResVO.setFailureCount(failureCount);
batchUploadResVO.setSuccessCount(successCount);
batchUploadResVO.setTotal(files.length);
batchUploadResVO.setHandleList(handleList);
batchUploadResVO.setUrlList(videoUrlList);
return ResponseEntity.status(HttpStatus.CREATED).body(batchUploadResVO);
}
@PostMapping(value = "/image/upload", headers = "content-type=multipart/form-data", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@RequiresPermissions("image:upload")
@ApiOperation(value = "单图片上传", notes = "单图片上传")
public ResponseEntity<ImageUploadResult> uploadFile(@RequestParam(value = "cover") MultipartFile uploadFile) throws Exception {
public ResponseEntity<ImageUploadResult> uploadImage(@RequestParam(value = "cover") MultipartFile uploadFile) throws Exception {
if (uploadFile == null || uploadFile.getSize() == 0) {
throw new InterfaceException(RESPONSE_CODE_ENUM.REQUEST_PARAMS_ERROR.getCode(), "没有文件可供上传");
}
......@@ -126,7 +187,7 @@ public class FileUploadController {
@PostMapping(value = "/video/upload", headers = "content-type=multipart/form-data", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@RequiresPermissions("video:upload")
@ApiOperation(value = "多视频上传", notes = "多视频上传")
public ResponseEntity<BatchUploadResVO> uploadFile(@RequestPart(value = "files", required = false) MultipartFile[] files) throws IOException {
public ResponseEntity<BatchUploadResVO> uploadVideo(@RequestPart(value = "files", required = false) MultipartFile[] files) throws IOException {
if (files.length == 0) {
throw new InterfaceException(RESPONSE_CODE_ENUM.REQUEST_PARAMS_ERROR.getCode(), "没有文件可供上传");
}
......@@ -160,7 +221,7 @@ public class FileUploadController {
handleResult.setHandleResult(HANDLE_STATUS_ENUM.SUCCESS.getName());
boolean anyMatch = Arrays.stream(VIDEO_TYPE).anyMatch(s -> Objects.equals(s, FilenameUtils.getExtension(originalFilename).toUpperCase()));
if (anyMatch) {
String url = FastDFSUtils.uploadFile(file.getInputStream(), file.getSize(), originalFilename);
String url = FastDFSUtils.uploadVideo(file.getInputStream(), file.getSize(), originalFilename);
handleResult.setFileUrl(url);
handleResult.setDescription("操作成功");
videoUrlList.add(url);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment