/**
*
* @param file
* @param response
*/
private void downLoad(File file, HttpServletResponse response,
String browser) throws IOException {
InputStream is = new FileInputStream(file);
try { String fileName = file.getName().replaceAll(" ", "");
OutputStream os = response.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(os); if (browser.indexOf("msie") != -1) {
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
fileName = new String(fileName.getBytes("UTF-8"), "GBK");
} else if (browser.indexOf("Firefox") != -1) {
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
} else if (browser.indexOf("Safari") != -1) {
fileName = new String(fileName.getBytes(), "ISO8859-1");
} response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename="
+ fileName);
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
} bos.flush();
bis.close();
bos.close(); is.close(); os.close(); } catch (Exception ex) { log.info(""); } finally {
is.close();
}
}

java下载文件的更多相关文章

  1. 【文件下载】Java下载文件的几种方式

    [文件下载]Java下载文件的几种方式  摘自:https://www.cnblogs.com/sunny3096/p/8204291.html 1.以流的方式下载. public HttpServl ...

  2. java下载文件工具类

    java下载文件工具类 package com.skjd.util; import java.io.BufferedInputStream; import java.io.BufferedOutput ...

  3. java下载文件时文件名出现乱码的解决办法

    转: java下载文件时文件名出现乱码的解决办法 2018年01月12日 15:43:32 橙子橙 阅读数:6249   java下载文件时文件名出现乱码的解决办法: String userAgent ...

  4. Java 下载文件

    public @ResponseBody void exportExcel(HttpServletRequest request, HttpServletResponse response, Khxx ...

  5. java下载文件demo

    java通过http方式下载文件 https://www.cnblogs.com/tiancai/p/7942201.html

  6. Java下载文件(流的形式)

    @RequestMapping("download") @ResponseBody public void download(HttpServletResponse respons ...

  7. Java下载文件的几种方式

    转发自博客园Sunny的文章 1.以流的方式下载 public HttpServletResponse download(String path, HttpServletResponse respon ...

  8. java 下载文件的两种方式和java文件的上传

    一:以网络的方式下载文件 try { // path是指欲下载的文件的路径. File file = new File(path); // 以流的形式下载文件. InputStream fis = n ...

  9. Java下载文件方法

    public static void download(String path, HttpServletResponse response) { try { // path是指欲下载的文件的路径. F ...

  10. java下载文件指定目录下的文件

    方法一: @RequestMapping('download')def download(HttpServletRequest request, HttpServletResponse respons ...

随机推荐

  1. jQuery的remove和detach的区别

    1.remove([expr])   概述:从DOM中删除所有匹配的元素. 这个方法不会把匹配的元素从jQuery对象中删除,因而可以在将来再使用这些匹配的元素.但除了这个元素本身得以保留之外,其他的 ...

  2. c#播放声音文件

    C#中声音的播放主要有三种方法: 1.使用API函数. 2.使用SoundPlayer类播放. 3.使用DirectX进行播放. 一.使用API函数进行播放. windows操作系统中的winmm.d ...

  3. error on line 1 at column 6: XML declaration allowed only at the start of the document

    This page contains the following errors: error on line 1 at column 6: XML declaration allowed only a ...

  4. 在centos服务器上配置gitlab钩子引发的一系列问题

    为了给公司的服务器上搭建gitlab环境并且配置钩子(实现在本地git push之后服务器自动git pull),整了好久,最后终于把问题解决了,下面是记录安装gitlab之后引发的一系列问题: 首先 ...

  5. 【问题】pod setup 问题

    安装pod setup 的时候,可能会安装失败,可以多试几次,但是如果一直失败,那就是由问题了. 解决办法: 1. 分别执行下面命令卸载cocoapods和xcodeproj,如果你的机器上面有多个版 ...

  6. 用TMS的控件就可以了,有bug叫他们改

    [深圳]大宝delphi本身不是太隐定.不建议弄太多自己的东西.还要debug好长时间.为了快.便不去弄控件了够用了.真的.都不用花太多时间去弄这弄那.有bug叫他们改便可以.

  7. Acdream1157---Segments (CDQ分治)

    陈丹琦分治~~~其实一些数据小的时候可以用二维或者多维树状数组做的,而数据大的时候就无力的题目,都可以用陈丹琦分治解决. 题目:由3钟类型操作:1)D L R(1 <= L <= R &l ...

  8. leetcode-Rising Temperature

    Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to ...

  9. HDOJ 2191

    多重背包. 模版. #include <iostream> #include <stdio.h> #include <stdlib.h> #include < ...

  10. iOS - 排序的队列中插入数值

    http://stackoverflow.com/questions/8180115/nsmutablearray-add-object-with-order 用Selector http://sta ...