增加单文件上传,根据md5获取文件信息

This commit is contained in:
zhouhao
2017-07-25 22:50:16 +08:00
parent 56379d8b68
commit 874db92537

View File

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
import org.hswebframework.expands.compress.Compress;
import org.hswebframework.expands.compress.zip.ZIPWriter;
import org.hswebframework.utils.StringUtils;
import org.hswebframework.web.BusinessException;
import org.hswebframework.web.NotFoundException;
import org.hswebframework.web.authorization.Authentication;
import org.hswebframework.web.authorization.annotation.Authorize;
@@ -30,11 +31,15 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* TODO 完成注释
* 文件操作控制器,提供文件上传下载等操作
*
* @author zhouhao
* @see FileService
* @since 3.0
*/
@RestController
@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)}进行文件保存
* 上传成功后,将返回资源信息如:[{"id":"fileId","name":"fileName","md5":"md5"}]
*
* @param files 文件列表
* @param files 上传的文件
* @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")
@AccessLogger("上传文件")
@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();
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")
@@ -236,4 +259,11 @@ public class FileController {
if (file.isEmpty()) return ResponseMessage.ok();
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));
}
}