Skip to content

Instantly share code, notes, and snippets.

@wdfx100
Last active December 16, 2015 07:59
Show Gist options
  • Save wdfx100/5402945 to your computer and use it in GitHub Desktop.
Save wdfx100/5402945 to your computer and use it in GitHub Desktop.
#文件下载#
//注意要点
//1.文件格式的设置
//2.文件名中中文的设置
//3.文件大小的设置
//4.设置一个相应的头 Servlet download file:搜 键:文件的描述 值:对应的文件
response.setContentType("application/pdf");//设置文件格式
String fileName = "第一章.pdf";
File file = new File("I:/test/upload","第一章.pdf");//下载文件的路径及名称
response.setContentLength(new Long(file.length()).intValue());//进度条的操作,提示完成进度
response.setHeader("Content-Disposition",
"attachment;filename="+new String(fileName.getBytes("UTF-8"),"ISO8859-1"));//中英文的切换
InputStream inputStream = new FileInputStream(file);//输入到客户
OutputStream oStream = response.getOutputStream();//从内存输出
BufferedInputStream bis = new BufferedInputStream(inputStream);
BufferedOutputStream bos =new BufferedOutputStream(oStream);
int len = -1;
byte[] buffer = new byte[1024];
while((len=bis.read(buffer))!=-1){
bos.write(buffer, 0, len);
}
bos.flush();
bis.close();
bos.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment