!34 代码合并

Merge pull request !34 from MAC/develop
This commit is contained in:
MAC 2021-04-15 23:59:52 +08:00 committed by Gitee
commit da420808eb
15 changed files with 450 additions and 84 deletions

View File

@ -20,11 +20,11 @@
<version>7.4.0</version> <version>7.4.0</version>
</dependency> </dependency>
<dependency> <!-- <dependency>-->
<groupId>commons-fileupload</groupId> <!-- <groupId>commons-fileupload</groupId>-->
<artifactId>commons-fileupload</artifactId> <!-- <artifactId>commons-fileupload</artifactId>-->
<version>1.3.3</version> <!-- <version>1.3.3</version>-->
</dependency> <!-- </dependency>-->
<!--图片压缩--> <!--图片压缩-->
<dependency> <dependency>

View File

@ -256,16 +256,37 @@ public class FileOperation {
} }
targetFile.createNewFile(); targetFile.createNewFile();
// 将压缩文件内容写入到这个文件中 // 将压缩文件内容写入到这个文件中
InputStream is = zipFile.getInputStream(entry); InputStream is = null;
FileOutputStream fos = new FileOutputStream(targetFile); FileOutputStream fos = null;
int len; try {
byte[] buf = new byte[2048]; is = zipFile.getInputStream(entry);
while ((len = is.read(buf)) != -1) { fos = new FileOutputStream(targetFile);
fos.write(buf, 0, len); int len;
byte[] buf = new byte[2048];
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
} catch (Exception e) {
// 关流顺序先打开的后关闭
if (fos != null) {
try {
fos.close();
} catch (Exception e1) {
log.error("关闭流失败:" + e1);
}
}
if (is != null) {
try {
is.close();
} catch (Exception e2) {
log.error("关闭流失败:" + e2);
}
}
} }
// 关流顺序先打开的后关闭
fos.close();
is.close();
} }
} }
} catch (Exception e) { } catch (Exception e) {
@ -297,7 +318,7 @@ public class FileOperation {
/** /**
* 解压rar * 解压rar
* *
* @param sourceFile 需要解压的文件 * @param sourceFile 需要解压的文件
* @param destDirPath 目的路径 * @param destDirPath 目的路径
* @throws Exception * @throws Exception
*/ */

View File

@ -3,7 +3,9 @@ package com.qiwenshare.common.operation.download;
import com.qiwenshare.common.operation.download.domain.DownloadFile; import com.qiwenshare.common.operation.download.domain.DownloadFile;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
public abstract class Downloader { public abstract class Downloader {
public abstract void download(HttpServletResponse httpServletResponse, DownloadFile uploadFile); public abstract void download(HttpServletResponse httpServletResponse, DownloadFile uploadFile);
public abstract InputStream getInputStream(DownloadFile downloadFile);
} }

View File

@ -53,6 +53,15 @@ public class AliyunOSSDownloader extends Downloader {
ossClient.shutdown(); ossClient.shutdown();
} }
@Override
public InputStream getInputStream(DownloadFile downloadFile) {
OSS ossClient = createOSSClient(qiwenFileConfig.getAliyun().getOss());
OSSObject ossObject = ossClient.getObject(qiwenFileConfig.getAliyun().getOss().getBucketName(),
downloadFile.getFileUrl().substring(1));
InputStream inputStream = ossObject.getObjectContent();
return inputStream;
}
public OSS createOSSClient(AliyunOSS aliyunOSS) { public OSS createOSSClient(AliyunOSS aliyunOSS) {
String endpoint = aliyunOSS.getEndpoint(); String endpoint = aliyunOSS.getEndpoint();
String accessKeyId = aliyunOSS.getAccessKeyId(); String accessKeyId = aliyunOSS.getAccessKeyId();

View File

@ -4,12 +4,15 @@ import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient; import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.qiwenshare.common.operation.download.domain.DownloadFile; import com.qiwenshare.common.operation.download.domain.DownloadFile;
import com.qiwenshare.common.operation.download.Downloader; import com.qiwenshare.common.operation.download.Downloader;
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.servlet.ServletOutputStream; import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
@Component @Component
public class FastDFSDownloader extends Downloader { public class FastDFSDownloader extends Downloader {
@ -22,9 +25,6 @@ public class FastDFSDownloader extends Downloader {
DownloadByteArray downloadByteArray = new DownloadByteArray(); DownloadByteArray downloadByteArray = new DownloadByteArray();
byte[] bytes = fastFileStorageClient.downloadFile(group, path, downloadByteArray); byte[] bytes = fastFileStorageClient.downloadFile(group, path, downloadByteArray);
// // 这里只是为了整合fastdfs所以写死了文件格式需要在上传的时候保存文件名下载的时候使用对应的格式
// response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("sb.xlsx", "UTF-8"));
// response.setCharacterEncoding("UTF-8");
ServletOutputStream outputStream = null; ServletOutputStream outputStream = null;
try { try {
outputStream = httpServletResponse.getOutputStream(); outputStream = httpServletResponse.getOutputStream();
@ -42,4 +42,14 @@ public class FastDFSDownloader extends Downloader {
} }
} }
} }
@Override
public InputStream getInputStream(DownloadFile downloadFile) {
String group = downloadFile.getFileUrl().substring(0, downloadFile.getFileUrl().indexOf("/"));
String path = downloadFile.getFileUrl().substring(downloadFile.getFileUrl().indexOf("/") + 1);
DownloadByteArray downloadByteArray = new DownloadByteArray();
byte[] bytes = fastFileStorageClient.downloadFile(group, path, downloadByteArray);
InputStream inputStream = new ByteArrayInputStream(bytes);
return inputStream;
}
} }

View File

@ -45,4 +45,18 @@ public class LocalStorageDownloader extends Downloader {
} }
} }
} }
@Override
public InputStream getInputStream(DownloadFile downloadFile) {
//设置文件路径
File file = FileOperation.newFile(PathUtil.getStaticPath() + downloadFile.getFileUrl());
InputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return inputStream;
}
} }

