1. public HttpServletResponse download(String path, HttpServletResponse response) {
  2. try {
  3. // path是指欲下载的文件的路径。
  4. File file = new File(path);
  5. // 取得文件名。
  6. String filename = file.getName();
  7. // 取得文件的后缀名。
  8. String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
  9. // 以流的形式下载文件。
  10. InputStream fis = new BufferedInputStream(new FileInputStream(path));
  11. byte[] buffer = new byte[fis.available()];
  12. fis.read(buffer);
  13. fis.close();
  14. // 清空response
  15. response.reset();
  16. // 设置response的Header
  17. response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
  18. response.addHeader("Content-Length", "" + file.length());
  19. OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
  20. response.setContentType("application/octet-stream");
  21. toClient.write(buffer);
  22. toClient.flush();
  23. toClient.close();
  24. } catch (IOException ex) {
  25. ex.printStackTrace();
  26. }
  27. return response;
  28. }
  29. public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {
  30. // 下载本地文件
  31. String fileName = "Operator.doc".toString(); // 文件的默认保存名
  32. // 读到流中
  33. InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路径
  34. // 设置输出的格式
  35. response.reset();
  36. response.setContentType("bin");
  37. response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
  38. // 循环取出流中的数据
  39. byte[] b = new byte[100];
  40. int len;
  41. try {
  42. while ((len = inStream.read(b)) > 0)
  43. response.getOutputStream().write(b, 0, len);
  44. inStream.close();
  45. } catch (IOException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. public void downloadNet(HttpServletResponse response) throws MalformedURLException {
  50. // 下载网络文件
  51. int bytesum = 0;
  52. int byteread = 0;
  53. URL url = new URL("windine.blogdriver.com/logo.gif");
  54. try {
  55. URLConnection conn = url.openConnection();
  56. InputStream inStream = conn.getInputStream();
  57. FileOutputStream fs = new FileOutputStream("c:/abc.gif");
  58. byte[] buffer = new byte[1204];
  59. int length;
  60. while ((byteread = inStream.read(buffer)) != -1) {
  61. bytesum += byteread;
  62. System.out.println(bytesum);
  63. fs.write(buffer, 0, byteread);
  64. }
  65. } catch (FileNotFoundException e) {
  66. e.printStackTrace();
  67. } catch (IOException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. //支持在线打开文件的一种方式
  72. public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {
  73. File f = new File(filePath);
  74. if (!f.exists()) {
  75. response.sendError(404, "File not found!");
  76. return;
  77. }
  78. BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
  79. byte[] buf = new byte[1024];
  80. int len = 0;
  81. response.reset(); // 非常重要
  82. if (isOnLine) { // 在线打开方式
  83. URL u = new URL("file:///" + filePath);
  84. response.setContentType(u.openConnection().getContentType());
  85. response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
  86. // 文件名应该编码成UTF-8
  87. } else { // 纯下载方式
  88. response.setContentType("application/x-msdownload");
  89. response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
  90. }
  91. OutputStream out = response.getOutputStream();
  92. while ((len = br.read(buf)) > 0)
  93. out.write(buf, 0, len);
  94. br.close();
  95. out.close();
  96. }