前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java下载文件(特殊处理含中文汉字的文件名)

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

作者头像
程裕强
发布2022-05-06 19:56:09
7320
发布2022-05-06 19:56:09
举报
文章被收录于专栏:大数据学习笔记
代码语言:javascript
复制
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");


    }
}
这里写图片描述
这里写图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-04-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档