提交 ae70522d 编写于 作者: 水库浪子

优化文件上传接口,文件上传同时支持本地上传与腾讯云上传

上级 3c98c717
......@@ -124,7 +124,7 @@ public class CommonController {
= fileService.upload(file);
AjaxResult ajax = AjaxResult.success();
ajax.put("fileName", sysFile.getFileName());
ajax.put("url", sysFile.getImgUrlPrefix()+sysFile.getFileName());
ajax.put("url", sysFile.getImgUrlPrefix());
return ajax;
} catch (Exception e) {
return AjaxResult.error(e.getMessage());
......@@ -135,7 +135,7 @@ public class CommonController {
/**
* 本地图片下载
*/
@GetMapping("/findFile")
@GetMapping("/common/findFile")
public void findFile(HttpServletResponse response,String fileName){
fileService.findFile(fileName,response);
}
......
......@@ -10,5 +10,5 @@ public class FileConfig {
/**腾讯云存储相关配置*/
private CosConfig cos;
/**文件前缀*/
private String imgUrlPrefix;
private String imgUrlPrefix="/common/findFile?fileName=";
}
package com.linkwechat.common.utils;
import javax.activation.MimetypesFileTypeMap;
import java.io.File;
/**
* @description:
* @author: HaoN
* @create: 2021-06-08 15:27
**/
public class ImageCheckUtils {
private static MimetypesFileTypeMap mtftp;
static {
mtftp = new MimetypesFileTypeMap();
/* 不添加下面的类型会造成误判 详见:http://stackoverflow.com/questions/4855627/java-mimetypesfiletypemap-always-returning-application-octet-stream-on-android-e*/
mtftp.addMimeTypes("image png tif jpg jpeg bmp");
}
public static boolean isImage(File file){
String mimetype= mtftp.getContentType(file);
String type = mimetype.split("/")[0];
return type.equals("image");
}
}
......@@ -275,9 +275,11 @@ public class FileUploadUtils {
String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
String pathFileName;
if(org.apache.commons.lang3.StringUtils.isBlank(currentDir)){
pathFileName = Constants.RESOURCE_PREFIX + "/" +fileName;
// pathFileName = Constants.RESOURCE_PREFIX + "/" +fileName;
pathFileName = "/" +fileName;
}else {
pathFileName = Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
// pathFileName = Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
pathFileName = "/" + currentDir + "/" + fileName;
}
return pathFileName;
}
......
......@@ -203,4 +203,7 @@ public class FileUtils extends org.apache.commons.io.FileUtils
}
return filename;
}
}
......@@ -2,13 +2,16 @@ package com.linkwechat.framework.web.service;
import com.linkwechat.common.config.RuoYiConfig;
import com.linkwechat.common.utils.ImageCheckUtils;
import com.linkwechat.common.utils.OsUtils;
import com.linkwechat.common.utils.file.FileUploadUtils;
import com.linkwechat.framework.web.domain.server.Sys;
import com.linkwechat.framework.web.domain.server.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
......@@ -54,7 +57,7 @@ public class FileService {
}
return SysFile.builder()
.fileName(fileName)
.imgUrlPrefix(ruoYiConfig.getFile().getImgUrlPrefix())
.imgUrlPrefix(ruoYiConfig.getFile().getImgUrlPrefix()+fileName)
.build();
}
......@@ -62,13 +65,28 @@ public class FileService {
public void findFile(String fileName, HttpServletResponse rp){
String fileDownUrl="";
// if(ruoYiConfig.getFile().isStartCosUpload()) {//开启云上传
// fileDownUrl= ruoYiConfig.getFile().getImgUrlPrefix();
// }else{
if(ruoYiConfig.getFile().isStartCosUpload()) {//开启云上传
fileDownUrl= ruoYiConfig.getFile().getImgUrlPrefix();
}else{
fileDownUrl=OsUtils.isWindows()?WINDOWSFILEPATH+fileName:LINUXFILEPATH+fileName;
// }
}
File file=new File(fileDownUrl);
if (file.exists()) {
if(ImageCheckUtils.isImage(file)){
rp.setContentType("image/png");
}else{
rp.setCharacterEncoding("utf-8");
rp.setContentType("multipart/form-data");
rp.setHeader("Content-Disposition", "attachment;fileName=" + fileName.substring(fileName.lastIndexOf("/")+1));
}
FileInputStream fis = null;
OutputStream os = null;
try {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册