java下载URl的网络资源

某人需要下载这个东西,但是呢?浏览器直接访问链接下载不了,于是有了下面一幕


package com.audaque.module.master.data.util;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Text {
    public static void main(String[] args) throws IOException {
        findSomeThing();
    }
  public static void findSomeThing() throws MalformedURLException, IOException {

                 String url = "XXX/FileRoot2/2016-4/20/68f1b550-cc4a-4e72-a4f4-ee5bf5f80a9d//mobliepic/";
                 String[] s={"0-222944","10028212-10318209","10318211-10553663","10553665-10765566","10765568-10987652",
                         "10987654-11242363","1101245-1415936","11242365-11464509","11464511-11678662","11678664-11903082",
                         "1415938-1695003","1695005-1940265","1940267-2181186","2181188-2467126","222946-379776","2467128-2765777","2765779-2977745","2977747-3255673","3255675-3574579",
                         "3574581-3810974","379778-525961","3810976-4005743","4005745-4266337","4266339-4494825",
                         "4494827-4683808","4683810-4933610","4933612-5155356","5155358-5465644","525963-733946","5465646-5671020","5671022-5839489","5839491-6048474","6048476-6346911",
                         "6346913-6545196","6545198-6755675","6755677-6978178","6978180-7153901","7153903-7286583","7286585-7475833",
                         "733948-899725","7475835-7712458","7712460-8013872","8013874-8215475","8215477-8572024","8572026-8936666",
                         "8936668-9338827","899727-1101243","9338829-9659808","9659810-9847752","9847754-10028210"};
                 for (int i = 0; i < s.length; i++) {

                     String imgurl = url + i+".png";
                      try {
                          imgurl ="XX/rECjt4aHus9.jpg?ssig=gBlMDN2s9C&Expires=1590794657&KID=sina,ishare&range=";

                          imgurl=imgurl+s[i];
                          // 构造URL
                             URL weburl = new URL(imgurl);
                            // 打开连接
                         URLConnection con = weburl.openConnection();
                               // 设置请求超时为5s
                                con.setConnectTimeout(5 * 1000);
                               // 输入流
                               InputStream is = con.getInputStream();

                         // 1K的数据缓冲
                              byte[] bs = new byte[1024];
                            // 读取到的数据长度
                               int len;
                               // 输出的文件流
                         File sf = new File("D:\\ceshi\\zibiz");

                             if (!sf.exists()) {
                                      sf.mkdirs();
                                  }
                                 OutputStream os = new FileOutputStream(sf.getPath() + "\\" + imgurl.substring(imgurl.lastIndexOf("range="),imgurl.length()-1) + ".jpg");
                                 // 开始读取
                               while ((len = is.read(bs)) != -1) {
                                      os.write(bs, 0, len);
                                   }
                               // 完毕,关闭所有链接
                              os.close();
                                is.close();

                             } catch (IOException e) {
                                continue;
                            }
                    }
             }

}

 

java下载URl的网络资源_第1张图片

你可能感兴趣的:(java技术)