java下载图片到本地,例如从网上下载图片,下载淘宝图片,下载百度图片等

刺骨的言语ヽ痛彻心扉 2022-10-29 07:41 314阅读 0赞

java下载图片到本地,例如从网上下载图片,下载淘宝图片,下载百度图片。

直接上代码如下:

  1. public static void download(String url,String saveFileName) throws Exception{
  2. URL url1 = new URL(url);
  3. URLConnection uc = url1.openConnection();
  4. InputStream inputStream = null;
  5. FileOutputStream out = null;
  6. try {
  7. inputStream = uc.getInputStream();
  8. out = new FileOutputStream(saveFileName);
  9. int j = 0;
  10. while ((j = inputStream.read()) != -1) {
  11. out.write(j);
  12. }
  13. }catch (Exception e){
  14. e.printStackTrace();
  15. }finally {
  16. if(out!=null){
  17. out.close();
  18. }
  19. if(inputStream!=null) {
  20. inputStream.close();
  21. }
  22. }
  23. }

main方法为:

  1. public static void main(String[] args){
  2. download("https://hbimg.huabanimg.com/38d7f27f25bc27342ec40ebc1337d074a244b59822f4b-f0S0oF_fw658", "D:\p\1\data\1_无_"+ UUID.randomUUID().toString()+".jpg");
  3. }

发表评论

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

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

相关阅读