View File

@ -12,7 +12,8 @@ import com.qiwenshare.common.util.FileUtil;
import com.qiwenshare.common.util.PathUtil; import com.qiwenshare.common.util.PathUtil;
import lombok.Data; import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.servlet.ServletFileUpload; //import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest; import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

View File

@ -8,8 +8,10 @@ import com.qiwenshare.common.operation.upload.Uploader;
import com.qiwenshare.common.util.FileUtil; import com.qiwenshare.common.util.FileUtil;
import com.qiwenshare.common.util.PathUtil; import com.qiwenshare.common.util.PathUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.disk.DiskFileItemFactory; //import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload; //import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest; import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

View File

@ -8,8 +8,9 @@ import com.qiwenshare.common.operation.upload.Uploader;
import com.qiwenshare.common.util.FileUtil; import com.qiwenshare.common.util.FileUtil;
import com.qiwenshare.common.util.PathUtil; import com.qiwenshare.common.util.PathUtil;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.fileupload.servlet.ServletFileUpload; //import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;

View File

@ -0,0 +1,202 @@
package com.qiwenshare.common.util;
import java.util.HashMap;
import java.util.Map;
public class MimeUtils {
public static String getMime(String suffix){
return map.get(suffix);
}
private static Map<String,String> map=new HashMap<String,String>();
static{
map.put("323","text/h323");
map.put("acx","application/internet-property-stream");
map.put("ai","application/postscript");
map.put("aif","audio/x-aiff");
map.put("aifc","audio/x-aiff");
map.put("aiff","audio/x-aiff");
map.put("asf","video/x-ms-asf");
map.put("asr","video/x-ms-asf");
map.put("asx","video/x-ms-asf");
map.put("au","audio/basic");
map.put("avi","video/x-msvideo");
map.put("axs","application/olescript");
map.put("bas","text/plain");
map.put("bcpio","application/x-bcpio");
map.put("bin","application/octet-stream");
map.put("bmp","image/bmp");
map.put("c","text/plain");
map.put("cat","application/vnd.ms-pkiseccat");
map.put("cdf","application/x-cdf");
map.put("cer","application/x-x509-ca-cert");
map.put("class","application/octet-stream");
map.put("clp","application/x-msclip");
map.put("cmx","image/x-cmx");
map.put("cod","image/cis-cod");
map.put("cpio","application/x-cpio");
map.put("crd","application/x-mscardfile");
map.put("crl","application/pkix-crl");
map.put("crt","application/x-x509-ca-cert");
map.put("csh","application/x-csh");
map.put("css","text/css");
map.put("dcr","application/x-director");
map.put("der","application/x-x509-ca-cert");
map.put("dir","application/x-director");
map.put("dll","application/x-msdownload");
map.put("dms","application/octet-stream");
map.put("doc","application/msword");
map.put("dot","application/msword");
map.put("dvi","application/x-dvi");
map.put("dxr","application/x-director");
map.put("eps","application/postscript");
map.put("etx","text/x-setext");
map.put("evy","application/envoy");
map.put("exe","application/octet-stream");
map.put("fif","application/fractals");
map.put("flr","x-world/x-vrml");
map.put("gif","image/gif");
map.put("gtar","application/x-gtar");
map.put("gz","application/x-gzip");
map.put("h","text/plain");
map.put("hdf","application/x-hdf");
map.put("hlp","application/winhlp");
map.put("hqx","application/mac-binhex40");
map.put("hta","application/hta");
map.put("htc","text/x-component");
map.put("htm","text/plain");
map.put("html","text/plain");
map.put("htt","text/webviewhtml");
map.put("ico","image/x-icon");
map.put("ief","image/ief");
map.put("iii","application/x-iphone");
map.put("ins","application/x-internet-signup");
map.put("isp","application/x-internet-signup");
map.put("jfif","image/pipeg");
map.put("jpe","image/jpeg");
map.put("jpeg","image/jpeg");
map.put("jpg","image/jpeg");
map.put("js","application/x-javascript");
map.put("latex","application/x-latex");
map.put("lha","application/octet-stream");
map.put("lsf","video/x-la-asf");
map.put("lsx","video/x-la-asf");
map.put("lzh","application/octet-stream");
map.put("m13","application/x-msmediaview");
map.put("m14","application/x-msmediaview");
map.put("m3u","audio/x-mpegurl");
map.put("man","application/x-troff-man");
map.put("mdb","application/x-msaccess");
map.put("me","application/x-troff-me");
map.put("mht","message/rfc822");
map.put("mhtml","message/rfc822");
map.put("mid","audio/mid");
map.put("mny","application/x-msmoney");
map.put("mov","video/quicktime");
map.put("movie","video/x-sgi-movie");
map.put("mp2","video/mpeg");
map.put("mp3","audio/mpeg");
map.put("mp4","video/mp4");
map.put("mpa","video/mpeg");
map.put("mpe","video/mpeg");
map.put("mpeg","video/mpeg");
map.put("mpg","video/mpeg");
map.put("mpp","application/vnd.ms-project");
map.put("mpv2","video/mpeg");
map.put("ms","application/x-troff-ms");
map.put("mvb","application/x-msmediaview");
map.put("nws","message/rfc822");
map.put("oda","application/oda");
map.put("p10","application/pkcs10");
map.put("p12","application/x-pkcs12");
map.put("p7b","application/x-pkcs7-certificates");
map.put("p7c","application/x-pkcs7-mime");
map.put("p7m","application/x-pkcs7-mime");
map.put("p7r","application/x-pkcs7-certreqresp");
map.put("p7s","application/x-pkcs7-signature");
map.put("pbm","image/x-portable-bitmap");
map.put("pdf","application/pdf");
map.put("pfx","application/x-pkcs12");
map.put("pgm","image/x-portable-graymap");
map.put("pko","application/ynd.ms-pkipko");
map.put("pma","application/x-perfmon");
map.put("pmc","application/x-perfmon");
map.put("pml","application/x-perfmon");
map.put("pmr","application/x-perfmon");
map.put("pmw","application/x-perfmon");
map.put("pnm","image/x-portable-anymap");
map.put("pot,","application/vnd.ms-powerpoint");
map.put("ppm","image/x-portable-pixmap");
map.put("pps","application/vnd.ms-powerpoint");
map.put("ppt","application/vnd.ms-powerpoint");
map.put("prf","application/pics-rules");
map.put("ps","application/postscript");
map.put("pub","application/x-mspublisher");
map.put("qt","video/quicktime");
map.put("ra","audio/x-pn-realaudio");
map.put("ram","audio/x-pn-realaudio");
map.put("ras","image/x-cmu-raster");
map.put("rgb","image/x-rgb");
map.put("rmi","audio/mid");
map.put("roff","application/x-troff");
map.put("rtf","application/rtf");
map.put("rtx","text/richtext");
map.put("scd","application/x-msschedule");
map.put("sct","text/scriptlet");
map.put("setpay","application/set-payment-initiation");
map.put("setreg","application/set-registration-initiation");
map.put("sh","application/x-sh");
map.put("shar","application/x-shar");
map.put("sit","application/x-stuffit");
map.put("snd","audio/basic");
map.put("spc","application/x-pkcs7-certificates");
map.put("spl","application/futuresplash");
map.put("src","application/x-wais-source");
map.put("sst","application/vnd.ms-pkicertstore");
map.put("stl","application/vnd.ms-pkistl");
map.put("stm","text/html");
map.put("svg","image/svg+xml");
map.put("sv4cpio","application/x-sv4cpio");
map.put("sv4crc","application/x-sv4crc");
map.put("swf","application/x-shockwave-flash");
map.put("t","application/x-troff");
map.put("tar","application/x-tar");
map.put("tcl","application/x-tcl");
map.put("tex","application/x-tex");
map.put("texi","application/x-texinfo");
map.put("texinfo","application/x-texinfo");
map.put("tgz","application/x-compressed");
map.put("tif","image/tiff");
map.put("tiff","image/tiff");
map.put("tr","application/x-troff");
map.put("trm","application/x-msterminal");
map.put("tsv","text/tab-separated-values");
map.put("txt","text/plain");
map.put("uls","text/iuls");
map.put("ustar","application/x-ustar");
map.put("vcf","text/x-vcard");
map.put("vrml","x-world/x-vrml");
map.put("wav","audio/x-wav");
map.put("wcm","application/vnd.ms-works");
map.put("wdb","application/vnd.ms-works");
map.put("wks","application/vnd.ms-works");
map.put("wmf","application/x-msmetafile");
map.put("wps","application/vnd.ms-works");
map.put("wri","application/x-mswrite");
map.put("wrl","x-world/x-vrml");
map.put("wrz","x-world/x-vrml");
map.put("xaf","x-world/x-vrml");
map.put("xbm","image/x-xbitmap");
map.put("xla","application/vnd.ms-excel");
map.put("xlc","application/vnd.ms-excel");
map.put("xlm","application/vnd.ms-excel");
map.put("xls","application/vnd.ms-excel");
map.put("xlt","application/vnd.ms-excel");
map.put("xlw","application/vnd.ms-excel");
map.put("xof","x-world/x-vrml");
map.put("xpm","image/x-xpixmap");
map.put("xwd","image/x-xwindowdump");
map.put("z","application/x-compress");
map.put("zip","application/zip");
}
}

