java文件下载及名称乱码问题
2012-02-23 14:09
399 查看
package com.wind.dairysystem.controller;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.filechooser.FileSystemView;
import com.wind.dairysystem.dao.DBUtil;
public class ImportUserDairyToExcel extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
resp.setCharacterEncoding("utf-8");
//S流下载
resp.setContentType("application/octet-stream;charset=UTF-8");
//E流下载
String time = req.getParameter("time");
FileSystemView fsv = FileSystemView.getFileSystemView();
String homePath = fsv.getHomeDirectory().getPath();
String fileName = "";
if(time != null)
fileName = "WWPI_成员日志统计结果."+time+".xls";
else
fileName = "WWPI_成员日志统计结果.xls";
String retuName = new String(fileName.getBytes("utf-8"),"ISO8859_1");
fileName = homePath +"\\"+ fileName;
System.out.println(fileName);
File excel = new File(fileName);
String year = time.substring(0, 4);
String mouth = time.substring(5,7);
DBUtil db = new DBUtil();
try {
db.importUserDairyToExcel(excel, year, mouth);
}catch(Exception e){
e.printStackTrace();
}
//STest
// 写流文件到前端浏览器
ServletOutputStream out = resp.getOutputStream();
System.out.println("retuName:" + retuName);
resp.reset();
resp.addHeader("Content-Disposition", "attachment; filename=" + retuName);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(fileName));
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (IOException e) {
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
excel.delete();
//ETest
//resp.getWriter().print("生成成功!");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(req, resp);
}
}
ext前端:
window.open("importUserDairyToExcel?time="+time);
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.filechooser.FileSystemView;
import com.wind.dairysystem.dao.DBUtil;
public class ImportUserDairyToExcel extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
resp.setCharacterEncoding("utf-8");
//S流下载
resp.setContentType("application/octet-stream;charset=UTF-8");
//E流下载
String time = req.getParameter("time");
FileSystemView fsv = FileSystemView.getFileSystemView();
String homePath = fsv.getHomeDirectory().getPath();
String fileName = "";
if(time != null)
fileName = "WWPI_成员日志统计结果."+time+".xls";
else
fileName = "WWPI_成员日志统计结果.xls";
String retuName = new String(fileName.getBytes("utf-8"),"ISO8859_1");
fileName = homePath +"\\"+ fileName;
System.out.println(fileName);
File excel = new File(fileName);
String year = time.substring(0, 4);
String mouth = time.substring(5,7);
DBUtil db = new DBUtil();
try {
db.importUserDairyToExcel(excel, year, mouth);
}catch(Exception e){
e.printStackTrace();
}
//STest
// 写流文件到前端浏览器
ServletOutputStream out = resp.getOutputStream();
System.out.println("retuName:" + retuName);
resp.reset();
resp.addHeader("Content-Disposition", "attachment; filename=" + retuName);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(fileName));
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (IOException e) {
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
excel.delete();
//ETest
//resp.getWriter().print("生成成功!");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(req, resp);
}
}
ext前端:
window.open("importUserDairyToExcel?time="+time);
相关文章推荐
- java 下载文件,中文文件名称乱码的问题
- java下载文件文件名针对不同浏览器乱码,文件名中存在括号等乱码问题
- Java web中不同浏览器间导出Excel文件名称乱码问题解决方案
- 关于java文件下载文件名乱码问题解决方案
- Java代码解决下载文件乱码问题
- java下载文件名称各种浏览器兼容问题
- JAVA文件下载中文名称乱码解决方案
- 解决Java下载文件,文件名包含中文在IE浏览器乱码问题
- Java下载中文名文件乱码问题
- (java)解决文件是中文名打包成zip文件名称乱码的问题。
- Java下载文件,文件名中文乱码问题
- java实现ftp文件上传下载,解决慢,中文乱码,多个文件下载等问题
- java 下载文件解决文件名乱码问题
- JAVA 文件下载乱码问题解决办法
- 整理java文件下载(中文乱码、火狐浏览器乱码、空格变+号、文件名包含空格时文件名可能显示不全等问题)
- java 文件下载文件名称乱码
- jsp页面java+js实现文件下载(并附带解决下载文件名乱码问题-火狐浏览器正常访问)
- 【javaweb:servlet】从服务端下载中文名称文件应该注意的问题!!!
- 下载文件,ie文件名称乱码问题
- Java中FTP文件上传下载中文文件乱码问题