|
|
@@ -1,11 +1,24 @@
|
|
|
package com.xunmei.file.service;
|
|
|
|
|
|
+import com.xunmei.common.core.utils.uuid.UUID;
|
|
|
+import com.xunmei.file.utils.FileDownUtils;
|
|
|
import com.xunmei.file.utils.FileUploadUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+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.context.annotation.Primary;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.net.URLDecoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+
|
|
|
/**
|
|
|
* 本地文件存储
|
|
|
*
|
|
|
@@ -15,24 +28,18 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
@Service
|
|
|
public class LocalSysFileServiceImpl implements ISysFileService
|
|
|
{
|
|
|
- /**
|
|
|
- * 资源映射路径 前缀
|
|
|
- */
|
|
|
- @Value("${file.prefix}")
|
|
|
- public String localFilePrefix;
|
|
|
|
|
|
- /**
|
|
|
- * 域名或本机访问地址
|
|
|
- */
|
|
|
- @Value("${file.domain}")
|
|
|
- public String domain;
|
|
|
-
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(LocalSysFileServiceImpl.class);
|
|
|
+
|
|
|
/**
|
|
|
* 上传文件存储在本地的根路径
|
|
|
*/
|
|
|
@Value("${file.path}")
|
|
|
private String localFilePath;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private HttpServletRequest request;
|
|
|
+
|
|
|
/**
|
|
|
* 本地文件上传接口
|
|
|
*
|
|
|
@@ -44,7 +51,45 @@ public class LocalSysFileServiceImpl implements ISysFileService
|
|
|
public String uploadFile(MultipartFile file) throws Exception
|
|
|
{
|
|
|
String name = FileUploadUtils.upload(localFilePath, file);
|
|
|
- String url = domain + localFilePrefix + name;
|
|
|
- return url;
|
|
|
+// String url = domain + localFilePrefix + name;
|
|
|
+ return name;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void downloadFile(HttpServletResponse response, String filePath){
|
|
|
+ ByteArrayOutputStream out = null;
|
|
|
+ try {
|
|
|
+ filePath = localFilePath + filePath;
|
|
|
+ filePath = URLDecoder.decode(filePath,"UTF-8");
|
|
|
+ out = FileDownUtils.downloadFile(filePath);
|
|
|
+ String fileSuffix = FileDownUtils.getFileSuffix(filePath);
|
|
|
+ String formFileName = UUID.randomUUID().toString() + fileSuffix;
|
|
|
+ String userAgent = request.getHeader("User-Agent");
|
|
|
+
|
|
|
+ // 针对IE或者以IE为内核的浏览器:
|
|
|
+ if (StringUtils.isNotEmpty(userAgent) && (userAgent.contains("MSIE") || userAgent.contains("Trident"))) {
|
|
|
+ formFileName = java.net.URLEncoder.encode(formFileName, "UTF-8");
|
|
|
+ } else {
|
|
|
+ // 非IE浏览器的处理:
|
|
|
+ formFileName = new String(formFileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
|
|
|
+ }
|
|
|
+ response.reset();
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
+ response.addHeader("Content-Disposition", "attachment;filename=" + formFileName);
|
|
|
+ response.addHeader("Content-Length", "" + out.size());
|
|
|
+ out.writeTo(response.getOutputStream());
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("文件下载失败! ");
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ out.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|