Guest User

Untitled

a guest
Jan 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. public String downloadImages(String serverUrl,String directory)
  2. {
  3.  
  4. if (serverUrl == null)
  5. return null;
  6.  
  7. String LocalFilePath=serverUrl.substring(serverUrl.lastIndexOf("/")+1, serverUrl.length());
  8. File file = new File(directory, LocalFilePath);
  9. if (!file.exists())
  10. {
  11. int bytesread = 0;
  12. byte[] bytes = new byte[1024];
  13. InputStream strm = null;
  14. HttpResponse response = null;
  15. HttpEntity entity = null;
  16. String LocalFile = null;
  17. FileOutputStream foStream;
  18. BufferedOutputStream writer = null;
  19. try {
  20. DefaultHttpClient httpclient = new DefaultHttpClient();
  21. serverUrl = serverUrl.replace(" ", "%20");
  22. HttpGet httpget = new HttpGet(serverUrl);
  23.  
  24. response = httpclient.execute(httpget);
  25.  
  26. if (response == null)
  27. {
  28. return null;
  29. }
  30. entity = response.getEntity();
  31. strm = entity.getContent();
  32. File dire = new File(directory);
  33. if (!dire.isDirectory())
  34. {
  35. dire.mkdirs();
  36. }
  37. LocalFile = directory+LocalFilePath;
  38. directory = null;
  39. foStream = new FileOutputStream(LocalFile);
  40. writer = new BufferedOutputStream(foStream);
  41.  
  42. do
  43. {
  44. bytesread = strm.read(bytes, 0, bytes.length);
  45. if(bytesread > 0)
  46. {
  47. writer.write(bytes, 0, bytesread);
  48. writer.flush();
  49. }
  50. } while (bytesread > 0);
  51.  
  52. writer.close();
  53. foStream.close();
  54.  
  55. strm.close();
  56. return LocalFile;
  57. }
  58. catch(Exception e)
  59. {
  60. file.delete();
  61. e.printStackTrace();
  62. return null;
  63. }
  64. }
  65. else
  66. {
  67. return "file exist";
  68. }
  69. }
Add Comment
Please, Sign In to add comment