如何解决Servlet中 response对象 getoutputstream 和 getwrite 的矛盾冲突 10
应用Ajax调用Servlet,在Servlet中从服务器下载文件,先用OutputStreamo=response.getOutputStream();建立输出对象,向...
应用Ajax调用Servlet,在Servlet中从服务器下载文件,先用
OutputStream o=response.getOutputStream(); 建立输出对象,向客户端写文件
然后在建立:PrintWriter out = response.getWriter(); 用out 向jsp回应请求。
出错:getOutputStream() has already been called for this response !!
整段代码:
OutputStream o=response.getOutputStream();
BufferedOutputStream bos=new BufferedOutputStream(o);
byte b[]=new byte[500];
response.setHeader("Content-disposition","attachment;filename="+new String(str.getBytes("gb2312"),"iso8859-1")); //客户使用保存文件的对话框
long fileLength=fileLoad.length(); //通知客户文件的长度:
String length=String.valueOf(fileLength);
response.setHeader("Content_Length",length);
FileInputStream in=new FileInputStream(fileLoad); //读取文件,并发送给客户下载:
int n=0;
while((n=in.read(b))!=-1)
{ bos.write(b,0,n); }
bos.close();
o.flush();
o.close();
in.close();
}catch(Exception e){System.out.print(e); }
// response.reset();
PrintWriter out = response.getWriter();
out.print(back); // 送h1.js 客户端
out.flush();
out.close();
return; 展开
OutputStream o=response.getOutputStream(); 建立输出对象,向客户端写文件
然后在建立:PrintWriter out = response.getWriter(); 用out 向jsp回应请求。
出错:getOutputStream() has already been called for this response !!
整段代码:
OutputStream o=response.getOutputStream();
BufferedOutputStream bos=new BufferedOutputStream(o);
byte b[]=new byte[500];
response.setHeader("Content-disposition","attachment;filename="+new String(str.getBytes("gb2312"),"iso8859-1")); //客户使用保存文件的对话框
long fileLength=fileLoad.length(); //通知客户文件的长度:
String length=String.valueOf(fileLength);
response.setHeader("Content_Length",length);
FileInputStream in=new FileInputStream(fileLoad); //读取文件,并发送给客户下载:
int n=0;
while((n=in.read(b))!=-1)
{ bos.write(b,0,n); }
bos.close();
o.flush();
o.close();
in.close();
}catch(Exception e){System.out.print(e); }
// response.reset();
PrintWriter out = response.getWriter();
out.print(back); // 送h1.js 客户端
out.flush();
out.close();
return; 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询