Web文件下载有两种。一种是文件在站点文件夹下。在浏览器中直接输入文件路径就可以下载。如http://www.xxx.com/file.zip。第二种是文件不在站点文件夹下或者文件是动态生成的(导出报表或者导出excel等),这样的情况须要通过response的OutputStream实现文件的下载。

DownloadUtils是一个Java Web文件下载工具类,提供多种静态方法实现文件下载。

package com.rhui.util;

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 java.net.URLEncoder; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; /**
* 文件下载类
*/
public class DownloadUtils {
/**
* 文件下载编码
* 该编码告诉浏览器文件名称的编码方式,以防下载中文文件名称时有乱码
*/
private static String encoding = "utf-8"; /**
* 文件下载
* @param response
* @param filePath 文件在server上的路径,包含文件名称
*/
public static void download(HttpServletResponse response, String filePath){
File file = new File(filePath.toString());
download(response, file, null, encoding);
} /**
* 文件下载
* @param response
* @param filePath 文件在server上的路径。包含文件名称称
* @param fileName 文件下载到浏览器的名称。假设不想让浏览器下载的文件名称称和server上的文件名称称一样,请设置该參数
*/
public static void download(HttpServletResponse response, String filePath, String fileName){
File file = new File(filePath.toString());
download(response, file, fileName, encoding);
} /**
* 文件下载
* @param response
* @param filePath 文件在server上的路径,包含文件名称称
* @param fileName 文件下载到浏览器的名称,假设不想让浏览器下载的文件名称称和server上的文件名称称一样,请设置该參数
* @param encoding 文件名称称编码
*/
public static void download(HttpServletResponse response, String filePath, String fileName, String encoding){
File file = new File(filePath.toString());
download(response, file, fileName, encoding);
} /**
* 文件下载
* @param response
* @param file 文件
* @param fileName 文件下载到浏览器的名称,假设不想让浏览器下载的文件名称称和server上的文件名称称一样,请设置该參数
*/
public static void download(HttpServletResponse response, File file) {
download(response, file, null, encoding);
} /**
* 文件下载
* @param response
* @param file 文件
* @param fileName 文件下载到浏览器的名称,假设不想让浏览器下载的文件名称称和server上的文件名称称一样,请设置该參数
*/
public static void download(HttpServletResponse response, File file, String fileName) {
download(response, file, fileName, encoding);
} /**
* 文件下载
* @param response
* @param file 文件
* @param fileName 文件下载到浏览器的名称,假设不想让浏览器下载的文件名称称和server上的文件名称称一样,请设置该參数
* @param encoding 文件名称称编码
*/
public static void download(HttpServletResponse response, File file, String fileName, String encoding) {
if(file == null || !file.exists() || file.isDirectory()){
return;
} // 假设不指定文件下载到浏览器的名称,则使用文件的默认名称
if (StringUtils.isBlank(fileName)) {
fileName = file.getName();
} try {
InputStream is = new FileInputStream(file);
download(response, is, fileName, encoding);
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 文件下载
* @param response
* @param is 文件输入流
* @param fileName 下载的文件名称称
* @throws IOException
*/
public static void download(HttpServletResponse response, InputStream is, String fileName){
download(response, is, fileName, encoding);
} /**
* 文件下载
* @param response
* @param is 文件输入流
* @param fileName 下载的文件名称称
* @param encoding 编码格式
*/
public static void download(HttpServletResponse response, InputStream is, String fileName, String encoding){
if(is == null || StringUtils.isBlank(fileName)){
return;
} BufferedInputStream bis = null;
OutputStream os = null;
BufferedOutputStream bos = null; try{
bis = new BufferedInputStream(is);
os = response.getOutputStream();
bos = new BufferedOutputStream(os);
response.setContentType("application/octet-stream;charset=" + encoding);
response.setCharacterEncoding(encoding);
response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(fileName, encoding));
byte[] buffer = new byte[1024];
int len = bis.read(buffer);
while(len != -1){
bos.write(buffer, 0, len);
len = bis.read(buffer);
} bos.flush();
}catch(IOException e){
e.printStackTrace();
}finally{
if(bis != null){
try{
bis.close();
}catch(IOException e){}
} if(is != null){
try{
is.close();
}catch(IOException e){}
}
}
} public static String getEncoding() {
return encoding;
} public static void setEncoding(String encoding) {
DownloadUtils.encoding = encoding;
}
}

假设文件保存在server的非站点文件夹下

String filePath = "c:\\file.zip";
DownloadUtils.download(response, filePath);

假设文件是输入流

// is为文件输入流
// fileName为浏览器下载的文件名
// encoding为文件名编码,预防文件里有中文的时候产生乱码
String fileName = "file.zip";
String encoding = "utf-8";
DownloadUtils.download(response, is, fileName, encoding);

Servlet中文件下载

package com.rhui.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.rhui.util.DownloadUtils; @WebServlet("/download/servlet")
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L; protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filePath = "c:\\file.zip";
DownloadUtils.download(response, filePath);
} }

