jsp上传下载下篇

上次实现了文件上传到本地磁盘并插入到了数据库 ,接下来就让我们将上传的资源实现下载的功能:

在显示所有文件的JSP中:

[html]  view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <title>文件下载页面</title>  
  11.   </head>  
  12.   <body>  
  13.     <table>  
  14.     <thead>  
  15.     <tr>  
  16.     <th>id</th><th>name</th><th>fileName</th><th>operator</th>  
  17.     </tr>  
  18.     </thead>  
  19.     <tbody>  
  20.     <c:forEach var="files" items="${list }">  
  21.     <tr>  
  22.     <td>  
  23.     ${files.id}  
  24.     </td>  
  25.     <td>  
  26.     ${files.name}  
  27.     </td>  
  28.     <td>  
  29.     ${files.fileName}  
  30.     </td>  
  31.     <td>  
  32.     <a href="${pageContext.request.contextPath}/download.do?filePath=${files.filePath}&fileName=${files.fileName}">下载</a>  
  33.     </td>  
  34.     </tr>  
  35.     </c:forEach>  
  36.     </tbody>  
  37.     </table>  
  38.   </body>  
  39. </html>  


实现浏览的servlet中:


[html]  view plain copy
  1. package cn.csdn.web.servlet;  
  2. import java.io.IOException;  
  3. import java.sql.SQLException;  
  4. import java.util.ArrayList;  
  5. import java.util.HashMap;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8.   
  9. import javax.servlet.ServletException;  
  10. import javax.servlet.http.HttpServlet;  
  11. import javax.servlet.http.HttpServletRequest;  
  12. import javax.servlet.http.HttpServletResponse;  
  13. import cn.csdn.web.dao.UploadDao;  
  14. import cn.csdn.web.dao.UploadDaoImpl;  
  15. import cn.csdn.web.domain.Upload;  
  16. public class ListFilesServlet extends HttpServlet {  
  17. UploadDao uDao = new UploadDaoImpl();  
  18. public void doGet(HttpServletRequest request, HttpServletResponse response)  
  19. throws ServletException, IOException {  
  20. request.setCharacterEncoding("utf-8");  
  21.   
  22. try {  
  23. List<Upload> list = uDao.checkAll();  
  24. List<Upload> entities = new ArrayList();  
  25. Map map = new HashMap();  
  26. for(Upload entity : list){  
  27. String fileName = entity.getFileName();  
  28. String realName = fileName.substring(fileName.lastIndexOf("_")+1);  
  29. entity.setFileName(realName);  
  30. System.out.println("-----"+entity.getFilePath());  
  31. entities.add(entity);  
  32. }  
  33. request.setAttribute("list", entities);  
  34.   
  35. request.getRequestDispatcher("/listfiles.jsp").forward(request, response);  
  36. } catch (SQLException e) {  
  37. e.printStackTrace();  
  38. }  
  39. }  
  40. public void doPost(HttpServletRequest request, HttpServletResponse response)  
  41. throws ServletException, IOException {  
  42. this.doGet(request, response);  
  43. }  
  44. }  



下面是实现下载功能的servlet:


[html]  view plain copy
  1. package cn.csdn.web.servlet;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.IOException;  
  5. import java.net.URLEncoder;  
  6.   
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.http.HttpServlet;  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11. public class DownLoadServlet extends HttpServlet {  
  12. public void doGet(HttpServletRequest request, HttpServletResponse response)  
  13. throws ServletException, IOException {  
  14.   
  15. String filePath = request.getParameter("filePath");//拿到请求中的文件路径  
  16. String fileName = filePath.substring(filePath.lastIndexOf("_")+1);//得到文件的真实名字  
  17.   
  18. filePath = new String(filePath.getBytes("iso8859-1"),"utf-8");  
  19. fileName = new String(fileName.getBytes("iso8859-1"),"utf-8");  
  20. fileName = URLEncoder.encode(fileName,"utf-8");  
  21. File file = new File(filePath);  
  22.   
  23. if(!file.exists()){  
  24. request.setAttribute("message","要下载的文件不存在");  
  25. request.getRequestDispatcher("/message.jsp").forward(request, response);  
  26. }else{  
  27. response.setHeader("content-disposition", "attachment;filename="+fileName);  
  28.   
  29. FileInputStream fis = new FileInputStream(file);  
  30. java.io.OutputStream os = response.getOutputStream();  
  31.   
  32. byte[] buffer = new byte[1024];  
  33. int len = 0;  
  34. while((len=fis.read(buffer))!=-1){  
  35. os.write(buffer, 0, len);  
  36. }  
  37.   
  38. fis.close();  
  39. }  
  40. }  
  41.   
  42.   
  43. public void doPost(HttpServletRequest request, HttpServletResponse response)  
  44. throws ServletException, IOException {  
  45. this.doGet(request, response);  
  46. }  
  47.   
  48.   
  49. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值