java 下载工具_Java 文件下载工具类

Java 文件下载工具类

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

private static Logger logger = LoggerFactory.getLogger(DownloadUtil.class);

文件下载方法

/**

* 文件下载方法

* @param response

* @param filePath

* @param encode

*/

public static void download(HttpServletResponse response, String filePath, String encode) {

response.setContentType("text/html;charset=" + encode);

BufferedInputStream bis = null;

BufferedOutputStream bos = null;

String downLoadPath = filePath;

try {

File file = new File(downLoadPath);

long fileLength = file.length();

String fileName = file.getName();

response.setContentType("application/x-msdownload;");

response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes(encode), "ISO8859-1"));

response.setHeader("Content-Length", String.valueOf(fileLength));

bis = new BufferedInputStream(new FileInputStream(downLoadPath));

bos = new BufferedOutputStream(response.getOutputStream());

byte[] buff = new byte[2048];

int bytesRead;

while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {

bos.write(buff, 0, bytesRead);

}

} catch (Exception e) {

logger.error(e.getMessage());

} finally {

if (bis != null)

try {

bis.close();

} catch (IOException e) {

logger.error(e.getMessage());

}

if (bos != null)

try {

bos.close();

} catch (IOException e) {

logger.error(e.getMessage());

}

}

}

以流的方式下载

/**

* 以流的方式下载

* @param response

* @param filePath

* @param encode

* @return response

*/

public static HttpServletResponse downloadStream(HttpServletResponse response, String filePath, String encode) {

response.setContentType("text/html;charset=" + encode);

try {

// path是指欲下载的文件的路径

File file = new File(filePath);

// 取得文件名

String filename = file.getName();

// 取得文件的后缀名

// String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

// 以流的形式下载文件

InputStream fis = new BufferedInputStream(new FileInputStream(filePath));

byte[] buffer = new byte[fis.available()];

fis.read(buffer);

fis.close();

// 清空response

response.reset();

// 设置response的Header

response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(encode), "ISO8859-1"));

response.addHeader("Content-Length", "" + file.length());

OutputStream toClient = new BufferedOutputStream(response.getOutputStream());

response.setContentType("application/octet-stream");

toClient.write(buffer);

toClient.flush();

toClient.close();

} catch (IOException ex) {

logger.error(ex.getMessage());

}

return response;

}

下载本地文件

/**

* 下载本地文件

* @param response

* @param filePath

* @param encode

*/

public static void downloadLocal(HttpServletResponse response, String filePath,String encode) {

response.setContentType("text/html;charset=" + encode);

try {

// 读到流中

InputStream inStream = new FileInputStream(filePath); // 文件的存放路径

// path是指欲下载的文件的路径

File file = new File(filePath);

// 取得文件名

String fileName = file.getName();

// 设置输出的格式

response.reset();

response.setContentType("bin");

response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes(encode), "ISO8859-1") + "\"");

// 循环取出流中的数据

byte[] b = new byte[100];

int len;

while ((len = inStream.read(b)) > 0) {

response.getOutputStream().write(b, 0, len);

}

inStream.close();

} catch (IOException e) {

logger.error(e.getMessage());

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值