提交 f4ceea79 authored 作者: 宋文杰's avatar 宋文杰

fix(fastdfs): fix fastdfs upload return style

上级 60967692
...@@ -3,6 +3,7 @@ package com.elephant.framework.galaxy.aroad.module.common.controller; ...@@ -3,6 +3,7 @@ package com.elephant.framework.galaxy.aroad.module.common.controller;
import com.elephant.framework.galaxy.aroad.constant.CommonConstants; import com.elephant.framework.galaxy.aroad.constant.CommonConstants;
import com.elephant.framework.galaxy.aroad.exception.UploadFileException; import com.elephant.framework.galaxy.aroad.exception.UploadFileException;
import com.elephant.framework.galaxy.aroad.module.codegen.response.R; import com.elephant.framework.galaxy.aroad.module.codegen.response.R;
import com.elephant.framework.galaxy.aroad.response.UpFileResponse;
import com.github.tobato.fastdfs.domain.fdfs.StorePath; import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient; import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
...@@ -78,7 +79,7 @@ public class FileUploadController { ...@@ -78,7 +79,7 @@ public class FileUploadController {
* @return R * @return R
*/ */
@PostMapping("/files") @PostMapping("/files")
public R uploadFile(HttpServletRequest request) throws IOException { public R uploadFiles(HttpServletRequest request) throws IOException {
Map<String, String> resultMap = new HashMap<>(); Map<String, String> resultMap = new HashMap<>();
//将当前上下文初始化给 CommonsMultipartResolver (多部分解析器) //将当前上下文初始化给 CommonsMultipartResolver (多部分解析器)
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext()); CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
...@@ -112,4 +113,43 @@ public class FileUploadController { ...@@ -112,4 +113,43 @@ public class FileUploadController {
System.out.println("没有上传文件"); System.out.println("没有上传文件");
return new R(CommonConstants.FAIL, "没有上传文件"); return new R(CommonConstants.FAIL, "没有上传文件");
} }
@PostMapping("/file")
public UpFileResponse uploadFile(HttpServletRequest request) throws IOException {
Map<String, String> resultMap = new HashMap<>();
//将当前上下文初始化给 CommonsMultipartResolver (多部分解析器)
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//检查form中是否有enctype="multipart/form-data"
if (multipartResolver.isMultipart(request)) {
//将request变成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
List<String> supportFileTypes = Arrays.asList("TXT", "XLS", "XLSX", "DOC", "DOCX", "PPT", "PPTX", "RAR", "ZIP");
//获取multiRequest 中所有的文件名
Iterator iter = multiRequest.getFileNames();
while (iter.hasNext()) {
String name = iter.next().toString();
//一次遍历所有文件
MultipartFile file = multiRequest.getFile(name);
if (file != null) {
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
if (StringUtils.isBlank(extension)) {
throw new UploadFileException("上传文件类型不支持");
}
if (!supportFileTypes.contains(extension.toUpperCase())) {
throw new UploadFileException("上传文件类型不支持");
}
StorePath storePath = this.storageClient.uploadFile(
file.getInputStream(), file.getSize(), extension, null);
return new UpFileResponse(file.getOriginalFilename(),storePath.getFullPath());
// resultMap.put(name, storePath.getFullPath());
}
}
// return new R<>(resultMap);
}
// System.out.println("没有上传文件");
// return new R(CommonConstants.FAIL, "没有上传文件");
return null;
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论