Java下载文件的几种方式

1.以流的方式下载.

复制代码
  public HttpServletResponse download(String path, HttpServletResponse response) {
        try {
            // path是指欲下载的文件的路径。
            File file = new File(path);
            // 取得文件名。
            String filename = file.getName();
            // 取得文件的后缀名。
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 以流的形式下载文件。</span>
        InputStream fis = <span style="color: #0000ff;">new</span> BufferedInputStream(<span style="color: #0000ff;">new</span><span style="color: #000000;"> FileInputStream(path));
        </span><span style="color: #0000ff;">byte</span>[] buffer = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">byte</span><span style="color: #000000;">[fis.available()];
        fis.read(buffer);
        fis.close();
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 清空response</span>

response.reset();
// 设置response的Header
response.addHeader(“Content-Disposition”, “attachment;filename=” + new String(filename.getBytes()));
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) {
ex.printStackTrace();
}
return response;
}

复制代码

2.下载本地文件

复制代码
  public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {
        // 下载本地文件
        String fileName = "Operator.doc".toString(); // 文件的默认保存名
        // 读到流中
        InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路径
        // 设置输出的格式
        response.reset();
        response.setContentType("bin");
        response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        // 循环取出流中的数据
        byte[] b = new byte[100];
        int len;
        try {
            while ((len = inStream.read(b)) > 0)
                response.getOutputStream().write(b, 0, len);
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
复制代码

3.下载网络文件

复制代码
   public void downloadNet(HttpServletResponse response) throws MalformedURLException {
        // 下载网络文件
        int bytesum = 0;
        int byteread = 0;
    URL url </span>= <span style="color: #0000ff;">new</span> URL("windine.blogdriver.com/logo.gif"<span style="color: #000000;">);

    </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
        URLConnection conn </span>=<span style="color: #000000;"> url.openConnection();
        InputStream inStream </span>=<span style="color: #000000;"> conn.getInputStream();
        FileOutputStream fs </span>= <span style="color: #0000ff;">new</span> FileOutputStream("c:/abc.gif"<span style="color: #000000;">);

        </span><span style="color: #0000ff;">byte</span>[] buffer = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">byte</span>[1204<span style="color: #000000;">];
        </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> length;
        </span><span style="color: #0000ff;">while</span> ((byteread = inStream.read(buffer)) != -1<span style="color: #000000;">) {
            bytesum </span>+=<span style="color: #000000;"> byteread;
            System.out.println(bytesum);
            fs.write(buffer, </span>0<span style="color: #000000;">, byteread);
        }
    } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (FileNotFoundException e) {
        e.printStackTrace();
    } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) {
        e.printStackTrace();
    }
}</span></pre>
复制代码

4.支持在线打开的方式

复制代码
  public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {
        File f = new File(filePath);
        if (!f.exists()) {
            response.sendError(404, "File not found!");
            return;
        }
        BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
        byte[] buf = new byte[1024];
        int len = 0;
    response.reset(); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 非常重要</span>
    <span style="color: #0000ff;">if</span> (isOnLine) { <span style="color: #008000;">//</span><span style="color: #008000;"> 在线打开方式</span>
        URL u = <span style="color: #0000ff;">new</span> URL("file:///" +<span style="color: #000000;"> filePath);
        response.setContentType(u.openConnection().getContentType());
        response.setHeader(</span>"Content-Disposition", "inline; filename=" +<span style="color: #000000;"> f.getName());
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 文件名应该编码成UTF-8</span>
    } <span style="color: #0000ff;">else</span> { <span style="color: #008000;">//</span><span style="color: #008000;"> 纯下载方式</span>
        response.setContentType("application/x-msdownload"<span style="color: #000000;">);
        response.setHeader(</span>"Content-Disposition", "attachment; filename=" +<span style="color: #000000;"> f.getName());
    }
    OutputStream out </span>=<span style="color: #000000;"> response.getOutputStream();
    </span><span style="color: #0000ff;">while</span> ((len = br.read(buf)) &gt; 0<span style="color: #000000;">)
        out.write(buf, </span>0<span style="color: #000000;">, len);
    br.close();
    out.close();
}</span></pre>
复制代码

 

即使爬到最高的山上,一次也只能脚踏实地地迈一步。
2
0
« 上一篇: 【MVC】Spring MVC常用配置
» 下一篇: 【拼接属性查询方式】MySql某一列属性值为拼接时的查询方式
	</div>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值