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
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