Servlet实现文件下载
程序员文章站
2022-05-24 20:51:15
...
1.servlet实现文件下载需要两个jar包
commons-fileupload-1.2.1.jar
commons-io-1.4.jar
2.后台代码编写
获得文件下载列表,并进行转发,提供到前台显示
package com.action;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
String path = getServletContext().getRealPath("/download");
//获取文件夹
File file = new File(path);
//获取文件夹下所有的名称
String[] fileNames = file.list();
req.setAttribute("fileNames", fileNames);
req.getRequestDispatcher("/downloadList.jsp").forward(req, resp);
}
}
3.前台页面边编写
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>
<base href="<%=basePath%>">
<title>My JSP 'downloadList.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
for (String fileName : (String[])request.getAttribute("fileNames")){
%>
<a href="do?fileName=<%=fileName%>"><%=fileName%></a>
<br>
<% }
%>
</body>
</html>
4.后台代码编写,进行文件下载
package com.action;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.mail.iap.Response;
public class DoDownloadServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//将要下载的文件写入输入流
//获取要下载的文件名
String fileName = req.getParameter("fileName");
fileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8");
//获取服务器upload路径
String basePath = getServletContext().getRealPath("/download");
FileInputStream fis = new FileInputStream(basePath+"/"+fileName);
System.out.println(basePath+"/"+fileName);
//通过response的outputstream向前台用户路径下输出
//告诉浏览器 以下载的方式打开
resp.setHeader("content-disposition","attachment;fileName="+URLEncoder.encode(fileName, "UTF-8"));
OutputStream fos = resp.getOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len=fis.read(buffer))!=-1){
fos.write(buffer, 0, len);
}
fis.close();
fos.close();
}
}
上一篇: 验证码的简单实现
下一篇: SpringTask实现定时任务