|
@@ -1,13 +1,16 @@
|
|
|
package org.hswebframework.web.controller.file;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
+import org.apache.commons.fileupload.ParameterParser;
|
|
|
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.WebUtil;
|
|
|
import org.hswebframework.web.authorization.Authentication;
|
|
|
import org.hswebframework.web.authorization.annotation.Authorize;
|
|
|
+import org.hswebframework.web.commons.entity.DataStatus;
|
|
|
import org.hswebframework.web.controller.message.ResponseMessage;
|
|
|
import org.hswebframework.web.entity.file.FileInfoEntity;
|
|
|
import org.hswebframework.web.logging.AccessLogger;
|
|
@@ -16,7 +19,6 @@ import org.hswebframework.web.service.file.FileService;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
@@ -26,14 +28,18 @@ import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.util.LinkedList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Optional;
|
|
|
import java.util.regex.Pattern;
|
|
|
import java.util.stream.Collectors;
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
+import static java.util.Optional.ofNullable;
|
|
|
+
|
|
|
/**
|
|
|
* 文件操作控制器,提供文件上传下载等操作
|
|
|
*
|
|
@@ -126,6 +132,7 @@ public class FileController {
|
|
|
@PathVariable("name") String name,
|
|
|
HttpServletResponse response,
|
|
|
HttpServletRequest request) throws IOException {
|
|
|
+
|
|
|
downLoad(id, name, response, request);
|
|
|
}
|
|
|
|
|
@@ -149,7 +156,7 @@ public class FileController {
|
|
|
HttpServletResponse response, HttpServletRequest request)
|
|
|
throws IOException {
|
|
|
FileInfoEntity fileInfo = fileInfoService.selectByIdOrMd5(idOrMd5);
|
|
|
- if (fileInfo == null || fileInfo.getStatus() != 1) {
|
|
|
+ if (fileInfo == null || !DataStatus.STATUS_ENABLED.equals(fileInfo.getStatus())) {
|
|
|
throw new NotFoundException("文件不存在");
|
|
|
}
|
|
|
String fileName = fileInfo.getName();
|
|
@@ -174,9 +181,9 @@ public class FileController {
|
|
|
//尝试判断是否为断点下载
|
|
|
try {
|
|
|
//获取要继续下载的位置
|
|
|
- String Range = request.getHeader("Range").replaceAll("bytes=", "").replaceAll("-", "");
|
|
|
+ String Range = request.getHeader("Range").replace("bytes=", "").replace("-", "");
|
|
|
skip = StringUtils.toInt(Range);
|
|
|
- } catch (Exception e) {
|
|
|
+ } catch (Exception ignore) {
|
|
|
}
|
|
|
response.setContentLength((int) fSize);//文件大小
|
|
|
response.setContentType(contentType);
|
|
@@ -205,7 +212,12 @@ public class FileController {
|
|
|
.map(this::upload)
|
|
|
.map(ResponseMessage::getResult)
|
|
|
.collect(Collectors.toList()))
|
|
|
- .include(FileInfoEntity.class, FileInfoEntity.id, FileInfoEntity.name, FileInfoEntity.md5);
|
|
|
+ .include(FileInfoEntity.class,
|
|
|
+ FileInfoEntity.id,
|
|
|
+ FileInfoEntity.name,
|
|
|
+ FileInfoEntity.md5,
|
|
|
+ FileInfoEntity.size,
|
|
|
+ FileInfoEntity.type);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -224,9 +236,21 @@ public class FileController {
|
|
|
if (file.isEmpty()) {
|
|
|
return ResponseMessage.ok();
|
|
|
}
|
|
|
- if (logger.isInfoEnabled())
|
|
|
- logger.info("start write file:{}", file.getOriginalFilename());
|
|
|
String fileName = file.getOriginalFilename();
|
|
|
+ String contentType = Optional.ofNullable(WebUtil.getHttpServletRequest())
|
|
|
+ .orElseThrow(UnsupportedOperationException::new)
|
|
|
+ .getContentType();
|
|
|
+ ParameterParser parser = new ParameterParser();
|
|
|
+ Map<String, String> params = parser.parse(contentType, ';');
|
|
|
+ if (params.get("charset") == null) {
|
|
|
+ try {
|
|
|
+ fileName = new String(file.getOriginalFilename().getBytes("ISO-8859-1"), "utf-8");
|
|
|
+ } catch (UnsupportedEncodingException ignore) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (logger.isInfoEnabled())
|
|
|
+ logger.info("start write file:{}", fileName);
|
|
|
+
|
|
|
FileInfoEntity fileInfo;
|
|
|
try {
|
|
|
fileInfo = fileService.saveFile(file.getInputStream(), fileName, file.getContentType(), creator);
|
|
@@ -235,13 +259,17 @@ public class FileController {
|
|
|
}
|
|
|
fileInfoList.add(fileInfo);
|
|
|
return ResponseMessage.ok(fileInfo)
|
|
|
- .include(FileInfoEntity.class, FileInfoEntity.id, FileInfoEntity.name, FileInfoEntity.md5);
|
|
|
+ .include(FileInfoEntity.class, FileInfoEntity.id,
|
|
|
+ FileInfoEntity.name,
|
|
|
+ FileInfoEntity.md5,
|
|
|
+ FileInfoEntity.size,
|
|
|
+ FileInfoEntity.type);
|
|
|
}
|
|
|
|
|
|
@PostMapping(value = "/upload-static")
|
|
|
@AccessLogger("上传静态文件")
|
|
|
@Authorize(action = "static")
|
|
|
- public ResponseMessage<String> uploadStatic(MultipartFile file) throws IOException {
|
|
|
+ public ResponseMessage<String> uploadStatic(@RequestParam("file") MultipartFile file) throws IOException {
|
|
|
if (file.isEmpty()) return ResponseMessage.ok();
|
|
|
return ResponseMessage.ok(fileService.saveStaticFile(file.getInputStream(), file.getOriginalFilename()));
|
|
|
}
|
|
@@ -249,7 +277,8 @@ public class FileController {
|
|
|
@GetMapping(value = "/md5/{md5}")
|
|
|
@AccessLogger("根据MD5获取文件信息")
|
|
|
public ResponseMessage<FileInfoEntity> uploadStatic(@PathVariable String md5) throws IOException {
|
|
|
- return ResponseMessage
|
|
|
- .ok(fileInfoService.selectByMd5(md5));
|
|
|
+ return ofNullable(fileInfoService.selectByMd5(md5))
|
|
|
+ .map(ResponseMessage::ok)
|
|
|
+ .orElseThrow(() -> new NotFoundException("file not found"));
|
|
|
}
|
|
|
}
|