网络地址转File对象

public static File inputStreamToFile(String url, String name) throws Exception{
        File file=null;
        try {
            HttpURLConnection httpUrl = (HttpURLConnection) new URL(url).openConnection();
            httpUrl.connect();
            InputStream ins=httpUrl.getInputStream();
            file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);
            if (file.exists()) {
                return file;
            }
            OutputStream os = new FileOutputStream(file);
            int bytesRead;
            int len = 8192;
            byte[] buffer = new byte[len];
            while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
            httpUrl.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return file;
    }
    public static void main(String[] args) throws Exception {
        File file = inputStreamToFile("https://ibsbjstar.ccb.com.cn/NCCB_Encoder/Encoder?CODE=EUSo4Twu6YRk2JDPJ9OnpfgrdDObpGwKBGOTqVwnBfWAvjCP5UOFuig6hySmrww2hXVIqDgb5NUOrvwI5NRuqtQ3pXcRVh", "aa.png");
    }
上一篇:从零开始的数据结构:李超线段树


下一篇:力扣204.计数质数