java实现文件下载的两种方式-文件下载

  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.webp");
  54. try {
  55. URLConnection conn = url.openConnection();
  56. InputStream inStream = conn.getInputStream();
  57. FileOutputStream fs = new FileOutputStream("c:/abc.webp");
  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. }

//支持在线打开文件的一种方式


  1. public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {
  2. File f = new File(filePath);
  3. if (!f.exists()) {
  4. response.sendError(404, "File not found!");
  5. return;
  6. }
  7. BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
  8. byte[] buf = new byte[1024];
  9. int len = 0;
  10. response.reset(); // 非常重要
  11. if (isOnLine) { // 在线打开方式
  12. URL u = new URL("file:///" + filePath);
  13. response.setContentType(u.openConnection().getContentType());
  14. response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
  15. // 文件名应该编码成UTF-8
  16. } else { // 纯下载方式
  17. response.setContentType("application/x-msdownload");
  18. response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
  19. }
  20. OutputStream out = response.getOutputStream();
  21. while ((len = br.read(buf)) > 0)
  22. out.write(buf, 0, len);
  23. br.close();
  24. out.close();

java实现文件下载的两种方式

推荐阅读