@RequestMapping("downloadsByplFromFTP")
@ResponseBody
public void downloadsByplFromFTP(HttpServletRequest request, HttpServletResponse response) throws IOException{
try {
//加载FTP
initFTP();
String ryids = request.getParameter("ryid"); //id
String [] ryidResult = ryids.split(",");
//每个人问资料打包成一个文件夹,所有人的文件夹打包成一个压缩包
String workids ="";
String xm ="";
//打包
byte[] buf = new byte[2048];
File zipFile = new File("d:\\EntryMaterialsPl");
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
//out.setEncoding("UTF-8");
for(int m=0;m<ryidResult.length ;m++){
String ryid = ryidResult[m];
//根据id获取workid返回拼成字符串的多个id
workids = uploadToFtpService.getWorkids(ryid);
//根据id查询人员姓名
xm = uploadToFtpService.getXm(ryid);
List<UploadToFtpEntity> fileInfoList = uploadToFtpService.getFileInfo(workids);
//防止文件名中有中文时出错
System.setProperty("sun.zip.encoding", System.getProperty("sun.jnu.encoding"));
for (int i=0;i< fileInfoList.size();i++) {
String cllx = fileInfoList.get(i).getCllx(); //材料类型
String name = ""; //附件名称
// 压缩包里可以包含多个文件夹
ZipEntry entry = new ZipEntry( xm+"\\"+(i+1)+"_"+name+"."+fileInfoList.get(i).getFileExpendname());
zipOut.putNextEntry(entry);
InputStream bis = ftp.retrieveFileStream(fileInfoList.get(i).getFilePath());
if (bis != null){
int readLen = -1;
while ((readLen = bis.read(buf, 0, 1024)) != -1) {
zipOut.write(buf, 0, readLen);
}
zipOut.closeEntry();
bis.close();
ftp.completePendingCommand();
//调用ftp.retrieveFileStream这个接口后,一定要手动close掉返回的InputStream,然后再调用 completePendingCommand方法
// ,若不是按照这个顺序,则会导致后面对FTPClient的操作都失败
}
}
}
zipOut.close();
//注销FTP登陆
ftp.logout();
//关闭FTP
if (ftp.isConnected()) { ftp.disconnect(); }
//下载
int len;
FileInputStream zipInput =new FileInputStream(zipFile);
OutputStream out = response.getOutputStream();
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment; filename="
+ URLEncoder.encode("EntryMaterialsPl", "UTF-8") + ".zip");
while ((len=zipInput.read(buf))!= -1){
out.write(buf,0,len);
}
zipInput.close();
out.flush();
out.close();
//删除压缩包
zipFile.delete();
} catch (Exception e)
{
System.out.println("文件打包下载有误: " + e.getLocalizedMessage());
e.printStackTrace();
}
}
package com.neusoft.sr.web.common.controller;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class FileToZip {
private static final String BASE_DIR = "";
private static final int BUFFER = 2048;
// 符号"/"用来作为目录标识判断符
private static final String PATH = "/";
private FileToZip() {
}
public static void fileToZip(String inputFileName, String outputFileName) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputFileName));
//out.setEncoding("GBK"); // ###### 这句话是关键,指定输出的编码方式
File f = new File(inputFileName);
fileToZip(out, f, "");
out.close();
}
private static void fileToZip(ZipOutputStream out, File f, String base) throws Exception {
if (f.isDirectory()) {
File[] fl = f.listFiles();
base = base.length() == 0 ? "" : base + "/";
out.putNextEntry(new ZipEntry(base));
for (int i = 0; i < fl.length; i++) {
fileToZip(out, fl[i], base + fl[i].getName());
}
} else {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
byte[] buffer = new byte[1024];
int n = in.read(buffer);
while (n != -1) {
out.write(buffer, 0, n);
n = in.read(buffer);
}
in.close();
}
}
public static void compress(File srcFile) throws Exception {
String name = srcFile.getName();
String basePath = srcFile.getParent();
String destPath = basePath + name + ".zip";
compress(srcFile, destPath);
}
public static void compress(File srcFile, File destFile) throws Exception {
// 对输出文件做CRC32校验
CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream(
destFile), new CRC32());
ZipOutputStream zos = new ZipOutputStream(cos);
zos.setLevel(9);
compress(srcFile, zos, BASE_DIR);
zos.flush();
zos.close();
}
public static void compress(File srcFile, String destPath) throws Exception {
compress(srcFile, new File(destPath));
}
private static void compress(File srcFile, ZipOutputStream zos,
String basePath) throws Exception {
if (srcFile.isDirectory()) {
compressDir(srcFile, zos, basePath);
} else {
compressFile(srcFile, zos, basePath);
}
}
private static void compressDir(File dir, ZipOutputStream zos,
String basePath) throws Exception {
File[] files = dir.listFiles();
// 构建空目录
if (files.length < 1) {
ZipEntry entry = new ZipEntry(dir.getName() + PATH);
zos.putNextEntry(entry);
zos.closeEntry();
}
for (File file : files) {
// 递归压缩
//这里加上basePath就会带上当前目录,如果不加就会只打包该目录下的文件/文件夹
compress(file, zos, basePath+ dir.getName() + PATH);
}
}
private static void compressFile(File file, ZipOutputStream zos, String dir)
throws Exception {
/**
* 压缩包内文件名定义
*
* <pre>
* 如果有多级目录,那么这里就需要给出包含目录的文件名
* 如果用WinRAR打开压缩包,中文名将显示为乱码
* </pre>
*/
ZipEntry entry = new ZipEntry(dir + file.getName());
zos.putNextEntry(entry);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
file));
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
zos.write(data, 0, count);
}
bis.close();
zos.closeEntry();
}
/**
* 将文件打包成ZIP压缩文件,main方法测试
* @param args
* @throws Exception
* @throws IOException
*/
public static void main(String[] args) throws Exception {
String sourceFilePath = "E:/java/iamges-tuojin-yn/MT-GROUNPON-2/1";
String targetUrl = "E:/java/iamges-tuojin-yn.zip";
//fileToZip(sourceFilePath, targetUrl);
compress(new File(sourceFilePath), targetUrl);
System.out.println("end !!!");
}
}