package com.test01;
import java.io.*;
import java.net.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class TestString {
public static void main(String[] args) throws IOException {
HttpDownload("https://static.588ku.com/comp/activity/teachAct/images/bg1.jpg","./test.jpg","9.104.0.3","8080");
}
public static void HttpDownload(String Url, String path, String proxyHost, String proxyPort) throws IOException {
URL url = new URL(Url);
//设置代理服务器
if (proxyHost != null){
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", proxyHost);
System.getProperties().put("proxyPort", proxyPort);
}
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
byte[] buffer = new byte[1024];
int byteRead;
while( (byteRead = bufferedInputStream.read(buffer)) != -1){
fileOutputStream.write(buffer,0,byteRead);
}
bufferedInputStream.close();
fileOutputStream.flush();
fileOutputStream.close();
}
}
数据流下载图片
最新推荐文章于 2022-10-28 15:40:25 发布