public static void saveImageFromURL(String url){ try { URL picUrl; HttpURLConnection conn = null; InputStream is = null; picUrl = new URL(url); conn = (HttpURLConnection) picUrl.openConnection(); conn.setConnectTimeout(20000); conn.setReadTimeout(20000); conn.connect(); // 获取图片大小 int picSize = conn.getContentLength(); is = conn.getInputStream(); String fileName = url.substring(url.lastIndexOf("/")+1); File folder = new File("c:/testimage/"); if(!folder.exists()){ folder.mkdir(); } File file = new File("c:/testimage/"+fileName); OutputStream os = new FileOutputStream(file); final int buffer_size = 1024; byte[] bytes = new byte[buffer_size]; for (;;) { int count = is.read(bytes, 0, buffer_size); if (count == -1) break; os.write(bytes, 0, count); } os.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(); } }
saveImageFromURL
最新推荐文章于 2022-06-24 23:03:55 发布