|
@@ -1,5 +1,6 @@
|
|
|
package org.hsweb.web.controller.file;
|
|
|
|
|
|
+import org.hsweb.web.exception.NotFoundException;
|
|
|
import org.hsweb.web.logger.annotation.AccessLogger;
|
|
|
import org.hsweb.web.authorize.annotation.Authorize;
|
|
|
import org.hsweb.web.bean.po.resource.Resources;
|
|
@@ -81,7 +82,7 @@ public class FileController {
|
|
|
@AccessLogger("下载文件")
|
|
|
public ResponseMessage restDownLoad(@PathVariable("id") String id,
|
|
|
@PathVariable("name") String name,
|
|
|
- HttpServletResponse response, HttpServletRequest request) {
|
|
|
+ HttpServletResponse response, HttpServletRequest request) throws Exception {
|
|
|
return downLoad(id, name, response, request);
|
|
|
}
|
|
|
|
|
@@ -96,67 +97,61 @@ public class FileController {
|
|
|
@AccessLogger("下载文件")
|
|
|
public ResponseMessage downLoad(@PathVariable("id") String id,
|
|
|
@RequestParam(value = "name", required = false) String name,
|
|
|
- HttpServletResponse response, HttpServletRequest request) {
|
|
|
- try {
|
|
|
- Resources resources = resourcesService.selectByPk(id);
|
|
|
- if (resources == null || resources.getStatus() != 1) {
|
|
|
- response.setStatus(404);
|
|
|
- return new ResponseMessage(false, "资源不存在!", "404");
|
|
|
- } else {
|
|
|
- if (!"file".equals(resources.getType()))
|
|
|
- return new ResponseMessage(false, "该资源不是文件!", "400");
|
|
|
- String fileBasePath = configService.get("upload", "basePath", "/upload/").trim();
|
|
|
- File file = new File(fileBasePath.concat(resources.getPath().concat("/".concat(resources.getMd5()))));
|
|
|
- if (!file.canRead()) {
|
|
|
- response.setStatus(404);
|
|
|
- return new ResponseMessage(false, "资源不存在!", "404");
|
|
|
- }
|
|
|
- //获取contentType,默认application/octet-stream
|
|
|
- String contentType = mediaTypeMapper.get(resources.getSuffix().toLowerCase());
|
|
|
- if (contentType == null)
|
|
|
- contentType = "application/octet-stream";
|
|
|
- if (StringUtils.isNullOrEmpty(name))//未自定义文件名,则使用上传时的文件名
|
|
|
- name = resources.getName();
|
|
|
- if (!name.contains("."))//如果未指定文件拓展名,则追加默认的文件拓展名
|
|
|
- name = name.concat(".").concat(resources.getSuffix());
|
|
|
- //关键字剔除
|
|
|
- name = fileNameKeyWordPattern.matcher(name).replaceAll("");
|
|
|
- int skip = 0;
|
|
|
- long fSize = file.length();
|
|
|
- //尝试判断是否为断点下载
|
|
|
- try {
|
|
|
- //获取要继续下载的位置
|
|
|
- String Range = request.getHeader("Range").replaceAll("bytes=", "").replaceAll("-", "");
|
|
|
- skip = StringUtils.toInt(Range);
|
|
|
- } catch (Exception e) {
|
|
|
- }
|
|
|
+ HttpServletResponse response, HttpServletRequest request) throws Exception {
|
|
|
+ Resources resources = resourcesService.selectByPk(id);
|
|
|
+ if (resources == null || resources.getStatus() != 1) {
|
|
|
+ throw new NotFoundException("文件不存在");
|
|
|
+ } else {
|
|
|
+ if (!"file".equals(resources.getType()))
|
|
|
+ throw new NotFoundException("文件不存在");
|
|
|
+ String fileBasePath = configService.get("upload", "basePath", "/upload/").trim();
|
|
|
+ File file = new File(fileBasePath.concat(resources.getPath().concat("/".concat(resources.getMd5()))));
|
|
|
+ if (!file.canRead()) {
|
|
|
+ throw new NotFoundException("文件不存在");
|
|
|
+ }
|
|
|
+ //获取contentType,默认application/octet-stream
|
|
|
+ String contentType = mediaTypeMapper.get(resources.getSuffix().toLowerCase());
|
|
|
+ if (contentType == null)
|
|
|
+ contentType = "application/octet-stream";
|
|
|
+ if (StringUtils.isNullOrEmpty(name))//未自定义文件名,则使用上传时的文件名
|
|
|
+ name = resources.getName();
|
|
|
+ if (!name.contains("."))//如果未指定文件拓展名,则追加默认的文件拓展名
|
|
|
+ name = name.concat(".").concat(resources.getSuffix());
|
|
|
+ //关键字剔除
|
|
|
+ name = fileNameKeyWordPattern.matcher(name).replaceAll("");
|
|
|
+ int skip = 0;
|
|
|
+ long fSize = file.length();
|
|
|
+ //尝试判断是否为断点下载
|
|
|
+ try {
|
|
|
+ //获取要继续下载的位置
|
|
|
+ String Range = request.getHeader("Range").replaceAll("bytes=", "").replaceAll("-", "");
|
|
|
+ skip = StringUtils.toInt(Range);
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
|
|
|
- response.setContentLength((int) fSize);//文件大小
|
|
|
- response.setContentType(contentType);
|
|
|
- response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(name, "utf-8"));
|
|
|
- //try with resource
|
|
|
- try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));
|
|
|
- BufferedOutputStream stream = new BufferedOutputStream(response.getOutputStream())) {
|
|
|
- //断点下载
|
|
|
- if (skip > 0) {
|
|
|
- inputStream.skip(skip);
|
|
|
- response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
|
|
|
- String contentRange = new StringBuffer("bytes ").append(skip).append("-").append(fSize - 1).append("/").append(fSize).toString();
|
|
|
- response.setHeader("Content-Range", contentRange);
|
|
|
- }
|
|
|
- byte b[] = new byte[2048 * 10];
|
|
|
- while ((inputStream.read(b)) != -1) {
|
|
|
- stream.write(b);
|
|
|
- }
|
|
|
- stream.flush();
|
|
|
- } catch (Exception e) {
|
|
|
- logger.debug(String.format("download file error%s", e.getMessage()));
|
|
|
- throw e;
|
|
|
+ response.setContentLength((int) fSize);//文件大小
|
|
|
+ response.setContentType(contentType);
|
|
|
+ response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(name, "utf-8"));
|
|
|
+ //try with resource
|
|
|
+ try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));
|
|
|
+ BufferedOutputStream stream = new BufferedOutputStream(response.getOutputStream())) {
|
|
|
+ //断点下载
|
|
|
+ if (skip > 0) {
|
|
|
+ inputStream.skip(skip);
|
|
|
+ response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
|
|
|
+ String contentRange = new StringBuffer("bytes ").append(skip).append("-").append(fSize - 1).append("/").append(fSize).toString();
|
|
|
+ response.setHeader("Content-Range", contentRange);
|
|
|
+ }
|
|
|
+ byte b[] = new byte[2048 * 10];
|
|
|
+ while ((inputStream.read(b)) != -1) {
|
|
|
+ stream.write(b);
|
|
|
}
|
|
|
- return null;
|
|
|
+ stream.flush();
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.debug(String.format("download file error%s", e.getMessage()));
|
|
|
+ throw e;
|
|
|
}
|
|
|
- } catch (Exception e) {
|
|
|
- return new ResponseMessage(false, e);
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -169,7 +164,7 @@ public class FileController {
|
|
|
*/
|
|
|
@RequestMapping(value = "/upload", method = RequestMethod.POST)
|
|
|
@AccessLogger("上传文件")
|
|
|
- public Object upload(@RequestParam("file") CommonsMultipartFile[] files) {
|
|
|
+ public Object upload(@RequestParam("file") CommonsMultipartFile[] files) throws Exception {
|
|
|
if (logger.isInfoEnabled())
|
|
|
logger.info(String.format("start upload , file number:%s", files.length));
|
|
|
List<Resources> resourcesList = new LinkedList<>();
|
|
@@ -178,16 +173,12 @@ public class FileController {
|
|
|
if (!file.isEmpty()) {
|
|
|
if (logger.isInfoEnabled())
|
|
|
logger.info(String.format("start write file:%s", file.getOriginalFilename()));
|
|
|
- try {
|
|
|
- String fileName = files[i].getOriginalFilename();
|
|
|
- Resources resources = fileService.saveFile(files[i].getFileItem().getInputStream(), fileName);
|
|
|
- resourcesList.add(resources);
|
|
|
- } catch (Exception e) {
|
|
|
- return new ResponseMessage(false, e);
|
|
|
- }
|
|
|
+ String fileName = files[i].getOriginalFilename();
|
|
|
+ Resources resources = fileService.saveFile(files[i].getFileItem().getInputStream(), fileName);
|
|
|
+ resourcesList.add(resources);
|
|
|
}
|
|
|
}//响应上传成功的资源信息
|
|
|
- return new ResponseMessage(true, resourcesList)
|
|
|
+ return ResponseMessage.ok(resourcesList)
|
|
|
.include(Resources.class, "u_id", "name", "md5");
|
|
|
}
|
|
|
}
|