|
@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
|
|
import org.hswebframework.expands.compress.Compress;
|
|
import org.hswebframework.expands.compress.Compress;
|
|
import org.hswebframework.expands.compress.zip.ZIPWriter;
|
|
import org.hswebframework.expands.compress.zip.ZIPWriter;
|
|
import org.hswebframework.utils.StringUtils;
|
|
import org.hswebframework.utils.StringUtils;
|
|
|
|
+import org.hswebframework.web.BusinessException;
|
|
import org.hswebframework.web.NotFoundException;
|
|
import org.hswebframework.web.NotFoundException;
|
|
import org.hswebframework.web.authorization.Authentication;
|
|
import org.hswebframework.web.authorization.Authentication;
|
|
import org.hswebframework.web.authorization.annotation.Authorize;
|
|
import org.hswebframework.web.authorization.annotation.Authorize;
|
|
@@ -30,11 +31,15 @@ import java.util.LinkedList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
import java.util.regex.Pattern;
|
|
import java.util.regex.Pattern;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+import java.util.stream.Stream;
|
|
|
|
|
|
/**
|
|
/**
|
|
- * TODO 完成注释
|
|
|
|
|
|
+ * 文件操作控制器,提供文件上传下载等操作
|
|
*
|
|
*
|
|
* @author zhouhao
|
|
* @author zhouhao
|
|
|
|
+ * @see FileService
|
|
|
|
+ * @since 3.0
|
|
*/
|
|
*/
|
|
@RestController
|
|
@RestController
|
|
@RequestMapping("${hsweb.web.mappings.file:file}")
|
|
@RequestMapping("${hsweb.web.mappings.file:file}")
|
|
@@ -203,30 +208,48 @@ public class FileController {
|
|
* 上传文件,支持多文件上传.获取到文件流后,调用{@link org.hswebframework.web.service.file.FileService#saveFile(InputStream, String, String, String)}进行文件保存
|
|
* 上传文件,支持多文件上传.获取到文件流后,调用{@link org.hswebframework.web.service.file.FileService#saveFile(InputStream, String, String, String)}进行文件保存
|
|
* 上传成功后,将返回资源信息如:[{"id":"fileId","name":"fileName","md5":"md5"}]
|
|
* 上传成功后,将返回资源信息如:[{"id":"fileId","name":"fileName","md5":"md5"}]
|
|
*
|
|
*
|
|
- * @param files 文件列表
|
|
|
|
|
|
+ * @param files 上传的文件
|
|
* @return 文件上传结果.
|
|
* @return 文件上传结果.
|
|
- * @throws IOException 保存文件错误
|
|
|
|
|
|
+ */
|
|
|
|
+ @PostMapping(value = "/upload-multi")
|
|
|
|
+ @AccessLogger("上传多个文件")
|
|
|
|
+ @Authorize(action = "upload")
|
|
|
|
+ public ResponseMessage<List<FileInfoEntity>> upload(@RequestParam("files") MultipartFile[] files) {
|
|
|
|
+ return ResponseMessage.ok(Stream.of(files)
|
|
|
|
+ .map(this::upload)
|
|
|
|
+ .map(ResponseMessage::getResult)
|
|
|
|
+ .collect(Collectors.toList()))
|
|
|
|
+ .include(FileInfoEntity.class, FileInfoEntity.id, FileInfoEntity.name, FileInfoEntity.md5);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 上传单个文件
|
|
|
|
+ *
|
|
|
|
+ * @param file 上传文件
|
|
|
|
+ * @return 上传结果
|
|
*/
|
|
*/
|
|
@PostMapping(value = "/upload")
|
|
@PostMapping(value = "/upload")
|
|
@AccessLogger("上传文件")
|
|
@AccessLogger("上传文件")
|
|
@Authorize(action = "upload")
|
|
@Authorize(action = "upload")
|
|
- public ResponseMessage<List<FileInfoEntity>> upload(MultipartFile[] files) throws IOException {
|
|
|
|
- if (logger.isInfoEnabled())
|
|
|
|
- logger.info(String.format("start upload , file number:%s", files.length));
|
|
|
|
- List<FileInfoEntity> resourcesList = new LinkedList<>();
|
|
|
|
- Authentication authentication = Authentication.current().orElseThrow(null);
|
|
|
|
|
|
+ public ResponseMessage<FileInfoEntity> upload(@RequestParam("file") MultipartFile file) {
|
|
|
|
+ List<FileInfoEntity> fileInfoList = new LinkedList<>();
|
|
|
|
+ Authentication authentication = Authentication.current().orElse(null);
|
|
String creator = authentication == null ? null : authentication.getUser().getId();
|
|
String creator = authentication == null ? null : authentication.getUser().getId();
|
|
- for (MultipartFile file : files) {
|
|
|
|
- if (!file.isEmpty()) {
|
|
|
|
- if (logger.isInfoEnabled())
|
|
|
|
- logger.info("start write file:{}", file.getOriginalFilename());
|
|
|
|
- String fileName = file.getOriginalFilename();
|
|
|
|
- FileInfoEntity resources = fileService.saveFile(file.getInputStream(), fileName, file.getContentType(), creator);
|
|
|
|
- resourcesList.add(resources);
|
|
|
|
- }
|
|
|
|
- }//响应上传成功的资源信息
|
|
|
|
- return ResponseMessage.ok(resourcesList)
|
|
|
|
- .include(FileInfoEntity.class, FileInfoEntity.id, FileInfoEntity.name, FileInfoEntity.id);
|
|
|
|
|
|
+ if (file.isEmpty()) {
|
|
|
|
+ return ResponseMessage.ok();
|
|
|
|
+ }
|
|
|
|
+ if (logger.isInfoEnabled())
|
|
|
|
+ logger.info("start write file:{}", file.getOriginalFilename());
|
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
|
+ FileInfoEntity fileInfo;
|
|
|
|
+ try {
|
|
|
|
+ fileInfo = fileService.saveFile(file.getInputStream(), fileName, file.getContentType(), creator);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new BusinessException("save file error", e);
|
|
|
|
+ }
|
|
|
|
+ fileInfoList.add(fileInfo);
|
|
|
|
+ return ResponseMessage.ok(fileInfo)
|
|
|
|
+ .include(FileInfoEntity.class, FileInfoEntity.id, FileInfoEntity.name, FileInfoEntity.md5);
|
|
}
|
|
}
|
|
|
|
|
|
@PostMapping(value = "/upload-static")
|
|
@PostMapping(value = "/upload-static")
|
|
@@ -236,4 +259,11 @@ public class FileController {
|
|
if (file.isEmpty()) return ResponseMessage.ok();
|
|
if (file.isEmpty()) return ResponseMessage.ok();
|
|
return ResponseMessage.ok(fileService.saveStaticFile(file.getInputStream(), file.getOriginalFilename()));
|
|
return ResponseMessage.ok(fileService.saveStaticFile(file.getInputStream(), file.getOriginalFilename()));
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ @GetMapping(value = "/md5/{md5}")
|
|
|
|
+ @AccessLogger("根据MD5获取文件信息")
|
|
|
|
+ public ResponseMessage<FileInfoEntity> uploadStatic(@PathVariable String md5) throws IOException {
|
|
|
|
+ return ResponseMessage
|
|
|
|
+ .ok(fileInfoService.selectByMd5(md5));
|
|
|
|
+ }
|
|
}
|
|
}
|