java web文件下载功能实现 (转)
http://blog.csdn.net/longshengguoji/article/details/39433307
需求:实现一个具有文件下载功能的网页,主要下载压缩包和图片
两种实现方法:
一:通过超链接实现下载
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <h1>通过链接下载文件</h1>
- <a href="/day06/download/cors.zip">压缩包</a>
- <a href="/day06/download/1.png">图片</a>
- </body>
- </html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>通过链接下载文件</h1>
<a href="/day06/download/cors.zip">压缩包</a>
<a href="/day06/download/1.png">图片</a>
</body>
</html>
其中day06/download是文档路径,本实例的程序结构如下:
二:通过Servlet程序实现下载
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <h1>通过链接下载文件</h1>
- <a href="/day06/download/cors.zip">压缩包</a>
- <a href="/day06/download/1.png">图片</a>
- <h1>通过servlet程序下载文件</h1>
- <a href="/day06/ServletDownload?filename=cors.zip">压缩包</a>
- <a href="/day06/ServletDownload?filename=1.png">图片</a>
- </body>
- </html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>通过链接下载文件</h1>
<a href="/day06/download/cors.zip">压缩包</a>
<a href="/day06/download/1.png">图片</a>
<h1>通过servlet程序下载文件</h1>
<a href="/day06/ServletDownload?filename=cors.zip">压缩包</a>
<a href="/day06/ServletDownload?filename=1.png">图片</a>
</body>
</html>
其中,/day06/ServletDownload 是servlet程序的映射路径
- package com.lsgjzhuwei.servlet.response;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- * Servlet implementation class ServletDownload
- */
- @WebServlet(asyncSupported = true, urlPatterns = { "/ServletDownload" })
- public class ServletDownload extends HttpServlet {
- private static final long serialVersionUID = 1L;
- /**
- * @see HttpServlet#HttpServlet()
- */
- public ServletDownload() {
- super();
- // TODO Auto-generated constructor stub
- }
- /**
- * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- //获得请求文件名
- String filename = request.getParameter("filename");
- System.out.println(filename);
- //设置文件MIME类型
- response.setContentType(getServletContext().getMimeType(filename));
- //设置Content-Disposition
- response.setHeader("Content-Disposition", "attachment;filename="+filename);
- //读取目标文件,通过response将目标文件写到客户端
- //获取目标文件的绝对路径
- String fullFileName = getServletContext().getRealPath("/download/" + filename);
- //System.out.println(fullFileName);
- //读取文件
- InputStream in = new FileInputStream(fullFileName);
- OutputStream out = response.getOutputStream();
- //写文件
- int b;
- while((b=in.read())!= -1)
- {
- out.write(b);
- }
- in.close();
- out.close();
- }
- /**
- * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- }
- }
package com.lsgjzhuwei.servlet.response; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class ServletDownload
*/
@WebServlet(asyncSupported = true, urlPatterns = { "/ServletDownload" })
public class ServletDownload extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public ServletDownload() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub //获得请求文件名
String filename = request.getParameter("filename");
System.out.println(filename); //设置文件MIME类型
response.setContentType(getServletContext().getMimeType(filename));
//设置Content-Disposition
response.setHeader("Content-Disposition", "attachment;filename="+filename);
//读取目标文件,通过response将目标文件写到客户端
//获取目标文件的绝对路径
String fullFileName = getServletContext().getRealPath("/download/" + filename);
//System.out.println(fullFileName);
//读取文件
InputStream in = new FileInputStream(fullFileName);
OutputStream out = response.getOutputStream(); //写文件
int b;
while((b=in.read())!= -1)
{
out.write(b);
} in.close();
out.close();
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
} }
重启tomcat服务器,即可实现对压缩包和对图片的下载。
java web文件下载功能实现 (转)的更多相关文章
- 【Servlet】java web 文件下载功能实现
需求:实现一个具有文件下载功能的网页,主要下载压缩包和图片 两种实现方法: 一:通过超链接实现下载 在HTML网页中,通过超链接链接到要下载的文件的地址 <!DOCTYPE html> & ...
- Java Web文件下载
Web文件下载有两种.一种是文件在站点文件夹下.在浏览器中直接输入文件路径就可以下载.如http://www.xxx.com/file.zip.第二种是文件不在站点文件夹下或者文件是动态生成的(导出报 ...
- JAVA WEB ------ 文件下载及导出数据到office Execl表格
文件下载需要五步: 1.设置文件ContentType类型 // 设置文件ContentType类型,这样设置,会自动判断下载文件类型 response.setContentType("mu ...
- Java web 文件下载
/** * 下载文件 * @param msg */ public boolean printOutFile(String fileFullName,String fileName) { if (fi ...
- java io 文件下载功能
一. @RequestMapping(value = "/download/{filename}") public void downloadFile(HttpServletReq ...
- Java Web(一) Servlet详解!!
这篇文章到上一篇,距离的有点遥远呀,隔了大概有两个月把,中间在家过了个年,哈哈~ 现在重新开始拾起,最近在看一本个人觉得很棒的书,<Java Web 整合开发王者归来>,现在写的这一系列基 ...
- (转)Java Web(一) Servlet详解!!
https://www.cnblogs.com/whgk/p/6399262.html 这篇文章到上一篇,距离的有点遥远呀,隔了大概有两个月把,中间在家过了个年,哈哈~ 现在重新开始拾起,最近在看一本 ...
- 重拾Java Web应用的基础体系结构
目录 一.背景 二.Web应用 2.1 HTML 2.2 HTTP 2.3 URL 2.4 Servlet 2.4.1 编写第一个Servlet程序 2.5 JSP 2.6 容器 2.7 URL映射到 ...
- JAVA文件下载功能问题解决日志
今天给报告系统做了个下载功能,遇到了挺多问题,通过查资料一一解决了. 1.首先遇到的问题是:java后台的输出流输出之后,没有任何报错,浏览器端不弹出保存文件的对话框,原本是ajax请求到后台的con ...
随机推荐
- 初始化collectionViewCell
#import <UIKit/UIKit.h> @interface TonyCollectionViewCell : UICollectionViewCell @property UII ...
- Bugzilla Error message: couldn't create child process: 720003: index.cgi
two steps is try to fix this issue. 1. Turn off the windowns firewall 2. Register the perl to the sy ...
- Handlebars.js 预编译(转)
Handlebars.js 官网上对预编译1是这样说的: 你需要安装 Node.js 你需要在全局环境中,通过 Npm 安装 handlebars 包 然后你就可以通过命令预编译你的 handleba ...
- MongoDB简单使用 —— 安装
下载 MongoDB的下载路径为:MongoDB Download Center.Win.Linux.Mac平台的都有,光Win平台的就提供msi和zip绿色版本的,这里我下载的是zip版本的. 命令 ...
- ARM LDR/STR, LDM/STM 指令
这里比较下容易混淆的四条指令,已经在这4条指令的混淆上花费了很多精力,现在做个小结,LDR,STR,LDM,STM这四条指令, 关于LDM和STM的说明,见另外一个说明文件,说明了这两个文件用于栈操作 ...
- Delphi 实现对注册表的监视和扫描
;iRes := RegEnumKey( hKeyx, dwIndex, buf, dwSize );if iRes = ERROR_NO_MORE_ITEMS thenbreakelse if iR ...
- WordPress主题开发:开启feed功能
开启feed功能 步骤一:在模版文件的<head></head>元素中添加wp_head()函数,且wp_head()函数要放在</head>标签之前,而且紧邻&l ...
- T4:使用 T4 消除程序和配置文件中重复的字符串信息
背景 我们经常在配置文件中配置各种:id.name,然后在程序中使用这些配置获取信息,这导致了字符串重复出现在系统的多个地方,非常不利于维护,本文介绍采用 T4 来消除这种重复. T4 消除重复 配置 ...
- Linux学习19-gitlab配置邮箱postfix(新用户激活邮件)
前言 gitlab新增新用户有两种方式,第一种可以用户主动注册(自己设置密码):第二种也可以通过root管理员用户直接添加用户,发个邮件到用户的邮箱里,收到邮件后激活. 如果是第二种方式添加新用户的话 ...
- appstore防代充的一些想法
点击这里可以查看代充相关的报道, 利用苹果商店规则漏洞,出现了一个灰色地下产业链>> 用户点击选择要充值的物品时,先向后台服务器发起一个创建订单号的请求,然后再向appstore发起购买商 ...