java web文件下载功能实现 (转)

时间:2023-03-08 17:45:57

http://blog.****.net/longshengguoji/article/details/39433307

需求:实现一个具有文件下载功能的网页,主要下载压缩包和图片

两种实现方法:

一:通过超链接实现下载

在HTML网页中,通过超链接链接到要下载的文件的地址
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <h1>通过链接下载文件</h1>
  9. <a href="/day06/download/cors.zip">压缩包</a>
  10. <a href="/day06/download/1.png">图片</a>
  11. </body>
  12. </html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>通过链接下载文件</h1>
<a href="/day06/download/cors.zip">压缩包</a>
<a href="/day06/download/1.png">图片</a>
</body>
</html>

其中day06/download是文档路径,本实例的程序结构如下:

java web文件下载功能实现 (转)
程序运行后,可以通过单击需要下载文档实现下载
java web文件下载功能实现 (转)
但是这里会出现一个问题,就是单击下载压缩包的时候会弹出下载页面,但是下载图片的时候浏览器就直接打开了图片,没有下载。
java web文件下载功能实现 (转)
    这是因为通过超链接下载文件时,如果浏览器可以识别该文件格式,浏览器就会直接打开。只有浏览器不能识别该文件格式的时候,才会实现下载。因此利用第二种方法实现下载功能。

二:通过Servlet程序实现下载

    通过Servlet下载文件的原理是通过servlet读取目标程序,将资源返回客户端。
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <h1>通过链接下载文件</h1>
  9. <a href="/day06/download/cors.zip">压缩包</a>
  10. <a href="/day06/download/1.png">图片</a>
  11. <h1>通过servlet程序下载文件</h1>
  12. <a href="/day06/ServletDownload?filename=cors.zip">压缩包</a>
  13. <a href="/day06/ServletDownload?filename=1.png">图片</a>
  14. </body>
  15. </html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>通过链接下载文件</h1>
<a href="/day06/download/cors.zip">压缩包</a>
<a href="/day06/download/1.png">图片</a>
<h1>通过servlet程序下载文件</h1>
<a href="/day06/ServletDownload?filename=cors.zip">压缩包</a>
<a href="/day06/ServletDownload?filename=1.png">图片</a>
</body>
</html>

其中,/day06/ServletDownload 是servlet程序的映射路径

然后新建一个servlet,名称为ServletDownload,URL映射为/ServletDownload
java web文件下载功能实现 (转)
添加代码如下:
  1. package com.lsgjzhuwei.servlet.response;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.annotation.WebServlet;
  9. import javax.servlet.http.HttpServlet;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. /**
  13. * Servlet implementation class ServletDownload
  14. */
  15. @WebServlet(asyncSupported = true, urlPatterns = { "/ServletDownload" })
  16. public class ServletDownload extends HttpServlet {
  17. private static final long serialVersionUID = 1L;
  18. /**
  19. * @see HttpServlet#HttpServlet()
  20. */
  21. public ServletDownload() {
  22. super();
  23. // TODO Auto-generated constructor stub
  24. }
  25. /**
  26. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  27. */
  28. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  29. // TODO Auto-generated method stub
  30. //获得请求文件名
  31. String filename = request.getParameter("filename");
  32. System.out.println(filename);
  33. //设置文件MIME类型
  34. response.setContentType(getServletContext().getMimeType(filename));
  35. //设置Content-Disposition
  36. response.setHeader("Content-Disposition", "attachment;filename="+filename);
  37. //读取目标文件,通过response将目标文件写到客户端
  38. //获取目标文件的绝对路径
  39. String fullFileName = getServletContext().getRealPath("/download/" + filename);
  40. //System.out.println(fullFileName);
  41. //读取文件
  42. InputStream in = new FileInputStream(fullFileName);
  43. OutputStream out = response.getOutputStream();
  44. //写文件
  45. int b;
  46. while((b=in.read())!= -1)
  47. {
  48. out.write(b);
  49. }
  50. in.close();
  51. out.close();
  52. }
  53. /**
  54. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  55. */
  56. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  57. // TODO Auto-generated method stub
  58. }
  59. }
package com.lsgjzhuwei.servlet.response;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class ServletDownload
*/
@WebServlet(asyncSupported = true, urlPatterns = { "/ServletDownload" })
public class ServletDownload extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public ServletDownload() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub //获得请求文件名
String filename = request.getParameter("filename");
System.out.println(filename); //设置文件MIME类型
response.setContentType(getServletContext().getMimeType(filename));
//设置Content-Disposition
response.setHeader("Content-Disposition", "attachment;filename="+filename);
//读取目标文件,通过response将目标文件写到客户端
//获取目标文件的绝对路径
String fullFileName = getServletContext().getRealPath("/download/" + filename);
//System.out.println(fullFileName);
//读取文件
InputStream in = new FileInputStream(fullFileName);
OutputStream out = response.getOutputStream(); //写文件
int b;
while((b=in.read())!= -1)
{
out.write(b);
} in.close();
out.close();
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
} }

重启tomcat服务器,即可实现对压缩包和对图片的下载。

java web文件下载功能实现 (转)
java web文件下载功能实现 (转)