Java实现文件流下载文件,浏览器无反应,后台无错误!如何解决?
response.setCharacterEncoding("UTF-8");response.setContentType("application/octet-str...
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream");//APPLICATION/OCTET-STREAM
response.addHeader("Content-Disposition", "attachment; filename=\""+fileName+"\"");
//response.setContentLength((int)text.length());
byte[] b=new byte[100];
java.io.OutputStream os=null;
java.io.InputStream is=null;
try{
is=new java.io.ByteArrayInputStream(text.getBytes());
os=response.getOutputStream();
int len=0;
while((len=is.read(b))>0){
os.write(b,0,len);
}
response.setStatus( response.SC_OK );
//response.flushBuffer();
//os.flush();
//os.close();
is.close();
}catch(IOException e){
//response.reset();
e.printStackTrace();
}
注释中的方法我都试过了,没有用。请教各位达人指点一二!!! 展开
response.setContentType("application/octet-stream");//APPLICATION/OCTET-STREAM
response.addHeader("Content-Disposition", "attachment; filename=\""+fileName+"\"");
//response.setContentLength((int)text.length());
byte[] b=new byte[100];
java.io.OutputStream os=null;
java.io.InputStream is=null;
try{
is=new java.io.ByteArrayInputStream(text.getBytes());
os=response.getOutputStream();
int len=0;
while((len=is.read(b))>0){
os.write(b,0,len);
}
response.setStatus( response.SC_OK );
//response.flushBuffer();
//os.flush();
//os.close();
is.close();
}catch(IOException e){
//response.reset();
e.printStackTrace();
}
注释中的方法我都试过了,没有用。请教各位达人指点一二!!! 展开
1个回答
展开全部
ServletOutputStream out = response.getOutputStream();
byte[] outputByte = new byte[4096];
while(text.read(outputByte, 0, 4096) != -1)
{
out.write(outputByte, 0, 4096);
}
fileIn.close();
out.flush();
out.close();
byte[] outputByte = new byte[4096];
while(text.read(outputByte, 0, 4096) != -1)
{
out.write(outputByte, 0, 4096);
}
fileIn.close();
out.flush();
out.close();
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询