根据URL下载图片
/*
urlList:需要保存图片的名称(我这里是截取的,但方法里其实没用到)
pic_url:需要下载图片的链接
path:路径名
*/
public static void downloadPicture(String urlList, String pic_url,String path) {
// TODO Auto-generated method stub
try {
URL url=new URL(pic_url);
urlList = urlList.substring(urlList.indexOf("mall_id")+8,urlList.indexOf("&"));
System.out.println(urlList);
DataInputStream dataInputStream = new DataInputStream(url.openStream());//读取网络资源
FileOutputStream fileOutputStream = new FileOutputStream(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 (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
测试
public static void main(String[] args)
{
String url="http://mobile.yangkeduo.com/mall_certificates.html?mall_id=466839685&refer_page_name=login&refer_page_id=login_1539132109994_s1IGaeoxzZ&refer_page_sn=10039";
String pic_url="http://imsproductionimg.yangkeduo.com/ded2dacde195436abe27a7e4ab6a9cbd.jpg?x-oss-process=image/watermark,text_5ou85aSa5aSa5YWl6am75LiT55SoIOWFtuS7luaXoOaViA==,type_ZmFuZ3poZW5nc2h1c29uZw==,color_c8c8c8,size_12,shadow_0,rotate_345,fill_1";
String path = "E:\\images\\"+url.substring(url.indexOf("mall_id")+8,url.indexOf("&"))+"_营业执照(文字).jpg";
downloadPicture(url,pic_url,path);
}