java程序,一直图片的网络路径,怎么样吧这个路径的这张照片取下来并保存在服务器上面?就这样一个地址
http://g3.ykimg.com/1100641F4651E8AB8200290955DEA7F39F0E12-D63B-A85A-C8BD-7919DD69CAB9高手求解答 谢谢图片java抓取图片

解决方案 »

  1.   

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
    URL url = new URL("http://g3.ykimg.com/1100641F4651E8AB8200290955DEA7F39F0E12-D63B-A85A-C8BD-7919DD69CAB9");

    URLConnection conn = url.openConnection();

    byte[] byteBuffer = new byte[4096];
    InputStream is = conn.getInputStream();
    FileOutputStream fos = new FileOutputStream("C:/Users/zhangxinxin/Desktop/test.jpg");
    int len = 0;
    int off =0;
    while((len=is.read(byteBuffer))!=-1)
    {
    fos.write(byteBuffer, off, len);
    }
    is.close();
    fos.close();
    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
      

  2.   

    怎么把图片放到WebContent/data目录下?
      

  3.   

    人家回复里面不是指定了地址了么  new FileOutputStream("C:/Users/zhangxinxin/Desktop/test.jpg");  改下就得了。
      

  4.   

    继续求教2楼兄弟, 如果抓取的图片不一定是jpg, 你会怎么处理?
      

  5.   


    public boolean getPic(String content,String filepath){
         boolean b =false;
            String searchImgReg = "(?x)(src|SRC|background|BACKGROUND)=('|\")(http://([\\w-]+\\.)+[\\w-]+(:[0-9]+)*(/[\\w-]+)*(/[\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")";
    //        String content = ts.getHtmlCode("http://www.infosec.org.cn/news/news_view.php?newsid=17118");
            Pattern pattern = Pattern.compile(searchImgReg);
            Matcher matcher = pattern.matcher(content);
            String filename="";
            while(matcher.find()){
             //替换文本值
             String str =matcher.group(3);
             //获取src的文本
                filename=str.substring(str.lastIndexOf("/")+1,str.length());
                String path = this.getClass().getClassLoader().getResource("").getPath();
                String uploadPath = path.substring(1,path.lastIndexOf("WEB-INF/classes")) +filepath;//选定上传的目录此处为当前目录 
                
                if(!new File(uploadPath).isDirectory())//选定上传的目录此处为当前目录,没有则创建 
                    new File(uploadPath).mkdirs(); 
                try {
    String upPath=new CatchPicture().upload(matcher.group(3),uploadPath,filename);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }  
    b = true;
            }
            return b;
        }
        
        /** 
         * 上传 图片  
         * @param urlStr 
         * @param path 
         * @return 
         * @throws Exception  
         */  
        public String upload(String urlStr,String path,String filename) throws Exception{  
            Calendar calendar = Calendar.getInstance();  
            String month = calendar.get(Calendar.YEAR) + "/"  
                    + (calendar.get(Calendar.MONTH) + 1);  
            download(urlStr,path,filename);  
            return path+month + "/" + filename;  
        }  
        /** 
         * 根据路径 下载图片 然后 保存到对应的目录下 
         * @param urlString 
         * @param savePath 
         * * @param filename 
         * @return 
         * @throws Exception 
         */  
        public void download(String urlString, String savePath,String filename) throws Exception {  
            // 构造URL  
            URL url = new URL(urlString);  
            // 打开连接  
            URLConnection con = url.openConnection();  
            //设置请求的路径  
            con.setConnectTimeout(5*1000);  
            // 输入流  
            InputStream is = con.getInputStream();  
          
            // 1K的数据缓冲  
            byte[] bs = new byte[1024];  
            // 读取到的数据长度  
            int len;  
            // 输出的文件流  
           File sf=new File(savePath);  
           if(!sf.exists()){  
               sf.mkdirs();  
           }  
           OutputStream os = new FileOutputStream(sf.getPath()+"\\"+filename);  
            // 开始读取  
            while ((len = is.read(bs)) != -1) {  
              os.write(bs, 0, len);  
            }  
            // 完毕,关闭所有链接  
            os.close();  
              
            is.close();  
        }   
        /**  
         * 根据URL获得所有的html信息  
         *   
         * @param url  
         * @return  
         */  
        public static String getHtmlByUrl(String url) {   
            String html = null;   
            HttpClient httpClient = new DefaultHttpClient(); // 创建httpClient对象   
            HttpGet httpget = new HttpGet(url); // 以get方式请求该URL   
            try {   
                HttpResponse responce = httpClient.execute(httpget); // 得到responce对象   
                int resStatu = responce.getStatusLine().getStatusCode(); // 返回码   
                if (resStatu == HttpStatus.SC_OK) { // 200正常 其他就不对   
                    HttpEntity entity = responce.getEntity();   // 获得相应实体   
                    String charset = "UTF-8";  //设置编码
                    if (entity != null) {   
                        html = EntityUtils.toString(entity,charset); // 获得html源代码   
                    }   
                }   
            } catch (Exception e) {   
                System.out.println("访问【" + url + "】出现异常!");   
                e.printStackTrace();   
            } finally {   
               httpClient.getConnectionManager().shutdown();   
            }   
            return html;   
        } 
    上面 方法 调用!!