用truezip-6.jar可以解决中文名称和中文路径等问题,
因为用了 zipOut = new ZipOutputStream(out,"GBK");
用ant.jar有时会出现问题.
页面编码用:encodeURIComponent()
后台解码用:URLDecoder.decode(files[i],"UTF-8")
Action
引用
public ActionForward exportPicture(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ServletOutputStream out = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
PicService service = new PicService();
String fileName = request.getParameter("fileName"); //文件名
String filePath = request.getParameter("filePath"); //文件路径
String path=request.getSession().getServletContext().getRealPath("/");
if(fileName!=null && filePath != null){
response.reset();
response.setContentType("application/zip;charset=UTF-8");
response.setHeader("Content-Disposition" ,"attachment;filename="+new String("picture.zip".getBytes(),"iso-8859-1"));
out = response.getOutputStream();
String[] files = filePath.split(",");
String[] fileNS = fileName.split(",");
String zipFilePath = service.getZipFile(files,fileNS,path);
FileInputStream fin = new FileInputStream(zipFilePath);
// bis = new BufferedInputStream(fin);
bos = new BufferedOutputStream(out);
byte buff[] = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = fin.read(buff, 0, buff.length)))
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
log.error("导出图片出错", e);
} finally {
if(bis != null)
try {
bis.close();
} catch (IOException e) {
log.error("导出图片出错", e);
}
if(bos != null)
try {
bos.close();
} catch (IOException e) {
log.error("导出图片出错", e);
}
if (out != null)
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
log.error("导出图片出错", e);
}
}
return null;
}
Service
引用
public String getZipFile(String[] f,String[] fn,String path){
String[] files = f ;
String filePath =path+System.currentTimeMillis()+".zip";
FileInputStream in = null;
FileOutputStream out=null;
ZipOutputStream zipOut = null;
try
{
out = new FileOutputStream( filePath ); //创建文件输出流对象
zipOut = new ZipOutputStream(out,"GBK"); //创建ZIP数据输出流对象
ZipEntry entry;
int nNumber;
for ( int i=0; i<files.length;i++){ //创建文件输入流对象
String filePath_ = URLDecoder.decode(files[i],"UTF-8") ;
File file = new File(path+(filePath_.replace("/", "\\")));
if(file.exists()){
// in = new FileInputStream( baseUrl.getUploadPhotoRoot()+(files[i].replace("/", "\\")) );
in = new FileInputStream(file);
//创建指向压缩原始文件的入口
if(in!=null){
String temp = URLDecoder.decode(fn[i],"UTF-8")+"_" + String.valueOf(i)+filePath_.substring(filePath_.lastIndexOf("."));
// String cnName=URLDecoder.decode(fn[i],"UTF-8");
// String temp =cnName+String.valueOf(i)+filePath_.substring(filePath_.lastIndexOf("."));
//entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/")));
entry = new ZipEntry(temp);
zipOut.putNextEntry( entry );
//向压缩文件中输出数据
byte[] buffer = new byte[512];
while ((nNumber=in.read(buffer)) != -1)
zipOut.write(buffer,0,nNumber);
in.close();
}
}
}
// zipOut.close();
//out.close();
}
catch(IOException e)
{
log.error(e.getMessage());
}
finally{
try {
if(zipOut!=null){
zipOut.close();
}
if(out!=null){
out.close();
}
// // in.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
return filePath;
}