河马日记

vuePress-theme-reco Mr.Tian    2019 - 2023
河马日记 河马日记

Choose mode

  • dark
  • auto
  • light
首页
分类
  • 后端
  • 前端
  • 随笔
  • 前后端结合
标签
时间线
联系方式
  • 微信
author-avatar

Mr.Tian

15

文章

5

标签

首页
分类
  • 后端
  • 前端
  • 随笔
  • 前后端结合
标签
时间线
联系方式
  • 微信
  • java下载图片

java下载图片

vuePress-theme-reco Mr.Tian    2019 - 2023

java下载图片


Mr.Tian 2019-11-09 00:02:46 java

java中下载图片到本地

package yourpackage;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @program: 
 * @description: 下载图片,跨域问题暂时解决
 * @author: tiandh
 * @create: 2019/11/8 16:44
 **/
public class DownloadImg {
    public static String downloadImg(String Url) throws Exception {
        String path = Url;
        URL url = new URL(path);
        HttpURLConnection http = (HttpURLConnection) url.openConnection();
        InputStream is = http.getInputStream();
        OutputStream os = new FileOutputStream("D://123.jpg");
        byte by[] = new byte[1024];
        int len = 0;
        while ((len = is.read(by, 0, by.length)) != -1) {
            os.write(by, 0, len);
        }
        os.close();
        is.close();
        // System.out.println("successful");
        return "下载成功";
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33