Commit 183e710d authored by liqin's avatar liqin 💬

bug fixed

parent 7b3c1d86
...@@ -25,7 +25,10 @@ import java.io.ByteArrayInputStream; ...@@ -25,7 +25,10 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.*; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@Slf4j @Slf4j
@Component @Component
...@@ -51,10 +54,19 @@ public class FastDFSUtils { ...@@ -51,10 +54,19 @@ public class FastDFSUtils {
FastDFSUtils.imageConfig = thumbImageConfig; 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); boolean isImage = FileTypeUtil.isImageByExtension(fileName);
if (isImage) { if (isImage) {
String mimeType = FileUtil.getMimeType(fileName); String mimeType = FileUtil.getMimeType(fileName);
...@@ -71,12 +83,12 @@ public class FastDFSUtils { ...@@ -71,12 +83,12 @@ public class FastDFSUtils {
throw new InterfaceException("400", "文件不是图片类型"); 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); Map<String, Object> map = CopyStreamUtils.copyInputStream(inputStream);
String md5 = (String) map.get("md5"); String md5 = (String) map.get("md5");
InputStream is = (InputStream) map.get("inputStream"); 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("fileName", "fileName"));
metaDataSet.add(new MetaData("fileSize", size + "")); metaDataSet.add(new MetaData("fileSize", size + ""));
metaDataSet.add(new MetaData("fileExtName", FilenameUtils.getExtension(fileName))); metaDataSet.add(new MetaData("fileExtName", FilenameUtils.getExtension(fileName)));
......
...@@ -2,7 +2,7 @@ package cn.wisenergy.chnmuseum.party.common.enums; ...@@ -2,7 +2,7 @@ package cn.wisenergy.chnmuseum.party.common.enums;
public enum FileTypeEnum { public enum FileTypeEnum {
DOC("Doc", "文档"), DATUM("Datum", "资料"),
IMAGE("Image", "图片"), IMAGE("Image", "图片"),
AUDIO("Audio", "音频"), AUDIO("Audio", "音频"),
VIDEO("Video", "视频"); VIDEO("Video", "视频");
......
...@@ -95,8 +95,6 @@ public class FileTypeUtil { ...@@ -95,8 +95,6 @@ public class FileTypeUtil {
is.read(b); is.read(b);
fileType = getFileTypeByStream(b); fileType = getFileTypeByStream(b);
is.close(); is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -140,9 +138,6 @@ public class FileTypeUtil { ...@@ -140,9 +138,6 @@ public class FileTypeUtil {
ImageReader reader = iter.next(); ImageReader reader = iter.next();
iis.close(); iis.close();
return reader.getFormatName(); return reader.getFormatName();
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
......
...@@ -45,6 +45,9 @@ public class FileUploadController { ...@@ -45,6 +45,9 @@ public class FileUploadController {
// 允许上传的格式 // 允许上传的格式
private static final String[] VIDEO_TYPE = new String[]{"MP4", "FLV"}; 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) // @RequestMapping(value = "/upload", method = RequestMethod.POST)
// public ResponseEntity<PicUploadResult> upload(@RequestParam(value = "bc_cover", required = true) MultipartFile uploadFile, HttpServletResponse response) throws Exception { // public ResponseEntity<PicUploadResult> upload(@RequestParam(value = "bc_cover", required = true) MultipartFile uploadFile, HttpServletResponse response) throws Exception {
// // 校验文件扩展名 // // 校验文件扩展名
...@@ -95,9 +98,67 @@ public class FileUploadController { ...@@ -95,9 +98,67 @@ public class FileUploadController {
// return ResponseEntity.status(HttpStatus.CREATED).body(fileUploadResult); // 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 = "单图片上传") @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) { if (uploadFile == null || uploadFile.getSize() == 0) {
throw new InterfaceException(RESPONSE_CODE_ENUM.REQUEST_PARAMS_ERROR.getCode(), "没有文件可供上传"); throw new InterfaceException(RESPONSE_CODE_ENUM.REQUEST_PARAMS_ERROR.getCode(), "没有文件可供上传");
} }
...@@ -126,7 +187,7 @@ public class FileUploadController { ...@@ -126,7 +187,7 @@ public class FileUploadController {
@PostMapping(value = "/video/upload", headers = "content-type=multipart/form-data", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) @PostMapping(value = "/video/upload", headers = "content-type=multipart/form-data", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@RequiresPermissions("video:upload") @RequiresPermissions("video:upload")
@ApiOperation(value = "多视频上传", notes = "多视频上传") @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) { if (files.length == 0) {
throw new InterfaceException(RESPONSE_CODE_ENUM.REQUEST_PARAMS_ERROR.getCode(), "没有文件可供上传"); throw new InterfaceException(RESPONSE_CODE_ENUM.REQUEST_PARAMS_ERROR.getCode(), "没有文件可供上传");
} }
...@@ -160,7 +221,7 @@ public class FileUploadController { ...@@ -160,7 +221,7 @@ public class FileUploadController {
handleResult.setHandleResult(HANDLE_STATUS_ENUM.SUCCESS.getName()); handleResult.setHandleResult(HANDLE_STATUS_ENUM.SUCCESS.getName());
boolean anyMatch = Arrays.stream(VIDEO_TYPE).anyMatch(s -> Objects.equals(s, FilenameUtils.getExtension(originalFilename).toUpperCase())); boolean anyMatch = Arrays.stream(VIDEO_TYPE).anyMatch(s -> Objects.equals(s, FilenameUtils.getExtension(originalFilename).toUpperCase()));
if (anyMatch) { if (anyMatch) {
String url = FastDFSUtils.uploadFile(file.getInputStream(), file.getSize(), originalFilename); String url = FastDFSUtils.uploadVideo(file.getInputStream(), file.getSize(), originalFilename);
handleResult.setFileUrl(url); handleResult.setFileUrl(url);
handleResult.setDescription("操作成功"); handleResult.setDescription("操作成功");
videoUrlList.add(url); 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