Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void downloadFile(String fileUrl)
- throws IOException {
- String fileName = fileUrl.substring(fileUrl.lastIndexOf("/")+1);
- URL url = new URL(fileUrl);
- InputStream is = url.openStream();
- FileOutputStream fos = new FileOutputStream("/Users/user/Desktop/" + fileName);
- int test = getFileSize(url);
- System.out.println(test);
- byte[] buffer = new byte[4096];
- int bytesRead = 0;
- System.out.print("Downloading " + fileName);
- while ((bytesRead = is.read(buffer)) != -1) {
- fos.write(buffer, 0, bytesRead);
- }
- System.out.print("done!");
- fos.close();
- is.close();
- }
- public static int getFileSize(URL url) {
- HttpURLConnection conn = null;
- try {
- conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("HEAD");
- conn.getInputStream();
- return conn.getContentLength();
- } catch (IOException e) {
- throw new RuntimeException(e);
- } finally {
- conn.disconnect();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment