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 文件在服务器上的路径,包含文件名
*/
public static void download(HttpServletResponse response, String filePath){
File file = new File(filePath.toString());
download(response, file, null, encoding);
}
/**
* 文件下载
* @param response
* @param filePath 文件在服务器上的路径,包括文件名称
* @param fileName 文件下载到浏览器的名称,如果不想让浏览器下载的文件名称和服务器上的文件名称一样,请设置该参数
*/
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 文件在服务器上的路径,包括文件名称
* @param fileName 文件下载到浏览器的名称,如果不想让浏览器下载的文件名称和服务器上的文件名称一样,请设置该参数
* @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 文件下载到浏览器的名称,如果不想让浏览器下载的文件名称和服务器上的文件名称一样,请设置该参数
*/
public static void download(HttpServletResponse response, File file) {
download(response, file, null, encoding);
}
/**
* 文件下载
* @param response
* @param file 文件
* @param fileName 文件下载到浏览器的名称,如果不想让浏览器下载的文件名称和服务器上的文件名称一样,请设置该参数
*/
public static void download(HttpServletResponse response, File file, String fileName) {
download(response, file, fileName, encoding);
}
/**
* 文件下载
* @param response
* @param file 文件
* @param fileName 文件下载到浏览器的名称,如果不想让浏览器下载的文件名称和服务器上的文件名称一样,请设置该参数
* @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;
}
}
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);
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);
}
}
package cn.itcast.day06.web.servlet;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 实现防盗链功能
// 获得 referer 头 用于说明来访者来自哪里
String referer = request.getHeader("referer");
if(referer==null || !referer.startsWith("http://localhost")) {
// 是盗链者
response.sendRedirect("/day06/index.jsp");
return ;
}
// 解决response中文乱码问题
response.setContentType("text/html;charset=utf-8"); // 设置消息体的编码
// 通过 http 协议 发送的http响应消息头 不能出现中文 中文必须要经过url编码
String filename = URLEncoder.encode("美女.jpg", "utf-8");
// 通知浏览器以下载的方式读取资源
response.setHeader("content-disposition", "attachment;filename="+filename);
// 读取图片数据 发给ie浏览器
String webPath = "/download/美女.jpg"; // 相当于当前web应用的path
ServletContext servletContext = super.getServletContext();
InputStream in = servletContext.getResourceAsStream(webPath);
OutputStream out = response.getOutputStream();
int len;
byte[] buffer = new byte[1024];
while((len=in.read(buffer))!=-1)
out.write(buffer, 0, len);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有