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 ...
随机推荐
- [Java]类的生命周期(下)类的初始化[转]
上接深入java虚拟机——深入java虚拟机(二)——类加载器详解(上),在上一篇文章中,我们讲解了类的生命周期的加载和连接,这一篇我们接着上面往下看. 类的初始化:在类的生命周期执行完加载和连接之后 ...
- j.u.c系列(03)---之AQS:AQS简介
写在前面 Java的内置锁一直都是备受争议的,在JDK 1.6之前,synchronized这个重量级锁其性能一直都是较为低下,虽然在1.6后,进行大量的锁优化策略,但是与Lock相比synchron ...
- [廖雪峰] Git 分支管理(3):分支管理策略
通常,合并分支时,如果可能,Git 会用 Fast forward 模式,但这种模式下,删除分支后,会丢掉分支信息. 如果要强制 禁用 Fast forward 模式,Git 就会在 merge 时生 ...
- 【Go命令教程】14. go env
命令 go env 用于打印 Go 语言的环境信息.其中的一些信息我们在之前已经多次提及,但是却没有进行详细的说明.在本小节,我们会对这些信息进行深入介绍.我们先来看一看 go env 命令情况下都会 ...
- error: internal error: unable to execute QEMU command 'migrate': this feature or command is not cur
感谢朋友支持本博客,欢迎共同探讨交流,因为能力和时间有限.错误之处在所难免,欢迎指正. 假设转载.请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...
- What is the largest TCP/IP network port number allowable for IPv4
69 down vote The largest port number is an unsigned short 2^16-1: 65535 A registered port is one ass ...
- Delphi 获取命令行输出的函数
function GetDosOutput(CommandLine: string; Work: string = 'C:\'): string; var SA: TSecurityAttribute ...
- Java异常(二) 《Effective Java》中关于异常处理的几条建议
概要 本章是从<Effective Java>摘录整理出来的关于异常处理的几条建议.内容包括:第1条: 只针对不正常的情况才使用异常第2条: 对于可恢复的条件使用被检查的异常,对于程序错误 ...
- ASIHTTPRequest系列(一):同步和异步请求
ASIHTTPRequest系列(一):同步和异步请求 发表于8个月前(2013-11-27 19:21) 阅读(431) | 评论(0) 6人收藏此文章, 我要收藏 赞0 ASIHTTPRequ ...
- netty 自定义通讯协议
Netty中,通讯的双方建立连接后,会把数据按照ByteBuf的方式进行传输,例如http协议中,就是通过HttpRequestDecoder对ByteBuf数据流进行处理,转换成http的对象.基于 ...