Java Web文件下载的更多相关文章

  1. 【Servlet】java web 文件下载功能实现

    需求:实现一个具有文件下载功能的网页,主要下载压缩包和图片 两种实现方法: 一:通过超链接实现下载 在HTML网页中,通过超链接链接到要下载的文件的地址 <!DOCTYPE html> & ...

  2. JAVA WEB ------ 文件下载及导出数据到office Execl表格

    文件下载需要五步: 1.设置文件ContentType类型 // 设置文件ContentType类型,这样设置,会自动判断下载文件类型 response.setContentType("mu ...

  3. java web文件下载功能实现 (转)

    http://blog.csdn.net/longshengguoji/article/details/39433307 需求:实现一个具有文件下载功能的网页,主要下载压缩包和图片 两种实现方法: 一 ...

  4. Java web 文件下载

    /** * 下载文件 * @param msg */ public boolean printOutFile(String fileFullName,String fileName) { if (fi ...

  5. java web(四)文件上传与下载

     一.文件上传原理 1.在TCP/IP中,最早出现的文件上传机制是FTP ,它是将文件由客户端发送到服务器的标准机制:但是在jsp使用过程中不能使用FTP方法上传文件,这是由jsp运行机制所决定. 通 ...

  6. Java Web整合开发实战:基于Struts 2+Hibernate+Spring 目录

    第1篇 Java Web开发基础第1章 Web的工作机制( 教学视频:31分钟) 1.1 理解Web的概念 1.1.1 Web的定义 1.1.2 Web的三个核心标准 1.2 C/S与B/S两种软件体 ...

  7. Java Web文件上传

    参考资料:http://www.cnblogs.com/xdp-gacl/p/4200090.html 一.问题描述 Java Web文件上传需要借助一些第三方库,常用的是借助Apache的包,有两个 ...

  8. JAVA Web 之 struts2文件上传下载演示(二)(转)

    JAVA Web 之 struts2文件上传下载演示(二) 一.文件上传演示 详细查看本人的另一篇博客 http://titanseason.iteye.com/blog/1489397 二.文件下载 ...

  9. JAVA Web 之 struts2文件上传下载演示(一)(转)

    JAVA Web 之 struts2文件上传下载演示(一) 一.文件上传演示 1.需要的jar包 大多数的jar包都是struts里面的,大家把jar包直接复制到WebContent/WEB-INF/ ...

随机推荐

  1. sql server truncate table 删除表数据限制条件

    truncate 注释 注释TRUNCATE TABLE 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行.但 TRUNCATE TABLE 比 DELETE 速度快 ...

  2. PKI中常用编码:ASN.1 DER BER Base64

    迟到了两年的笔记... 在PKI的应用中,常会用到以下几个编码概念: ASN.1(Abstract Syntax Notation One, 抽象语法标记) 定义:A standard interfa ...

  3. 6.11 将分割数据转换为多值IN列表

    问题 已经有了分隔数据,想要将其转换为WHERE子句IN列表中的项目.考虑下面的字符串: 7654,7698,7782,7788 要将该字符串用在WHERE子句中,但是下面的SQL语句是错误的,因为E ...

  4. HDU_1421_搬寝室_dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1421 搬寝室 Time Limit: 2000/1000 MS (Java/Others)    Me ...

  5. iOS布局分类

    1.线性布局: 2.集合布局: 3.单元布局: 需要考虑因素: 空间充足.空间不足时内容.尺寸的取舍.

  6. 安卓app测试之流量监控

    一.查看PID 通过ps命令查看:ps | grep packageName 案例:adb shell "ps | grep tv.danmaku.bili" adb shell ...

  7. Nginx 通过 certbot 为网站自动配置 SSL 证书并续期

    一.背景知识 1.1.http 和 https 是什么? 简单来说,http 是一个传输网页内容的协议,比如你看到的 http 开头的网站 http://www.163.com ,其网页上的文字.图片 ...

  8. impdp and docker install oracleXE

    docker oracle https://hub.docker.com/r/sath89/oracle-xe-11g/ docker run -d -p 8080:8080 -p 1521:1521 ...

  9. wpf 自定义单选按钮 RadioButton

    新建RadioButtonEx.cs public class RadioButtonEx : RadioButton { public Geometry SelectIcon { get { ret ...

  10. block教程

    http://pernghh.pixnet.net/blog/trackback/eac87d412e/33563409 本文来自台湾的某开发人员的博客,被墙,感觉讲的比较易懂,所以引过来.文字简体化 ...