<mime-mapping> <extension>zip</extension> <mime-type>application/zip</mime-type> </mime-mapping>
,那么如果你在你的项目下有个foo.zip,那么在浏览器里直接输入 http://localhost:8080/foo.zip ,这时tomcat就会执行上述的两个步骤。浏览器会得到头信息:有文件要下载,文件名是foo.zip,文件类型是application/zip,即可以使用支持zip格式文件的应用程序打开等头信息。
response.setContentType("application/msword"); response.setHeader("Content-Disposition", "attachment;filename=\"" + new String("测试.doc".getBytes("GBK"),"iso-8859-1") + "\"");
FileInputStream in = new FileInputStream("c:/测试.doc"); byte[] bytes = new byte[1024]; while((m=in.read(bytes))!=-1){ response.getOutputStream().write(bytes, 0, m); } in.close();
那么访问http://localhost:8080/d.jsp 的时候就会下载c:/测试.doc了。
response.setHeader("Accept-Ranges", "bytes");
以了解细节
struts2 public InputStream getInputStream() 下载
来了解详情