getOutputStream() has already been called for this response java getOutputStream()
FileInputStreamfi=newFileInputStream(dlFileName);byte[]b=newbyte[fi.available()];for(...
FileInputStream fi = new FileInputStream(dlFileName);
byte[] b = new byte[fi.available()];
for (int i = 0; i < b.length; i++){
b[i] = (byte)fi.read();
}
response.setContentType("application/octet-stream; charset=Shift_JIS");
response.setHeader("Content-Disposition", "attachment;filename=\"" + dlFileName + "\"");
response.setContentLength(b.length);
try{
ServletOutputStream os = response.getOutputStream();
os.write(b);
os.close();
}catch(Exception e){
} finally {
fi.close();
}
java中用response.getOutputStream()实现文件下载。可是报错
getOutputStream() has already been called for this response
上网查了查,用JSP实现的话,据说在最后加上out.clear();out=pageContext.pushBody();就能解决这个问题。可我的下载是写在java中的,应该怎么解决呢? 展开
byte[] b = new byte[fi.available()];
for (int i = 0; i < b.length; i++){
b[i] = (byte)fi.read();
}
response.setContentType("application/octet-stream; charset=Shift_JIS");
response.setHeader("Content-Disposition", "attachment;filename=\"" + dlFileName + "\"");
response.setContentLength(b.length);
try{
ServletOutputStream os = response.getOutputStream();
os.write(b);
os.close();
}catch(Exception e){
} finally {
fi.close();
}
java中用response.getOutputStream()实现文件下载。可是报错
getOutputStream() has already been called for this response
上网查了查,用JSP实现的话,据说在最后加上out.clear();out=pageContext.pushBody();就能解决这个问题。可我的下载是写在java中的,应该怎么解决呢? 展开
展开全部
response.setCharacterEncoding("UTF-8");
response.setContentType("application/zip; charset=GBK");
InputStream inStream = new FileInputStream(new File(filePath));// 文件的存放路径
String fileName = filePath.substring(filePath.lastIndexOf("\\") + 1);
response.reset();
response.addHeader("Content-Disposition", "attachment; filename=\""
+ fileName + "\"");
byte[] b = new byte[1024];
int len;
while ((len = inStream.read(b)) != -1)
response.getOutputStream().write(b, 0, len);//直接弹出下载框,
response.setContentType("application/zip; charset=GBK");
InputStream inStream = new FileInputStream(new File(filePath));// 文件的存放路径
String fileName = filePath.substring(filePath.lastIndexOf("\\") + 1);
response.reset();
response.addHeader("Content-Disposition", "attachment; filename=\""
+ fileName + "\"");
byte[] b = new byte[1024];
int len;
while ((len = inStream.read(b)) != -1)
response.getOutputStream().write(b, 0, len);//直接弹出下载框,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
那就创建一个out吧PrintWriter out = response.getWriter().
追问
这种情况getOutputStream();不能用么?下载的是CSV文件,里面还有汉字,PrintWriter没问题吧...
追答
response.setContentType用这个设一下编码,gbk,或者utf-8,根据自己情况而定,不设的话很可能会有乱码
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
试一试 楼上xinfei0804 说的吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询