public static String downloadURL(String urlIn, String folderOut, String mongoHostIP) throws IOException { System.out.println("downloadURL"); String imgHash = null; byte[] data = null; // connect to URL and get input stream URL imageURL = new URL(urlIn); File localDir = new File(folderOut); localDir.mkdir(); InputStream inputStream = null; URLConnection urlConnection = null; int noOfBytes = 0; byte[] byteChunk = new byte[4096]; ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); urlConnection = imageURL.openConnection(); urlConnection.addRequestProperty( "User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"); urlConnection.connect(); inputStream = urlConnection.getInputStream(); while ((noOfBytes = inputStream.read(byteChunk)) > 0) { byteOutputStream.write(byteChunk, 0, noOfBytes); } // hash creation from image file try { System.out.println("Start MD5 Digest"); data = byteOutputStream.toByteArray(); MessageDigest md = MessageDigest.getInstance("MD5"); md.update(data); byte[] hash = md.digest(); imgHash = String.format("%032x", new java.math.BigInteger(1, hash)); System.out.println("Hash : " + imgHash); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } MongoClient mongoclient = new MongoClient(mongoHostIP, 27017); // System.out.println("mongoHostIP :: " + mongoHostIP); Morphia morphia = new Morphia(); morphia.map(ForensicReport.class).map(dqReport.class); Datastore ds = new Morphia().createDatastore(mongoclient, "ForensicDatabase"); ds.ensureCaps(); String baseFolder = folderOut + imgHash + "/"; ForensicReport report = ds.get(ForensicReport.class, imgHash); // check if hash exist if (report != null) { System.out.println("Exists"); } else { // if hash does not exist in database, then download the image report = new ForensicReport(); report.id = imgHash; try { File writeFolder = new File(baseFolder); if (!writeFolder.exists()) writeFolder.mkdirs(); File imageFile = new File(baseFolder, "Raw"); OutputStream outputStream = new FileOutputStream(imageFile); byteOutputStream.writeTo(outputStream); outputStream.close(); BufferedImage downloadedImage = ImageIO.read(imageFile); ImageIO.write(downloadedImage, "JPEG", new File(baseFolder, "Display.jpg")); // store in database image information report.sourceImage = baseFolder + "Raw"; report.displayImage = baseFolder + "Display.jpg"; report.sourceURL = urlIn; report.status = "Downloaded"; ds.save(report); } catch (Exception e) { e.printStackTrace(); mongoclient.close(); return "URL_ERROR"; } } mongoclient.close(); System.out.println("Downloaded."); return imgHash; }