Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- public class Downloader {
- private URL url;
- private byte[] buffer;
- public Downloader(String url)
- {
- try {
- this.url = new URL(url);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- }
- public void download() throws IOException
- {
- if (this.url == null)
- return;
- DataInputStream is = null;
- DataOutputStream os = null;
- try {
- // TODO pouzijte promennou url a ziskejte z ni InputStream OK
- // TODO pouzijte Buffer OK
- is = new DataInputStream(url.openStream());
- buffer = new byte[1024*1024];
- int length = 0;
- // TODO z promenne url ziskejte jmeno souboru OK
- // TODO jmeno souboru je vcetne cesty. Orezte dany string a ziskejte pouze jmeno s priponou OK
- String fileName = url.getPath();
- fileName = fileName.replace("/", "");
- //System.out.println(fileName);
- // TODO vytvorte soubor s nazvem z promenne fileName OK
- // TODO pouzijte Buffer
- File file = new File(fileName);
- os = new DataOutputStream(new FileOutputStream(file));
- int c;
- // TODO zapiste vsechna data z is do os. Pouzijte promennou c OK
- // while ((c = is.read()) != -1) {
- // os.write(c);
- // }
- while ((length = is.read(buffer)) >= 0){
- os.write(buffer, 0, length);
- }
- }
- // TODO doplnte block finally kde oba streamy zavrete pokud nejsou null
- finally
- {
- if (os != null) os.close();
- if (is != null) is.close();
- }
- }
- }
Add Comment
Please, Sign In to add comment