FileExportUtils
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
public class FileExportUtils {
private static HttpServletRequest excelRequest = null;
private static HttpServletResponse excelResponse = null;
private static FileOutputStream fileOut;
public FileExportUtils(PageContext context) {
this.excelRequest = (HttpServletRequest) context.getRequest();
this.excelResponse = (HttpServletResponse) context.getResponse();
}
public FileExportUtils(HttpServletRequest request,
HttpServletResponse response) {
excelRequest = request;
excelResponse = response;
}
public void outputTxt(String excelName, String resultStr) {
//String realPath = "c:\\"+excelName+".xls";
String realPath = getExcelRealPath(excelName);
writerFileStream(realPath,resultStr);
downloadFile(excelName);
}
private static String getExcelRealPath(String excelName) {
String realPath = excelRequest.getRealPath("/UploadFile");
File excelFile = new File(realPath);
if(!excelFile.exists()) {
excelFile.mkdirs();
}
excelName = realPath+ "\\" + excelName+".txt";
return excelName;
}
private static void downloadFile(String strfileName) {
try {
// 获得ServletContext对象
if(excelFileNotFund(strfileName)) {
throw new IllegalArgumentException("File=["+strfileName+"] not fund file path");
}
// 取得文件的绝对路径
File excelFile = getExcelDownloadPath(strfileName);
putResponseStream(strfileName, excelFile);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
* @param strfileName : 文件名称
* @param excelName : 文件的相对路径或绝对路径
* @throws UnsupportedEncodingException
* @throws FileNotFoundException
* @throws IOException
*/
private static void putResponseStream(String strfileName, File excelName)
throws UnsupportedEncodingException, FileNotFoundException,
IOException {
strfileName = URLEncoder.encode(strfileName, "UTF-8");
excelResponse.setHeader("Content-disposition","attachment; filename=" + strfileName+".txt");
excelResponse.setContentLength((int) excelName.length());
// excelResponse.setContentType("application/x-download");
// excelResponse.setContentType("application/vnd.ms-Excel");
excelResponse.setContentType("application/x-msdownload");
byte[] buffer = new byte[4096];
int i = 0;
FileInputStream fis = new FileInputStream(excelName);
while ((i = fis.read(buffer,0,4096)) > 0) {
JspWriter out = null;
excelResponse.getOutputStream().write(buffer, 0, i);
}
excelResponse.flushBuffer();
}
//用传入参数的判断
private static boolean excelFileNotFund(String strfileName) {
return strfileName == null|| strfileName.equals("");
}
public static File getExcelDownloadPath(String excelName) {
String realPath = excelRequest.getRealPath("/UploadFile");
excelName = realPath+ "\\" + excelName+".txt";
File excelFile = new File(excelName);
return excelFile;
}
/**
* 输入当前WorkBook为下载临时文件记录
*
* @param excelName
*/
public void writerFileStream(String excelName,String resultStr) {
try {
fileOut = new FileOutputStream(excelName);
fileOut.write(resultStr.getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fileOut.flush();
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
//FileOutputStream fis = new FileOutputStream();
}
}