声明:根据http请求图片的url下载文件到本地

 

public void  downLoadPhoto(String photoUrl)throws Exception{
         
        //获取照片返回二进制流
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            ResponseEntity<byte[]> entity = restTemplate.exchange(photoUrl, HttpMethod.GET,new HttpEntity<>(headers), byte[].class);
            byte[] body = entity.getBody();
            InputStream sbs = new ByteArrayInputStream(body);

            File dest = new File("/Users/qixin/Desktop/a.jpg");
            if(!dest.exists()) {
                dest.createNewFile();
            }

            InputStream in = null;
            OutputStream out = null;

            in = new ByteArrayInputStream(body);
            out = new FileOutputStream(dest);

            byte []bt = new byte[1024];
            int length=0;
            while(	(length = in.read(bt)) !=-1) {
                //一次性写入文件a中
                out.write(bt,0,length);
            }

            if(null!=out) {
                out.close();
            }



 }