Java学习笔记――IO操作之以图片地址下载图片 (二)

2014-11-24 10:11:51 · 作者: · 浏览: 1
try {
URL url = new URL(strUr1);
InputStream fStream = url.openConnection().getInputStream();
int b = 0;
FileOutputStream fos = new FileOutputStream(new File(filePath));
while ((b=fStream.read())!=-1) {
fos.write(b);
}
fStream.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void DownPictureToInternet(String filePath,String strUr1){
try {
URL url = new URL(strUr1);
InputStream fStream = url.openConnection().getInputStream();
int b = 0;

FileOutputStream fos = new FileOutputStream(new File(filePath));
while ((b=fStream.read())!=-1) {
fos.write(b);
}
fStream.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

应用:
[java]
public static void main(String[] args) {
mWriterPicture("test/1.jpg", mReaderPicture("res/me.jpg"));
mWriterPicture("test/2.jpg", mReaderPictureToInternet(
"https://www.cppentry.com/upload_files/article/76/1_q3wmd__.jpg"));
DownPictureToInternet("test/下载.jpg",
"https://www.cppentry.com/upload_files/article/76/1_ptftl__.jpg");
}