public void downloadFile(String url) {

try {
URL path = null;
try {
path = new URL(url);
} catch (Exception e) {
System.out.println("Error input url");
}
String fileName = url.substring(url.lastIndexOf("/")+1);
FilterInputStream in = (FilterInputStream) path.openStream();
File fileOut = new File("d:\\Temp\\"+fileName);
FileOutputStream out = new FileOutputStream(fileOut);
byte[] bytes = new byte[1024];
int c;
while ((c = in.read(bytes)) != -1) {
out.write(bytes, 0, c);
}
in.close();
out.close();
} catch (Exception e) {
System.out.println("Error!");
throw new RuntimeException(e);
}
}

@Test
public void test(){
downloadFile("http://imgsrc.baidu.com/baike/pic/item/f765756053ce5274eaf8f80b.jpg");

}

相关文章:

  • 2021-12-20
  • 2021-11-23
  • 2021-08-07
  • 2021-08-26
  • 2022-01-16
  • 2021-11-24
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-29
  • 2021-11-12
  • 2021-07-05
  • 2022-01-25
相关资源
相似解决方案