public static Data readFileAsData(String locationURL,String mime) throws IOException, NoSuchAlgorithmException { URL url = new URL(locationURL); URLConnection connection = url.openConnection(); if (!connection.getContentType().equalsIgnoreCase(mime)) { return null; } InputStream in = url.openStream(); ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); byte[] block = new byte[10240]; int len; while ((len = in.read(block)) >= 0) { byteOut.write(block, 0, len); } in.close(); byte[] body = byteOut.toByteArray(); Data data = new Data(); data.setSize(body.length); data.setBodyHash(MessageDigest.getInstance("MD5").digest(body)); data.setBody(body); return data; }