View File

@ -1,6 +1,7 @@
package com.qiwenshare.common.util; package com.qiwenshare.common.util;
import com.qiwenshare.common.config.PropertiesUtil; import com.qiwenshare.common.config.PropertiesUtil;
import com.qiwenshare.common.constant.FileConstant;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
@ -66,21 +67,9 @@ public class PathUtil {
} }
/**
* 获取文件扩展名
*
* @return string
*/
private static String getFileExt(String fileName) {
if (fileName.lastIndexOf(".") == -1) {
return ".png";
//这里暂时用jpg后续应该去获取真实的文件类型
}
return fileName.substring(fileName.lastIndexOf("."));
}
public static String getParentPath(String path) { public static String getParentPath(String path) {
return path.substring(0, path.lastIndexOf("/")); return path.substring(0, path.lastIndexOf(FileConstant.pathSeparator));
} }
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -5,6 +5,7 @@ import com.qiwenshare.common.exception.NotLoginException;
import com.qiwenshare.common.result.RestResult; import com.qiwenshare.common.result.RestResult;
import com.qiwenshare.common.util.DateUtil; import com.qiwenshare.common.util.DateUtil;
import com.qiwenshare.common.util.FileUtil; import com.qiwenshare.common.util.FileUtil;
import com.qiwenshare.common.util.MimeUtils;
import com.qiwenshare.file.anno.MyLog; import com.qiwenshare.file.anno.MyLog;
import com.qiwenshare.file.api.IFileService; import com.qiwenshare.file.api.IFileService;
import com.qiwenshare.file.api.IFiletransferService; import com.qiwenshare.file.api.IFiletransferService;
@ -16,19 +17,27 @@ import com.qiwenshare.file.domain.UserBean;
import com.qiwenshare.file.domain.UserFile; import com.qiwenshare.file.domain.UserFile;
import com.qiwenshare.file.dto.DownloadFileDTO; import com.qiwenshare.file.dto.DownloadFileDTO;
import com.qiwenshare.file.dto.UploadFileDTO; import com.qiwenshare.file.dto.UploadFileDTO;
import com.qiwenshare.file.dto.file.PreviewDTO;
import com.qiwenshare.file.vo.file.UploadFileVo; import com.qiwenshare.file.vo.file.UploadFileVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@Slf4j
@Tag(name = "filetransfer", description = "该接口为文件传输接口,主要用来做文件的上传和下载") @Tag(name = "filetransfer", description = "该接口为文件传输接口,主要用来做文件的上传和下载")
@RestController @RestController
@RequestMapping("/filetransfer") @RequestMapping("/filetransfer")
@ -131,10 +140,55 @@ public class FiletransferController {
@Operation(summary = "下载文件", description = "下载文件接口", tags = {"filetransfer"}) @Operation(summary = "下载文件", description = "下载文件接口", tags = {"filetransfer"})
@MyLog(operation = "下载文件", module = CURRENT_MODULE) @MyLog(operation = "下载文件", module = CURRENT_MODULE)
@RequestMapping(value = "/downloadfile", method = RequestMethod.GET) @RequestMapping(value = "/downloadfile", method = RequestMethod.GET)
public void downloadFile(HttpServletResponse response, DownloadFileDTO downloadFileDTO) { public void downloadFile(HttpServletResponse httpServletResponse, DownloadFileDTO downloadFileDTO) {
filetransferService.downloadFile(response, downloadFileDTO); httpServletResponse.setContentType("application/force-download");// 设置强制下载不打开
UserFile userFile = userFileService.getById(downloadFileDTO.getUserFileId());
String fileName = userFile.getFileName() + "." + userFile.getExtendName();
try {
fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
httpServletResponse.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
filetransferService.downloadFile(httpServletResponse, downloadFileDTO);
} }
@Operation(summary="预览文件", description="用于文件预览", tags = {"filetransfer"})
@GetMapping("/preview")
public void preview(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, PreviewDTO previewDTO){
UserFile userFile = userFileService.getById(previewDTO.getUserFileId());
FileBean fileBean = fileService.getById(userFile.getFileId());
String mime= MimeUtils.getMime(userFile.getExtendName());
// httpServletResponse.setContentType(mime);
httpServletResponse.setHeader("Content-Type", mime);
String rangeString = httpServletRequest.getHeader("Range");//如果是video标签发起的请求就不会为null
if (StringUtils.isNotEmpty(rangeString)) {
long range = Long.valueOf(rangeString.substring(rangeString.indexOf("=") + 1, rangeString.indexOf("-")));
httpServletResponse.setContentLength(Math.toIntExact(fileBean.getFileSize()));
httpServletResponse.setHeader("Content-Range", String.valueOf(range + (Math.toIntExact(fileBean.getFileSize()) - 1)));
}
httpServletResponse.setHeader("Accept-Ranges", "bytes");
String fileName = userFile.getFileName() + "." + userFile.getExtendName();
try {
fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
httpServletResponse.addHeader("Content-Disposition", "fileName=" + fileName);// 设置文件名
DownloadFileDTO downloadFileDTO = new DownloadFileDTO();
downloadFileDTO.setUserFileId(previewDTO.getUserFileId());
try {
filetransferService.downloadFile(httpServletResponse, downloadFileDTO);
}catch (Exception e){
//org.apache.catalina.connector.ClientAbortException: java.io.IOException: 你的主机中的软件中止了一个已建立的连接
log.error("该异常忽略不做处理");
}
}
@Operation(summary = "获取存储信息", description = "获取存储信息", tags = {"filetransfer"}) @Operation(summary = "获取存储信息", description = "获取存储信息", tags = {"filetransfer"})
@RequestMapping(value = "/getstorage", method = RequestMethod.GET) @RequestMapping(value = "/getstorage", method = RequestMethod.GET)

View File

@ -0,0 +1,10 @@
package com.qiwenshare.file.dto.file;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Data
@Schema(name = "预览文件DTO",required = true)
public class PreviewDTO {
private Long userFileId;
}

View File

@ -1,7 +1,11 @@
package com.qiwenshare.file.service; package com.qiwenshare.file.service;
import java.io.UnsupportedEncodingException; import java.io.*;
import java.util.List; import java.util.List;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -20,6 +24,7 @@ import com.qiwenshare.common.operation.download.Downloader;
import com.qiwenshare.common.factory.FileOperationFactory; import com.qiwenshare.common.factory.FileOperationFactory;
import com.qiwenshare.common.operation.upload.Uploader; import com.qiwenshare.common.operation.upload.Uploader;
import com.qiwenshare.common.util.PathUtil;
import com.qiwenshare.file.api.IFiletransferService; import com.qiwenshare.file.api.IFiletransferService;
import com.qiwenshare.common.config.QiwenFileConfig; import com.qiwenshare.common.config.QiwenFileConfig;
@ -130,35 +135,101 @@ public class FiletransferService implements IFiletransferService {
public void downloadFile(HttpServletResponse httpServletResponse, DownloadFileDTO downloadFileDTO) { public void downloadFile(HttpServletResponse httpServletResponse, DownloadFileDTO downloadFileDTO) {
UserFile userFile = userFileMapper.selectById(downloadFileDTO.getUserFileId()); UserFile userFile = userFileMapper.selectById(downloadFileDTO.getUserFileId());
String fileName = userFile.getFileName() + "." + userFile.getExtendName(); if (userFile.getIsDir() == 0) {
try {
fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
httpServletResponse.setContentType("application/force-download");// 设置强制下载不打开
httpServletResponse.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
FileBean fileBean = fileMapper.selectById(userFile.getFileId());
Downloader downloader = null; FileBean fileBean = fileMapper.selectById(userFile.getFileId());
if (fileBean.getIsOSS() != null && fileBean.getIsOSS() == 1) { Downloader downloader = null;
downloader = aliyunOSSOperationFactory.getDownloader(); if (fileBean.getIsOSS() != null && fileBean.getIsOSS() == 1) {
} else if (fileBean.getStorageType() == 0) { downloader = aliyunOSSOperationFactory.getDownloader();
downloader = localStorageOperationFactory.getDownloader(); } else if (fileBean.getStorageType() == 0) {
} else if (fileBean.getStorageType() == 1) { downloader = localStorageOperationFactory.getDownloader();
downloader = aliyunOSSOperationFactory.getDownloader(); } else if (fileBean.getStorageType() == 1) {
} else if (fileBean.getStorageType() == 2) { downloader = aliyunOSSOperationFactory.getDownloader();
downloader = fastDFSOperationFactory.getDownloader(); } else if (fileBean.getStorageType() == 2) {
} downloader = fastDFSOperationFactory.getDownloader();
if (downloader == null) { }
log.error("下载失败文件存储类型不支持下载storageType:{}, isOSS:{}", fileBean.getStorageType(), fileBean.getIsOSS()); if (downloader == null) {
throw new UploadGeneralException("下载失败"); log.error("下载失败文件存储类型不支持下载storageType:{}, isOSS:{}", fileBean.getStorageType(), fileBean.getIsOSS());
} throw new UploadGeneralException("下载失败");
DownloadFile uploadFile = new DownloadFile(); }
uploadFile.setFileUrl(fileBean.getFileUrl()); DownloadFile uploadFile = new DownloadFile();
uploadFile.setFileUrl(fileBean.getFileUrl());
// uploadFile.setTimeStampName(fileBean.getTimeStampName()); // uploadFile.setTimeStampName(fileBean.getTimeStampName());
downloader.download(httpServletResponse, uploadFile); downloader.download(httpServletResponse, uploadFile);
} else {
LambdaQueryWrapper<UserFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.likeRight(UserFile::getFilePath, userFile.getFilePath())
.eq(UserFile::getUserId, userFile.getUserId())
.eq(UserFile::getIsDir, 0)
.eq(UserFile::getDeleteFlag, 0);
List<UserFile> userFileList = userFileMapper.selectList(lambdaQueryWrapper);
String staticPath = PathUtil.getStaticPath();
String tempPath = staticPath + "temp" + File.separator;
File tempFile = new File(tempPath);
if (!tempFile.exists()) {
tempFile.mkdirs();
}
FileOutputStream f = null;
try {
f = new FileOutputStream(tempPath + userFile.getFileName() + ".zip");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
ZipOutputStream zos = new ZipOutputStream(csum);
BufferedOutputStream out = new BufferedOutputStream(zos);
zos.setComment("A test of Java Zipping");
for (UserFile userFile1 : userFileList) {
FileBean fileBean = fileMapper.selectById(userFile.getFileId());
Downloader downloader = null;
if (fileBean.getIsOSS() != null && fileBean.getIsOSS() == 1) {
downloader = aliyunOSSOperationFactory.getDownloader();
} else if (fileBean.getStorageType() == 0) {
downloader = localStorageOperationFactory.getDownloader();
} else if (fileBean.getStorageType() == 1) {
downloader = aliyunOSSOperationFactory.getDownloader();
} else if (fileBean.getStorageType() == 2) {
downloader = fastDFSOperationFactory.getDownloader();
}
if (downloader == null) {
log.error("下载失败文件存储类型不支持下载storageType:{}, isOSS:{}", fileBean.getStorageType(), fileBean.getIsOSS());
throw new UploadGeneralException("下载失败");
}
DownloadFile downloadFile = new DownloadFile();
downloadFile.setFileUrl(fileBean.getFileUrl());
InputStream inputStream = downloader.getInputStream(downloadFile);
BufferedInputStream bis = new BufferedInputStream(inputStream);
try {
zos.putNextEntry(new ZipEntry(userFile1.getFilePath()));
byte[] buffer = new byte[1024];
int i = bis.read(buffer);
while (i != -1) {
out.write(buffer, 0, i);
i = bis.read(buffer);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
} }
@Override @Override

20
pom.xml
View File

@ -92,25 +92,6 @@
<artifactId>mybatis-plus-boot-starter</artifactId> <artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version> <version>3.4.1</version>
</dependency> </dependency>
<!-- <dependency>-->
<!-- <groupId>com.alibaba</groupId>-->
<!-- <artifactId>fastjson</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.hibernate</groupId>-->
<!-- <artifactId>hibernate-core</artifactId>-->
<!-- <version>5.4.1.Final</version>-->
<!-- </dependency>-->
<!--阿里云短信验证服务-->
<!-- <dependency>-->
<!-- <groupId>com.aliyun</groupId>-->
<!-- <artifactId>aliyun-java-sdk-core</artifactId>-->
<!-- <version>4.0.3</version>-->
<!-- </dependency>-->
<dependency> <dependency>
<groupId>org.apache.shiro</groupId> <groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId> <artifactId>shiro-core</artifactId>
@ -123,7 +104,6 @@
<modules> <modules>
<module>file-common</module> <module>file-common</module>
<module>file-web</module> <module>file-web</module>
<!-- <module>file-office</module>-->
</modules> </modules>