根据网络地址下载图片及文件

缺乏、安全感 2023-06-22 03:27 103阅读 0赞
  1. /**
  2. * @Author LiuYong
  3. * @Date 2019/12/13 17:44
  4. * @Description TODO
  5. * @param urlList url下载地址
  6. * @param path 保存地址
  7. * @return
  8. **/
  9. private static void downloadPicture(String urlList,String path) {
  10. URL url = null;
  11. try {
  12. url = new URL(urlList);
  13. DataInputStream dataInputStream = new DataInputStream(url.openStream());
  14. FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
  15. ByteArrayOutputStream output = new ByteArrayOutputStream();
  16. byte[] buffer = new byte[1024];
  17. int length;
  18. while ((length = dataInputStream.read(buffer)) > 0) {
  19. output.write(buffer, 0, length);
  20. }
  21. fileOutputStream.write(output.toByteArray());
  22. dataInputStream.close();
  23. fileOutputStream.close();
  24. } catch (MalformedURLException e) {
  25. e.printStackTrace();
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. }

发表评论

表情:
评论列表 (有 0 条评论,103人围观)

还没有评论,来说两句吧...

相关阅读