|
|
@@ -1,19 +1,13 @@
|
|
|
package com.railway.web.controller.common;
|
|
|
|
|
|
-import com.railway.common.config.RailwayConfig;
|
|
|
-import com.railway.common.constant.Constants;
|
|
|
import com.railway.common.core.domain.AjaxResult;
|
|
|
import com.railway.common.utils.StringUtils;
|
|
|
-import com.railway.common.utils.file.FileUploadUtils;
|
|
|
import com.railway.common.utils.file.FileUtils;
|
|
|
-import com.railway.framework.config.ServerConfig;
|
|
|
+import com.railway.system.service.ISysFileService;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.http.MediaType;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
@@ -29,39 +23,12 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
@RestController
|
|
|
public class CommonController {
|
|
|
|
|
|
- private final ServerConfig serverConfig;
|
|
|
+ private final ISysFileService sysFileService;
|
|
|
|
|
|
private static final String FILE_DELIMETER = ",";
|
|
|
|
|
|
- public CommonController(ServerConfig serverConfig) {
|
|
|
- this.serverConfig = serverConfig;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 通用下载请求
|
|
|
- *
|
|
|
- * @param fileName 文件名称
|
|
|
- * @param delete 是否删除
|
|
|
- */
|
|
|
- @GetMapping("common/download")
|
|
|
- public void fileDownload(String fileName, Boolean delete, HttpServletResponse response) {
|
|
|
- try {
|
|
|
- if (!FileUtils.checkAllowDownload(fileName)) {
|
|
|
- throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
|
|
|
- }
|
|
|
- String realFileName =
|
|
|
- System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
|
|
|
- String filePath = RailwayConfig.getDownloadPath() + fileName;
|
|
|
-
|
|
|
- response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
|
|
- FileUtils.setAttachmentResponseHeader(response, realFileName);
|
|
|
- FileUtils.writeBytes(filePath, response.getOutputStream());
|
|
|
- if (delete) {
|
|
|
- FileUtils.deleteFile(filePath);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("下载文件失败", e);
|
|
|
- }
|
|
|
+ public CommonController(ISysFileService sysFileService) {
|
|
|
+ this.sysFileService = sysFileService;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -71,11 +38,10 @@ public class CommonController {
|
|
|
@ResponseBody
|
|
|
public AjaxResult uploadFile(MultipartFile file) {
|
|
|
try {
|
|
|
- // 上传文件路径
|
|
|
- String filePath = RailwayConfig.getUploadPath();
|
|
|
- // 上传并返回新文件名称
|
|
|
- String fileName = FileUploadUtils.upload(filePath, file);
|
|
|
- String url = serverConfig.getUrl() + fileName;
|
|
|
+ // 上传并返回访问地址
|
|
|
+ String url = sysFileService.uploadFile(file);
|
|
|
+ // 新文件名称
|
|
|
+ String fileName = FileUtils.getName(url);
|
|
|
AjaxResult ajax = AjaxResult.success();
|
|
|
ajax.put("fileName", fileName);
|
|
|
ajax.put("url", url);
|
|
|
@@ -92,14 +58,13 @@ public class CommonController {
|
|
|
@ResponseBody
|
|
|
public AjaxResult uploadFiles(List<MultipartFile> files) {
|
|
|
try {
|
|
|
- // 上传文件路径
|
|
|
- String filePath = RailwayConfig.getUploadPath();
|
|
|
List<String> fileNames = new ArrayList<>();
|
|
|
List<String> urls = new ArrayList<>();
|
|
|
for (MultipartFile file : files) {
|
|
|
- // 上传并返回新文件名称
|
|
|
- String fileName = FileUploadUtils.upload(filePath, file);
|
|
|
- String url = serverConfig.getUrl() + fileName;
|
|
|
+ // 上传并返回访问地址
|
|
|
+ String url = sysFileService.uploadFile(file);
|
|
|
+ // 新文件名称
|
|
|
+ String fileName = FileUtils.getName(url);
|
|
|
fileNames.add(fileName);
|
|
|
urls.add(url);
|
|
|
}
|
|
|
@@ -112,28 +77,4 @@ public class CommonController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 本地资源通用下载
|
|
|
- */
|
|
|
- @GetMapping("/common/download/resource")
|
|
|
- public void resourceDownload(String resource,
|
|
|
- HttpServletResponse response) {
|
|
|
- try {
|
|
|
- if (!FileUtils.checkAllowDownload(resource)) {
|
|
|
- throw new Exception(StringUtils.format("资源文件({})非法,不允许下载。 ", resource));
|
|
|
- }
|
|
|
- // 本地资源路径
|
|
|
- String localPath = RailwayConfig.getProfile();
|
|
|
- // 数据库资源地址
|
|
|
- String downloadPath =
|
|
|
- localPath + StringUtils.substringAfter(resource, Constants.RESOURCE_PREFIX);
|
|
|
- // 下载名称
|
|
|
- String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
|
|
|
- response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
|
|
- FileUtils.setAttachmentResponseHeader(response, downloadName);
|
|
|
- FileUtils.writeBytes(downloadPath, response.getOutputStream());
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("下载文件失败", e);
|
|
|
- }
|
|
|
- }
|
|
|
}
|