Skocz do zawartości

Historia edycji

globox99

globox99

Witam, mam pewien problem.. Próbuję pobrać pliczek z URL przy pomocy JAVY ale nie ważne jakiego kodu używam to zawsze pobiera niekompletny plik. Jakby 1/10 całości..

Oto mój kod:

    public void file() throws IOException{
        InputStream in = null;
        FileOutputStream out = null;
        try {
            System.out.println("Starting download");
            long t1 = System.currentTimeMillis();
            URL url = new URL("http://www.minecraftpolska.net");// or you can hard code the URL
            // Open the input and out files for the streams
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            in = conn.getInputStream();
            out = new FileOutputStream("jakispliczek.jar");//// or you can hard code the filename
            // Read data into buffer and then write to the output file
            byte[] buffer = new byte[8192];
            int bytesRead;
            while ((bytesRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesRead);
            }
            long t2 = System.currentTimeMillis();
            System.out.println("Time for download & save file in millis:"+(t2-t1));
        } catch (Exception e) {
            // Display or throw the error
            System.out.println("Erorr while execting the program: "
                    + e.getMessage());
        } finally {
            // Close the resources
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }

 

globox99

globox99

Witam, mam pewien problem.. Próbuję pobrać pliczek z URL przy pomocy JAVY ale nie ważne jakiego kodu używam to zawsze pobiera niekompletny plik. Jakby 1/10 całości..

Oto mój kod:

    public void file() throws IOException{
        InputStream in = null;
        FileOutputStream out = null;
        try {
            System.out.println("Starting download");
            long t1 = System.currentTimeMillis();
            URL url = new URL("http://www.minecraftpolska.net/smap");// or you can hard code the URL
            // Open the input and out files for the streams
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            in = conn.getInputStream();
            out = new FileOutputStream("McPolskaSMS.jar");//// or you can hard code the filename
            // Read data into buffer and then write to the output file
            byte[] buffer = new byte[8192];
            int bytesRead;
            while ((bytesRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesRead);
            }
            long t2 = System.currentTimeMillis();
            System.out.println("Time for download & save file in millis:"+(t2-t1));
        } catch (Exception e) {
            // Display or throw the error
            System.out.println("Erorr while execting the program: "
                    + e.getMessage());
        } finally {
            // Close the resources
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }

 

×