|
@@ -1,24 +1,13 @@
|
|
|
package com.uas.report.service.impl;
|
|
package com.uas.report.service.impl;
|
|
|
|
|
|
|
|
-import java.io.File;
|
|
|
|
|
-import java.io.FileFilter;
|
|
|
|
|
-import java.io.FileInputStream;
|
|
|
|
|
-import java.io.FileNotFoundException;
|
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
-import java.io.OutputStream;
|
|
|
|
|
-import java.net.URI;
|
|
|
|
|
-import java.net.URLEncoder;
|
|
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
|
-import java.util.Date;
|
|
|
|
|
-import java.util.HashMap;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
-import java.util.Map;
|
|
|
|
|
-
|
|
|
|
|
-import javax.servlet.ServletOutputStream;
|
|
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
-
|
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.lowagie.text.pdf.PdfReader;
|
|
|
|
|
+import com.uas.report.SpecialProperties;
|
|
|
|
|
+import com.uas.report.schedule.model.TaskInformation;
|
|
|
|
|
+import com.uas.report.schedule.service.Executable;
|
|
|
|
|
+import com.uas.report.schedule.service.TaskService;
|
|
|
|
|
+import com.uas.report.service.FileService;
|
|
|
|
|
+import com.uas.report.util.*;
|
|
|
import org.apache.commons.io.IOUtils;
|
|
import org.apache.commons.io.IOUtils;
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
@@ -28,20 +17,14 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
-import com.lowagie.text.pdf.PdfReader;
|
|
|
|
|
-import com.uas.report.SpecialProperties;
|
|
|
|
|
-import com.uas.report.schedule.model.TaskInformation;
|
|
|
|
|
-import com.uas.report.schedule.service.Executable;
|
|
|
|
|
-import com.uas.report.schedule.service.TaskService;
|
|
|
|
|
-import com.uas.report.service.FileService;
|
|
|
|
|
-import com.uas.report.util.ArrayUtils;
|
|
|
|
|
-import com.uas.report.util.ExceptionUtils;
|
|
|
|
|
-import com.uas.report.util.FileUtils;
|
|
|
|
|
-import com.uas.report.util.ReportConstants;
|
|
|
|
|
-import com.uas.report.util.ReportUtils;
|
|
|
|
|
-import com.uas.report.util.StringUtils;
|
|
|
|
|
-import com.uas.report.util.ZipUtils;
|
|
|
|
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
+import java.io.*;
|
|
|
|
|
+import java.net.URI;
|
|
|
|
|
+import java.net.URLEncoder;
|
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
|
|
@Service
|
|
@Service
|
|
|
public class FileServiceImpl implements FileService {
|
|
public class FileServiceImpl implements FileService {
|
|
@@ -261,22 +244,19 @@ public class FileServiceImpl implements FileService {
|
|
|
throw new FileNotFoundException("文件不存在:" + filePath);
|
|
throw new FileNotFoundException("文件不存在:" + filePath);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- String fileName = "";
|
|
|
|
|
// 下载文件夹之前,需进行压缩
|
|
// 下载文件夹之前,需进行压缩
|
|
|
if (file.isDirectory()) {
|
|
if (file.isDirectory()) {
|
|
|
- fileName = file.getName() + ".zip";
|
|
|
|
|
byte[] data = ZipUtils.zipFolder(filePath, fileFilter);
|
|
byte[] data = ZipUtils.zipFolder(filePath, fileFilter);
|
|
|
- download(data, fileName, response);
|
|
|
|
|
|
|
+ download(data, file.getName() + ".zip", response);
|
|
|
} else {
|
|
} else {
|
|
|
- fileName = file.getName();
|
|
|
|
|
- download(file, fileName, response);
|
|
|
|
|
|
|
+ download(file, file.getName(), response);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public void download(byte[] data, String fileName, HttpServletResponse response) {
|
|
public void download(byte[] data, String fileName, HttpServletResponse response) {
|
|
|
if (ArrayUtils.isEmpty(data) || StringUtils.isEmpty(fileName) || response == null) {
|
|
if (ArrayUtils.isEmpty(data) || StringUtils.isEmpty(fileName) || response == null) {
|
|
|
- throw new IllegalArgumentException("参数不能为空:data,filePath,response");
|
|
|
|
|
|
|
+ throw new IllegalArgumentException("参数不能为空:data,fileName,response");
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
|
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
|
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
|
@@ -299,6 +279,7 @@ public class FileServiceImpl implements FileService {
|
|
|
}
|
|
}
|
|
|
FileInputStream fis = new FileInputStream(file);
|
|
FileInputStream fis = new FileInputStream(file);
|
|
|
|
|
|
|
|
|
|
+ fileName = StringUtils.isEmpty(fileName) ? file.getName() : fileName;
|
|
|
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
|
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
|
|
ServletOutputStream outputStream = response.getOutputStream();
|
|
ServletOutputStream outputStream = response.getOutputStream();
|
|
|
try {
|
|
try {
|
|
@@ -319,15 +300,36 @@ public class FileServiceImpl implements FileService {
|
|
|
@Override
|
|
@Override
|
|
|
public void rangeDownload(File file, String fileName, HttpServletRequest request, HttpServletResponse response)
|
|
public void rangeDownload(File file, String fileName, HttpServletRequest request, HttpServletResponse response)
|
|
|
throws IOException {
|
|
throws IOException {
|
|
|
|
|
+ rangeDownload(file, fileName, null, request, response);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void rangeDownloadWithContentType(File file, String contentType, HttpServletRequest request, HttpServletResponse response)
|
|
|
|
|
+ throws IOException {
|
|
|
|
|
+ if (StringUtils.isEmpty(contentType)) {
|
|
|
|
|
+ throw new IllegalArgumentException("参数不能为空:contentType");
|
|
|
|
|
+ }
|
|
|
|
|
+ rangeDownload(file, null, contentType, request, response);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 下载文件(支持断点续传)
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param file 要下载的文件
|
|
|
|
|
+ * @param fileName 下载后的文件名称
|
|
|
|
|
+ * @param contentType 指定的contentType,可为空
|
|
|
|
|
+ * @param request
|
|
|
|
|
+ * @param response
|
|
|
|
|
+ * @throws IOException
|
|
|
|
|
+ */
|
|
|
|
|
+ private void rangeDownload(File file, String fileName, String contentType, HttpServletRequest request, HttpServletResponse response)
|
|
|
|
|
+ throws IOException {
|
|
|
if (file == null) {
|
|
if (file == null) {
|
|
|
throw new IllegalArgumentException("参数不能为空:file");
|
|
throw new IllegalArgumentException("参数不能为空:file");
|
|
|
}
|
|
}
|
|
|
if (!file.exists() || !file.isFile()) {
|
|
if (!file.exists() || !file.isFile()) {
|
|
|
throw new IOException("文件不存在或并非文件");
|
|
throw new IOException("文件不存在或并非文件");
|
|
|
}
|
|
}
|
|
|
- if (StringUtils.isEmpty(fileName)) {
|
|
|
|
|
- fileName = file.getName();
|
|
|
|
|
- }
|
|
|
|
|
FileInputStream fis = new FileInputStream(file);
|
|
FileInputStream fis = new FileInputStream(file);
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
@@ -368,8 +370,14 @@ public class FileServiceImpl implements FileService {
|
|
|
|
|
|
|
|
long contentLength = end - start + 1;
|
|
long contentLength = end - start + 1;
|
|
|
response.setHeader("Content-Length", contentLength + "");
|
|
response.setHeader("Content-Length", contentLength + "");
|
|
|
- // response.setContentType("application/octet-stream");
|
|
|
|
|
- response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
|
|
|
|
|
|
+ // 如果指定了 contentType
|
|
|
|
|
+ if (!StringUtils.isEmpty(contentType)) {
|
|
|
|
|
+ response.setContentType(contentType);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ fileName = StringUtils.isEmpty(fileName) ? file.getName() : fileName;
|
|
|
|
|
+ // response.setContentType("application/octet-stream");
|
|
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
ServletOutputStream outputStream = response.getOutputStream();
|
|
ServletOutputStream outputStream = response.getOutputStream();
|
|
|
byte[] data = new byte[1024];
|
|
byte[] data = new byte[1024];
|
|
@@ -380,6 +388,7 @@ public class FileServiceImpl implements FileService {
|
|
|
outputStream.write(data, 0, i);
|
|
outputStream.write(data, 0, i);
|
|
|
count += i;
|
|
count += i;
|
|
|
}
|
|
}
|
|
|
|
|
+ // 最后一个数据块不足 1024 个字节
|
|
|
if (count < contentLength) {
|
|
if (count < contentLength) {
|
|
|
i = fis.read(data, 0, (int) (contentLength - count));
|
|
i = fis.read(data, 0, (int) (contentLength - count));
|
|
|
outputStream.write(data, 0, i);
|
|
outputStream.write(data, 0, i);
|
|
@@ -387,7 +396,6 @@ public class FileServiceImpl implements FileService {
|
|
|
outputStream.flush();
|
|
outputStream.flush();
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
|
logger.error(e.getMessage());
|
|
logger.error(e.getMessage());
|
|
|
- return;
|
|
|
|
|
}
|
|
}
|
|
|
} finally {
|
|
} finally {
|
|
|
fis.close();
|
|
fis.close();
|