public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/x-download; charset=utf-8");//1
String imageDir = this.getServletContext().getRealPath("uploadfile/file")+"\\";
response.setHeader("Content-disposition", "attachment; filename="
+URLEncoder.encode("文件下载.rar","utf-8"));//2
ServletOutputStream outStream = response.getOutputStream();
java.io.BufferedOutputStream bos=new java.io.BufferedOutputStream(outStream);
java.io.File srcFile = new java.io.File (imageDir+"teachsource1247291616515.rar");
java.io.FileInputStream stream = new java.io.FileInputStream(srcFile);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1)
{
bos.write(buffer, 0, bytesRead);
}
bos.close();
outStream.close();
stream.close();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/x-download; charset=utf-8");//1
String imageDir = this.getServletContext().getRealPath("uploadfile/file")+"\\";
response.setHeader("Content-disposition", "attachment; filename="
+URLEncoder.encode("文件下载.rar","utf-8"));//2
ServletOutputStream outStream = response.getOutputStream();
java.io.BufferedOutputStream bos=new java.io.BufferedOutputStream(outStream);
java.io.File srcFile = new java.io.File (imageDir+"teachsource1247291616515.rar");
java.io.FileInputStream stream = new java.io.FileInputStream(srcFile);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1)
{
bos.write(buffer, 0, bytesRead);
}
bos.close();
outStream.close();
stream.close();
}
就两个要注意的地方。
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
蕃薯耀分享 2013年4月25日 16:10:29 星期四