67,538
社区成员
发帖
与我相关
我的任务
分享
public void DownExcelFile(HttpServletResponse response,String fileDownPath) {
File file = new File(fileDownPath);
response.setContentType("text/plain;charset=utf-8");
if (file.exists()) {
try {
log.info("enter try");
// 要用servlet 来打开一个 EXCEL 文档,需要将 response 对象中 header 的 contentType 设置成"application/x-msexcel"。
response.setContentType("application/x-msexcel");
// 保存文件名称
fileDownPath = fileDownPath.substring(fileDownPath.lastIndexOf("/") + 1);
// 处理中文文件名
// fileName = new String(fileName.getBytes("GB2312"), "utf-8");
//servlet中,要在 header中设置下载方式
response.setHeader("Content-Disposition","attachment; filename=" + fileDownPath);
//FileInputStream输入流
//FileInputStream bis = new FileInputStream(file);
//缓冲流(BufferedStream)可以一次读写一批数据,,缓冲流(Buffered Stream)大大提高了I/O的性能。
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
//OutputStream输出流
OutputStream bos = response.getOutputStream();
byte[] buff = new byte[1024];
int readCount = 0;
//每次从文件流中读1024个字节到缓冲里。
readCount = bis.read(buff);
while (readCount != -1) {
//把缓冲里的数据写入浏览器
bos.write(buff, 0, readCount);
readCount = bis.read(buff);
}
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
response.setStatus(HttpServletResponse.SC_OK);
response.flushBuffer();
response.getWriter().close();
} catch (Exception e) {
log.error("DownExcelFile error:" + e.getMessage());
}
}
}
public void DownExcelFile(HttpServletResponse response,String fileDownPath) {
LZ要清楚。保存路径是弹出保存对话框的时候自己选择的。。。只能弹出保存文件名。。。
/**
* 下载服务器中的excel文件
* @param filePath
* @param filename
* @param response
* @throws Exception
*/
public static void download(File filePath, String filename, HttpServletResponse response) throws Exception{
// TODO Auto-generated method stub
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
//File uploadFile = new File(filePath);
File uploadFile = filePath;
fis = new FileInputStream(uploadFile);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
filename = URLEncoder.encode(filename, "GBK");
//弹出下载对话框的关键代码,filename为显示的保存文件名称
response.setContentType("application/x-download");
response.setHeader("Content-Disposition","attachment;filename="+ filename);
int bytesRead = 0;
//都是用输入流进行先读,然后用输出流去写,用的是缓冲输入输出流
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
try{
bos.write(buffer, 0, bytesRead);
}catch(Exception e){}
}
try{bos.flush();}catch(Exception e){}
try{bos.close();}catch(Exception e){}
try{bis.close();}catch(Exception e){}
try{fos.close();}catch(Exception e){}
try{fis.close();}catch(Exception e){}
}
String filepath = "e:\\文件名.xls";
FileInputStream fis = new FileInputStream(new File(filepath));
OutputStream os = response.getOutputStream();
response.setHeader("Content-disposition","attachment;filename=" +
URLEncoder.encode("文件名.xls", "utf-8"));
int i = 0;
byte[] b = new byte[8192];
while ((i = fis.read(b, 0, 8192)) != -1)
{
os.write(b, 0, i);
}
os.flush();
fis.close();
return null;