近期,项目要求把服务器存储的上传文件,批量下载到本地.参考网上资料,实现了服务器文件打包成压缩文件然后down到本地功能. 具体代码实现: 1、在服务器端创建一个临时zip格式文件 2、用jdk自带的API将服务器所有文件输入到zip文件中 3、将zip文件下载到本地,并删除服务器端zip文件 @RequestMapping(value = "/downloadZip.do")
public String downloadFiles(String tcLwIds, HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<File> files = new ArrayList<File>();
File Allfile = new File(request.getSession().getServletContext().getRealPath("/") + "upload/");
if (Allfile.exists()) {
File[] fileArr = Allfile.listFiles();
for (File file2 : fileArr) {
files.add(file2);
}
}
String fileName = UUID.randomUUID().toString() + ".zip";
String outFilePath = request.getSession().getServletContext().getRealPath("/") + "upload/";
File fileZip = new File(outFilePath + fileName);
FileOutputStream outStream = new FileOutputStream(fileZip);
ZipOutputStream toClient = new ZipOutputStream(outStream);
zipFile(files, toClient);
toClient.close();
outStream.close();
this.downloadFile(fileZip, response, true);
return null;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
将服务器文件存到压缩包中 public static void zipFile(List<File> files, ZipOutputStream outputStream) throws IOException, ServletException {
try {
int size = files.size();
for (int i = 0; i < size; i++) {
File file = (File) files.get(i);
zipFile(file, outputStream);
}
} catch (IOException e) {
throw e;
}
}
public static void zipFile(File inputFile, ZipOutputStream outputstream) throws IOException, ServletException {
try {
if (inputFile.exists()) {
if (inputFile.isFile()) {
FileInputStream inStream = new FileInputStream(inputFile);
BufferedInputStream bInStream = new BufferedInputStream(inStream);
ZipEntry entry = new ZipEntry(inputFile.getName());
outputstream.putNextEntry(entry);
final int MAX_BYTE = 10 * 1024 * 1024;
long streamTotal = 0;
int streamNum = 0;
int leaveByte = 0;
byte[] inOutbyte;
streamTotal = bInStream.available();
streamNum = (int) Math.floor(streamTotal / MAX_BYTE);
leaveByte = (int) streamTotal % MAX_BYTE;
if (streamNum > 0) {
for (int j = 0; j < streamNum; ++j) {
inOutbyte = new byte[MAX_BYTE];
bInStream.read(inOutbyte, 0, MAX_BYTE);
outputstream.write(inOutbyte, 0, MAX_BYTE);
}
}
inOutbyte = new byte[leaveByte];
bInStream.read(inOutbyte, 0, leaveByte);
outputstream.write(inOutbyte);
outputstream.closeEntry();
bInStream.close();
inStream.close();
}
} else {
throw new ServletException("文件不存在!");
}
} catch (IOException e) {
throw e;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
下载文件 public void downloadFile(File file,HttpServletResponse response,boolean isDelete) {
try {
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes("UTF-8"),"ISO-8859-1"));
toClient.write(buffer);
toClient.flush();
toClient.close();
if(isDelete)
{
file.delete();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
单个文件的下载 @RequestMapping(value="/singleDownload.do")
public String singleDownload(String fileName, HttpServletRequest request,
HttpServletResponse response)throws Exception{
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;fileName="+ fileName);
String realPath = request.getSession().getServletContext().getRealPath("/");
String path = realPath+"upload/";
File file = new File(path+ File.separator + fileName);
downloadFile(file, response, false);
return null;
}
|