当前位置:编程学习 > JAVA >>

getOutputStream() has already been called for this response java

public ModelAndView item_xml_download(HttpServletRequest request, HttpServletResponse response) throws Exception {
InputStream is = null;
OutputStream os = null;
List list = null;

try {

String month = request.getParameter("month");
// 得到你要下载的文件名(由前一个页面传过来)
String fileName = month + ".xml";
// 得到文件在服务器中的路径
String path = Constants.XMLDOWNLOAD_PATH + Constants.SYSTEM_FILE_SEPARATOR + fileName;// 这就是文件所在完整路径

service.createitem(month, Constants.XMLDOWNLOAD_PATH);
// 清空response 所有信息
response.reset();
// 设置IE浏览器内容类型 表示为 下载
response.setContentType("application/x-download;charset=UTF-8");
// 设置IE浏览器的 头
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);

// 从服务器上 读取文件
File file = new File(path);
response.setContentLength(Integer.valueOf(((Long) file.length()).toString()));
// 输入流 读取目标文件
is = new FileInputStream(file);

os = new BufferedOutputStream(response.getOutputStream());

byte[] buffer = new byte[4 * 1024];
int read = 0;
while ((read = is.read(buffer)) != -1) {
os.write(buffer, 0, read);
}

os.flush();
// 关闭输出流
os.close();
// 关闭输入流
is.close();

list = (List) service.queryList();

--------------------编程问答-------------------- 在使用poi碰到过这个问题,重复使用getOutputStream,后来改用了jxl就好了
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,