java 文件下载示例_java 下载文件的样例

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值