以下是下载的代码
package com.fh.util;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
/**
* 下载文件
* @version
*/
public class FileDownload {
/**
* @param response
* @param filePath //文件完整路径(包括文件名和扩展名)
* @param fileName //下载后看到的文件名
* @return 文件名
*/
public static void fileDownload(final HttpServletResponse response, String filePath, String fileName) throws Exception{
byte[] data = FileUtil.toByteArray2(filePath);
fileName = URLEncoder.encode(fileName, "UTF-8");
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.addHeader("Content-Length", "" + data.length);
response.setContentType("application/octet-stream;charset=UTF-8");
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
outputStream.write(data);
outputStream.flush();
outputStream.close();
response.flushBuffer();
}
}
以下为压缩代码
package com.fh.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Random;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletResponse;
import com.fh.util.express.enums.Method;
public class ZipUtils {
private Object path;
private ZipUtils(){
}
public static void doCompress(String srcFile,String cph, String zipFile) throws IOException {
try {
doCompress(new File(srcFile),cph, new File(zipFile));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 文件压缩
* @param srcFile 目录或者单个文件
* @param zipFile 压缩后的ZIP文件
* @throws Exception
*/
public static void doCompress(File srcFile,String cph, File zipFile) throws Exception {
ZipOutputStream out = null;
try {
out = new ZipOutputStream(new FileOutputStream(zipFile));
doCompress(srcFile,cph, out);
} catch (Exception e) {
throw e;
} finally {
out.close();//记得关闭资源
}
}
/**
*
* @param filelName 要下载的文件的路径全名
* @param cph
* @param out
* @throws IOException
*/
public static void doCompress(String filelName,String cph, ZipOutputStream out) throws IOException{
doCompress(new File(filelName),cph, out);
}
/**
*
* @param file 要写入发每一个文件
* @param cph
* @param out
* @throws IOException
*/
public static void doCompress(File file,String cph, ZipOutputStream out) throws IOException{
doCompress(file,cph, out, "");//为了判断是不是文件夹做准备
}
public static void doCompress(File inFile,String cph, ZipOutputStream out, String dir) throws IOException {
if ( inFile.isDirectory() ) {
File[] files = inFile.listFiles();
if (files!=null && files.length>0) {
for (File file : files) {
String name = inFile.getName();
if (!"".equals(dir)) {
name = dir + "/" + name;
}
ZipUtils.doCompress(file,cph, out, name);
}
}
} else {
ZipUtils.doZip(inFile,cph, out, dir);
}
}
/**
*
* @param inFile 压缩包下面的每一个文件
* @param cph 车牌号
* @param out
* @param dir 是一个空字符串,为了判断有没有子集目录
* @throws IOException
*/
public static void doZip(File inFile, String cph,ZipOutputStream out, String dir) throws IOException {
String entryName = null;
if (!"".equals(dir)) {
entryName = dir + "/"+ inFile.getName();
}else {
entryName =inFile.getName();
}
ZipEntry entry = new ZipEntry(entryName);
out.putNextEntry(entry);
int len = 0 ;
byte[] buffer = new byte[3*1024];
FileInputStream fis = new FileInputStream(inFile);
while ((len = fis.read(buffer)) > 0) {
out.write(buffer);
out.flush();
}
out.closeEntry();
fis.close();
}
@SuppressWarnings("unused")
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();//递归删除目录中的子目录下
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// 目录此时为空,可以删除
return dir.delete();
}
/**
* 删除空目录
* @param dir 将要删除的目录路径
*/
@SuppressWarnings("unused")
public static void doDeleteEmptyDir(String dir) {
boolean success = (new File(dir)).delete();
if (success) {
System.out.println("Successfully deleted empty directory: " + dir);
} else {
System.out.println("Failed to delete empty directory: " + dir);
}
}
public static void main(String[] args) throws IOException {
doCompress("D:/车检报告/201702/泾川县鸿鑫物流有限公司","", "D:/车检报告/201702/泾川县鸿鑫物流有限公司22.zip");//动态压缩,创建临时文件,完成后删除临时文件
}
}