上次实现了文件上传到本地磁盘并插入到了数据库 ,接下来就让我们将上传的资源实现下载的功能:
在显示所有文件的JSP中:
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>文件下载页面</title>
- </head>
- <body>
- <table>
- <thead>
- <tr>
- <th>id</th><th>name</th><th>fileName</th><th>operator</th>
- </tr>
- </thead>
- <tbody>
- <c:forEach var="files" items="${list }">
- <tr>
- <td>
- ${files.id}
- </td>
- <td>
- ${files.name}
- </td>
- <td>
- ${files.fileName}
- </td>
- <td>
- <a href="${pageContext.request.contextPath}/download.do?filePath=${files.filePath}&fileName=${files.fileName}">下载</a>
- </td>
- </tr>
- </c:forEach>
- </tbody>
- </table>
- </body>
- </html>
实现浏览的servlet中:
- package cn.csdn.web.servlet;
- import java.io.IOException;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import cn.csdn.web.dao.UploadDao;
- import cn.csdn.web.dao.UploadDaoImpl;
- import cn.csdn.web.domain.Upload;
- public class ListFilesServlet extends HttpServlet {
- UploadDao uDao = new UploadDaoImpl();
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- request.setCharacterEncoding("utf-8");
- try {
- List<Upload> list = uDao.checkAll();
- List<Upload> entities = new ArrayList();
- Map map = new HashMap();
- for(Upload entity : list){
- String fileName = entity.getFileName();
- String realName = fileName.substring(fileName.lastIndexOf("_")+1);
- entity.setFileName(realName);
- System.out.println("-----"+entity.getFilePath());
- entities.add(entity);
- }
- request.setAttribute("list", entities);
- request.getRequestDispatcher("/listfiles.jsp").forward(request, response);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- this.doGet(request, response);
- }
- }
下面是实现下载功能的servlet:
- package cn.csdn.web.servlet;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.net.URLEncoder;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class DownLoadServlet extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String filePath = request.getParameter("filePath");//拿到请求中的文件路径
- String fileName = filePath.substring(filePath.lastIndexOf("_")+1);//得到文件的真实名字
- filePath = new String(filePath.getBytes("iso8859-1"),"utf-8");
- fileName = new String(fileName.getBytes("iso8859-1"),"utf-8");
- fileName = URLEncoder.encode(fileName,"utf-8");
- File file = new File(filePath);
- if(!file.exists()){
- request.setAttribute("message","要下载的文件不存在");
- request.getRequestDispatcher("/message.jsp").forward(request, response);
- }else{
- response.setHeader("content-disposition", "attachment;filename="+fileName);
- FileInputStream fis = new FileInputStream(file);
- java.io.OutputStream os = response.getOutputStream();
- byte[] buffer = new byte[1024];
- int len = 0;
- while((len=fis.read(buffer))!=-1){
- os.write(buffer, 0, len);
- }
- fis.close();
- }
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- this.doGet(request, response);
- }
- }