J2EE 文件下载工具类
一个简单的下载类,在此记录
package com.sgcc.ahepc.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
import org.dom4j.DocumentException;
public class FileUtil {
public static void downFile(String path,String filename,HttpServletResponse response) throws DocumentException, IOException{
response.setContentType("multipart/form-data");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename,"UTF-8"));
String sep = System.getProperty("file.separator");
String filePath = path+sep+filename;
InputStream is = new FileInputStream(new File(filePath));
OutputStream os = response.getOutputStream();
byte[] b= new byte[1024];
int length;
while((length = is.read(b)) > 0){
os.write(b,0,length);
}
os.flush();
os.close();
is.close();
}
}
该类适合多种文件相应,什么excel,word,图片等,统统不是问题。哈哈