Download via proxy

akkela

Grünschnabel
Java:
 public void downloadFile(String geturl, String output) throws Throwable {


        final URL url = new URL(geturl);


        final URLConnection conn = url.openConnection();
        
        if (jCheckBox1.isSelected()){


                String host = jTextField9.getText();
                String port = jTextField10.getText();
                String user = jTextField11.getText();
                String pw = jTextField12.getText();
                System.setProperty("proxyPort", port);
                System.setProperty("proxyHost", host);
                System.setProperty( "http.proxyUser", user );
                System.setProperty( "http.proxyPassword", pw );

            }
        

        final InputStream is = new BufferedInputStream(conn.getInputStream());
        final OutputStream os =
                new BufferedOutputStream(new FileOutputStream(output));
        int fsize = (int) size;

        byte[] chunk = new byte[fsize];
        int chunkSize;
        while ((chunkSize = is.read(chunk)) != -1) {
            os.write(chunk, 0, chunkSize);

        }
        os.flush();
        os.close();
        is.close();
    }

Moin Gemeinde,
diese methode läuft eigentlich einwandfrei. nun will ich das an einem rechner mit proxy benutzen und das was ich bis jetzt gefunden hab - läuft nicht :(

kann da vllt einer mithelfen?
 
Zuletzt bearbeitet:
Vielleicht solltest du die Verbindung erst dann öffnen, wenn du den Proxy eingestellt hast? Wenn du erst eine Verbindung aufbaust, dann die Proxy-Einstellungen veränderst und danach versuchst herunter zu laden, muss das schief gehen!
 
ach, so einfach ging das doch :)
Danke Fabio

Hier die Lösung

Java:
 public void downloadFile(String geturl, String output) throws Throwable {


        final URL url = new URL(geturl);


        final URLConnection conn = url.openConnection();

        if (jCheckBox1.isSelected()){
                String user = jTextField11.getText();
                String pw = jTextField12.getText();
                "Basic " + new sun.misc.BASE64Encoder().encode((user + ":" + pw).getBytes()) );


            }

        final InputStream is = new BufferedInputStream(conn.getInputStream());
        final OutputStream os =
                new BufferedOutputStream(new FileOutputStream(output));
        int fsize = (int) size;

        byte[] chunk = new byte[fsize];
        int chunkSize;
        while ((chunkSize = is.read(chunk)) != -1) {
            os.write(chunk, 0, chunkSize);

        }
        os.flush();
        os.close();
        is.close();
    }

würde mal sagen - CLOSED
 
Zuletzt bearbeitet:
Zurück