Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public String downloadImages(String serverUrl,String directory)
- {
- if (serverUrl == null)
- return null;
- String LocalFilePath=serverUrl.substring(serverUrl.lastIndexOf("/")+1, serverUrl.length());
- File file = new File(directory, LocalFilePath);
- if (!file.exists())
- {
- int bytesread = 0;
- byte[] bytes = new byte[1024];
- InputStream strm = null;
- HttpResponse response = null;
- HttpEntity entity = null;
- String LocalFile = null;
- FileOutputStream foStream;
- BufferedOutputStream writer = null;
- try {
- DefaultHttpClient httpclient = new DefaultHttpClient();
- serverUrl = serverUrl.replace(" ", "%20");
- HttpGet httpget = new HttpGet(serverUrl);
- response = httpclient.execute(httpget);
- if (response == null)
- {
- return null;
- }
- entity = response.getEntity();
- strm = entity.getContent();
- File dire = new File(directory);
- if (!dire.isDirectory())
- {
- dire.mkdirs();
- }
- LocalFile = directory+LocalFilePath;
- directory = null;
- foStream = new FileOutputStream(LocalFile);
- writer = new BufferedOutputStream(foStream);
- do
- {
- bytesread = strm.read(bytes, 0, bytes.length);
- if(bytesread > 0)
- {
- writer.write(bytes, 0, bytesread);
- writer.flush();
- }
- } while (bytesread > 0);
- writer.close();
- foStream.close();
- strm.close();
- return LocalFile;
- }
- catch(Exception e)
- {
- file.delete();
- e.printStackTrace();
- return null;
- }
- }
- else
- {
- return "file exist";
- }
- }
Add Comment
Please, Sign In to add comment