Created
September 26, 2013 10:08
-
-
Save liutian/6712211 to your computer and use it in GitHub Desktop.
java servlet downLoadFile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//重设response | |
response.reset(); | |
//设置要下载的文件content-Type | |
response.setContentType("application/x-msdownload"); | |
//设置下载文件的名字 | |
String fileName = (new File(filePath)).getName(); | |
response.addHeader("Content-Disposition", "attachment; filename="+fileName); | |
//获取文件输入流 | |
FileInputStream fileIn = new FileInputStream(filePath); | |
//获得输出流 | |
OutputStream servletOS = response.getOutputStream(); | |
//往输出流中写入数据 | |
byte[] dataBtye = new byte[1024000]; | |
int len = -1; | |
while((len = fileIn.read(dataBtye)) != -1){ | |
servletOS.write(dataBtye, 0, len); | |
} | |
servletOS.flush(); | |
servletOS.close(); | |
fileIn.close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment