当前位置: 技术问答>java相关
用jspSmartUpload下载文件时的一个问题
来源: 互联网 发布时间:2015-06-26
本文导语: 当下载文本文件时,不能下载文件,而是浏览器打开了这个文本文件 请教怎么才能下载这个文本文件 | int buffsize = 1024*2; response.setContentType( "application/none" ); response.setHeader("Conte...
当下载文本文件时,不能下载文件,而是浏览器打开了这个文本文件
请教怎么才能下载这个文本文件
请教怎么才能下载这个文本文件
|
int buffsize = 1024*2;
response.setContentType( "application/none" );
response.setHeader("Content-disposition","attachment; filename=" + strFileName );
try{
URL url=new URL(/tech-qa-java/strFileURL/index.html);
BufferedInputStream bis = new BufferedInputStream(url.openStream());
BufferedOutputStream bos = new BufferedOutputStream( response.getOutputStream());
byte[] buff = new byte[buffsize];
int bytesRead;
while (-1!=(bytesRead=(bis.read(buff,0,buff.length)))){
bos.write(buff,0,bytesRead);
}
if (bis!=null){
bis.close();
}
if (bos!=null){
bos.close();
}
}catch(IOException ioe){
System.out.println("IOException at download file!"+ioe.getMessage());
ioe.printStackTrace();
}
response.setContentType( "application/none" );
response.setHeader("Content-disposition","attachment; filename=" + strFileName );
try{
URL url=new URL(/tech-qa-java/strFileURL/index.html);
BufferedInputStream bis = new BufferedInputStream(url.openStream());
BufferedOutputStream bos = new BufferedOutputStream( response.getOutputStream());
byte[] buff = new byte[buffsize];
int bytesRead;
while (-1!=(bytesRead=(bis.read(buff,0,buff.length)))){
bos.write(buff,0,bytesRead);
}
if (bis!=null){
bis.close();
}
if (bos!=null){
bos.close();
}
}catch(IOException ioe){
System.out.println("IOException at download file!"+ioe.getMessage());
ioe.printStackTrace();
}
|
在生成文件的那个程序里加上
response.setHeader("Content-Disposition","attachment;fileName="" + fileName + """);
response.setHeader("Content-Disposition","attachment;fileName="" + fileName + """);