Java下载文件(特殊处理含中文汉字的文件名)


import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class FileDownload {
    /**
     * 特殊处理含中文的文件名
     * @param urlPath
     * @param downloadDir
     * @return
     */
    public static File download(String urlPath, String downloadDir) {
        File file = null;
        try {
            String[] array=urlPath.split("/");
            String name=array[array.length-1];
            String domain=array[0]+"//"+array[2]+"/";
            String dir="";
            for(int i=3;i<array.length-1;i++) {
                dir+=array[i]+"/";
            }
            //System.out.println(domain+dir+URLEncoder.encode(name,"UTF-8"));
            // 统一资源
            URL url = new URL(domain+dir+URLEncoder.encode(name,"UTF-8"));
            // http的连接类
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            // 设定请求的方法,默认是GET
            connection.setRequestMethod("GET");
            // 设置字符编码
            connection.setRequestProperty("Charset", "UTF-8");
            // 打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)
            connection.connect();
            int code = connection.getResponseCode();
            if (code != HttpURLConnection.HTTP_OK) {
                System.out.println("错误返回码:"+code);
                return null;
            }
            BufferedInputStream bin = new BufferedInputStream(connection.getInputStream());
            String path = downloadDir + File.separatorChar + dir+name;
            file = new File(path);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            OutputStream out = new FileOutputStream(file);
            int size = 0;
            int len = 0;
            byte[] buf = new byte[1024];
            while ((size = bin.read(buf)) != -1) {
                len += size;
                out.write(buf, 0, size);
            }
            System.out.println("文件下载结束!");
            bin.close();
            out.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
        return file;
    }


    public static void main(String[] args) throws Exception{

        //FileDownload.download("http://hadoop.apache.org/images/hadoop-logo.jpg","d:\\download");
        //FileDownload.download("http://19912.xc.cangpie.com/xiaz/hadoop实战第3版pdf中文版@68_134529.exe","d:\\download");


    }
}

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值