Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. public static void saveImage(String imageUrl) throws IOException {
  2. String tempDir = System.getProperty("java.io.tmpdir");
  3. URL u = new URL(imageUrl);
  4. String fileName = u.getFile();
  5. String destName = tempDir + fileName.substring(fileName.lastIndexOf("_"));
  6. System.out.println(destName);
  7. HttpURLConnection i = (HttpURLConnection) u.openConnection();
  8. i.setRequestProperty("Connection", "close");
  9. InputStream is = i.getInputStream();
  10. OutputStream os = new FileOutputStream(destName);
  11.  
  12. byte[] b = new byte[2048];
  13. int length;
  14.  
  15. while ((length = is.read(b)) != -1) {
  16. os.write(b, 0, length);
  17. }
  18. is.close();
  19. os.close();
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement