首页 > 代码库 > jsp中文件下载的实现
jsp中文件下载的实现
方式一:采用RequestDispatcher进行
package cn.jbit.download.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DownloadServlet extends HttpServlet { private static final long serialVersionUID = 6765085208899952414L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filedownload = "/upload/helloworld.jar";//即将下载的文件的相对路径 String filedisplay = "helloworld.jar";//下载文件时显示的文件保存名称 response.setContentType("application/x-download");//设置为下载application/x-download //response.setContentType("application/x-msdownload");//设置为下载application/x-msdownload //response.setContentType("application/octet-stream");//设置为下载application/octet-stream response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay); try { RequestDispatcher rd = request.getRequestDispatcher(filedownload); if(rd != null) { rd.forward(request,response); } response.flushBuffer(); } catch (Exception e) { e.printStackTrace(); } } }方式二:采用文件流输出的方式下载
package cn.jbit.download.servlet; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DownloadOfIOServlet extends HttpServlet { private static final long serialVersionUID = 6765085208899952414L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String basePath = request.getSession().getServletContext().getRealPath("/upload"); //System.out.println(basePath); String filedisplay = "helloworld.jar"; String filedownload = basePath + File.separator + "helloworld.jar"; System.out.println("----------------------"+filedownload); response.setContentType("applicaiton/x-download"); response.addHeader("Content-Disposition", "attachment;filename="+filedisplay); InputStream is = null; OutputStream os = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; is = new FileInputStream(new File(filedownload)); bis = new BufferedInputStream(is); os = response.getOutputStream(); bos = new BufferedOutputStream(os); byte[] b = new byte[1024]; int len = 0; while((len = bis.read(b)) != -1){ bos.write(b,0,len); } bis.close(); is.close(); bos.close(); os.close(); } }方式三:网页上做超级链接(不推荐使用,样服务器上的目录资源会直接暴露给最终用户)
<a href=http://www.mamicode.com/"/DownloadFile/upload/helloworld.jar">helloworld.jarjsp页面代码:
<%@ 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=http://www.mamicode.com/"">>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。