package org.ewhl.common.utils;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
public class DownloadImgForUrlUtils {
public static void main(String[] args) {
//图片地址
String url = "http://*******/**.jpg";
//保存路径(此处是linux下的路径)
String path="/usr/local/nginx/html/zhsqUserPhoto/**.jpg";
downloadPicture(url,path);
}
//链接url下载图片
public static void downloadPicture(String urlList,String path) {
URL url = null;
try {
url = new URL(urlList);
DataInputStream dataInputStream = new DataInputStream(url.openStream());
FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
fileOutputStream.write(output.toByteArray());
dataInputStream.close();
fileOutputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
java后台下载网络图片
最新推荐文章于 2024-04-26 20:10:19 发布