JSP文件上传下载-----下载篇

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

在显示所有文件的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);
}


}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值