Java获取URL地址图片

 Java获取url地址图片

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package  cn.itcast.web.controller;
 
import  java.io.DataInputStream;
import  java.io.DataOutputStream;
import  java.io.FileOutputStream;
import  java.net.HttpURLConnection;
import  java.net.URL;
import  java.util.Date;
 
public  class  CatchPic {
      public  static  boolean  saveUrlAs(String fileUrl, String savePath) /* fileUrl网络资源地址 */  
         {  
       
             try  {  
                 /* 将网络资源地址传给,即赋值给url */  
                 URL url =  new  URL(fileUrl);  
                   
                 /* 此为联系获得网络资源的固定格式用法,以便后面的in变量获得url截取网络资源的输入流 */  
                 HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
                 DataInputStream in =  new  DataInputStream(connection.getInputStream());  
                   
                 /* 此处也可用BufferedInputStream与BufferedOutputStream  需要保存的路径*/  
                 DataOutputStream out =  new  DataOutputStream( new  FileOutputStream(savePath));  
                   
                   
                 /* 将参数savePath,即将截取的图片的存储在本地地址赋值给out输出流所指定的地址 */  
                 byte [] buffer =  new  byte [ 4096 ];  
                 int  count =  0 ;  
                 while  ((count = in.read(buffer)) >  0 ) /* 将输入流以字节的形式读取并写入buffer中 */  
                 {  
                     out.write(buffer,  0 , count);  
                 }  
                 out.close(); /* 后面三行为关闭输入输出流以及网络资源的固定格式 */  
                 in.close();  
                 connection.disconnect();  
                 return  true ; /* 网络资源截取并存储本地成功返回true */  
       
             catch  (Exception e) {  
                 System.out.println(e + fileUrl + savePath);  
                 return  false ;  
             }  
         }  
       
         public  static  void  main(String[] args) {  
            CatchPic pic =  new  CatchPic(); /* 创建实例 */  
               
             //需要下载的URL  
             String photoUrl =  "http://photos.listhub.net/GAMLS/07442715/25?lm=20150426T002920" ;  
       
             // 截取最后/后的字符串  
             String fileName =  new  Date().getTime()+ ".png" ;  
               
             //图片保存路径  
             String filePath =  "D:/img/" ;  
               
             /* 调用函数,并且进行传参 */  
             boolean  flag = pic.saveUrlAs(photoUrl, filePath + fileName);  
               
             System.out.println( "Run ok!\n Get URL file "  + flag);  
             System.out.println(filePath);  
             System.out.println(fileName);  
         }  
 
}


分享到: 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值