comment
IRC Chat
play_arrow
Este sitio utiliza cookies propias y de terceros. Si continúa navegando consideramos que acepta el uso de cookies. OK Más Información.

Descargar archivos de la web usando un progressBar

  • 0 Respuestas
  • 2217 Vistas

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado ProcessKill

  • *
  • Underc0der
  • Mensajes: 154
  • Actividad:
    0%
  • Reputación 0
  • Arriba los hackers de Argentina!
    • Ver Perfil
    • Email
« en: Febrero 24, 2010, 04:16:01 pm »
[ -Consola ]
____________________________________________________________________________________________

Wait..
You are not allowed to view links. Register or Login
______________________________________________________

Date: Sat Sep 05 10:56:19 VET 2009
Last Modified: Tue Feb 08 11:12:02 VET 2005
Expiration: Wed Dec 31 20:00:00 VET 1969
Host: You are not allowed to view links. Register or Login Port: 80
Path: /asignaturas/Informat1/ayudainf/aprendainf/Java/Java2.pdf
Name: Java2.pdf
Size: 2050138
Protocol: http://
______________________________________________________
Init.
Receiving data..


____________________________________________________________________________________________

[ -CODE -]

Código: Java
  1. import java.awt.Color;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8. import java.util.Date;
  9. import javax.swing.JFrame;
  10. import javax.swing.JOptionPane;
  11. import javax.swing.JProgressBar;
  12.  
  13. /**
  14. * Hilo para descarga de un archivo desde una URL
  15. * implementando un progressbar
  16. *
  17. * @author L-EYER
  18. *
  19. * @see GlassFish Tools Bundle For Eclipse
  20.    Version: 0.9.9
  21. */
  22. public class Downloader extends You are not allowed to view links. Register or Login{
  23.    private You are not allowed to view links. Register or Login           url          =   null;
  24.    private You are not allowed to view links. Register or Login  progressBar  =   null;
  25.    private You are not allowed to view links. Register or Login connection   =   null;
  26.    /**
  27.    * Constructor
  28.    *
  29.    * @param URL
  30.    * @param Barra de Progreso
  31.    */
  32.    public Downloader(
  33.          final You are not allowed to view links. Register or Login You are not allowed to view links. Register or Login,
  34.          You are not allowed to view links. Register or Login progress) {
  35.    super("Downloader");
  36.    this.url = You are not allowed to view links. Register or Login; this.progressBar = progress;
  37.    progressBar.setStringPainted(true);}
  38.    @Override
  39.    public void run() {
  40.       try {
  41.        You are not allowed to view links. Register or Login.out.println("Wait..");
  42.        connection = url.openConnection();
  43.        connection.connect();
  44.           progressBar.setMinimum(0);
  45.           progressBar.setForeground(new You are not allowed to view links. Register or Login(160,20,9,100));
  46.           progressBar.setBorderPainted(true);
  47.          } catch (You are not allowed to view links. Register or Login e) {You are not allowed to view links. Register or Login.err.println("Error in conection!");You are not allowed to view links. Register or Login.exit(0);
  48.          } catch (You are not allowed to view links. Register or Login e) {
  49.             You are not allowed to view links. Register or Login.err.println(e);You are not allowed to view links. Register or Login.exit(0);
  50.          }
  51.       int read = 0;
  52.       final int SIZE = getLength(url);
  53.       progressBar.setMaximum(SIZE);
  54.       You are not allowed to view links. Register or Login stream  =  null;
  55.       try {
  56.          stream = connection.getInputStream();
  57.          You are not allowed to view links. Register or Login.out.println(url.toExternalForm());
  58.          You are not allowed to view links. Register or Login.out.println("______________________________________________________\n");
  59.          You are not allowed to view links. Register or Login.out.println("Date: "+new You are not allowed to view links. Register or Login(connection.getDate()));
  60.          You are not allowed to view links. Register or Login.out.println("Last Modified: "+ new You are not allowed to view links. Register or Login(connection.getLastModified()));
  61.          You are not allowed to view links. Register or Login.out.println("Expiration: " + new You are not allowed to view links. Register or Login(connection.getExpiration()));
  62.          You are not allowed to view links. Register or Login.out.println("Host: "+url.getHost()+" Port: "+url.getDefaultPort());
  63.          You are not allowed to view links. Register or Login.out.println("Path: "+url.getPath());
  64.          You are not allowed to view links. Register or Login.out.println("Name: "+getFileName(url));
  65.          You are not allowed to view links. Register or Login.out.println("Size: "+getLength(url));
  66.          You are not allowed to view links. Register or Login.out.println("Protocol:"+url.getProtocol()+"://");
  67.          You are not allowed to view links. Register or Login.out.println("______________________________________________________");
  68.          final You are not allowed to view links. Register or Login fileOutputStream = new You are not allowed to view links. Register or Login(getFileName(url));
  69.          final byte[] data =
  70.             new byte[ SIZE ];
  71.           try {
  72.                You are not allowed to view links. Register or Login.sleep(100);
  73.             } catch (You are not allowed to view links. Register or Login e) {You are not allowed to view links. Register or Login.err.println(e);You are not allowed to view links. Register or Login.exit(0);
  74.             }
  75.          int offset = 0;
  76.          You are not allowed to view links. Register or Login.out.println("Init.");
  77.          You are not allowed to view links. Register or Login.out.println("Receiving data..");
  78.          while((read = stream.read(data)) > 0){
  79.              offset += read;
  80.             progressBar.setValue(offset);
  81.             fileOutputStream.write(data,
  82.                   0,
  83.                   read);
  84.          }
  85.          You are not allowed to view links. Register or Login.out.println("Completed! 100%");
  86.          You are not allowed to view links. Register or Login.showMessageDialog(new You are not allowed to view links. Register or Login(),
  87.                "Completed!",
  88.                "100%",
  89.                You are not allowed to view links. Register or Login.INFORMATION_MESSAGE);
  90.          progressBar.setValue(0);
  91.          try{
  92.          stream.close();
  93.          fileOutputStream.flush();
  94.          fileOutputStream.close();
  95.              offset = 0;
  96.             }catch (You are not allowed to view links. Register or Login e) {You are not allowed to view links. Register or Login.err.println(e);You are not allowed to view links. Register or Login.exit(0);
  97.             }
  98.       }catch (You are not allowed to view links. Register or Login e) {
  99.       You are not allowed to view links. Register or Login.err.println(e+"ERROR:URL");You are not allowed to view links. Register or Login.exit(0);
  100.         }catch (You are not allowed to view links. Register or Login e) {
  101.       You are not allowed to view links. Register or Login.err.println(e);
  102.        }
  103.        super.run();
  104.       }
  105.        final int getLength(You are not allowed to view links. Register or Login urlFile){
  106.       You are not allowed to view links. Register or Login connection = null;
  107.       int size = 0;
  108.       try {
  109.       connection = urlFile.openConnection();
  110.          size = connection.getContentLength();
  111.       } catch (You are not allowed to view links. Register or Login e) {
  112.          You are not allowed to view links. Register or Login.err.println(e+":ERROR");You are not allowed to view links. Register or Login.exit(0);
  113.       } catch (You are not allowed to view links. Register or Login e) {
  114.          You are not allowed to view links. Register or Login.err.println(e+":ERROR");You are not allowed to view links. Register or Login.exit(0);
  115.       }
  116.       return size;
  117.    }
  118.    You are not allowed to view links. Register or Login getFileName(You are not allowed to view links. Register or Login You are not allowed to view links. Register or Login){
  119.      You are not allowed to view links. Register or Login path=You are not allowed to view links. Register or Login.getPath();
  120.      int lastIndexOf=path.lastIndexOf("/");
  121.      You are not allowed to view links. Register or Login name = path.substring(lastIndexOf+1);
  122.        return name;
  123.    }
  124. } class mainClass {
  125.    public static void main(final You are not allowed to view links. Register or Login[] args){
  126.       try{
  127.          final You are not allowed to view links. Register or Login progressBar = new You are not allowed to view links. Register or Login();
  128.          You are not allowed to view links. Register or Login url = new You are not allowed to view links. Register or Login("http://www.tecnun.es/asignaturas/Informat1/ayudainf/aprendainf/Java/Java2.pdf");
  129.          Downloader downloader = new Downloader(url,progressBar);
  130.          downloader.start();
  131.       } catch (You are not allowed to view links. Register or Login e) {
  132.       You are not allowed to view links. Register or Login.err.println("Error: Protocol"+e);
  133.       You are not allowed to view links. Register or Login.exit(0);
  134.       } catch (You are not allowed to view links. Register or Login e) {
  135.          You are not allowed to view links. Register or Login.err.println(e);
  136.       }
  137.    }
« Última modificación: Abril 18, 2013, 12:44:20 pm por Expermicid »

 

¿Te gustó el post? COMPARTILO!



× Neptuno × Copia archivos de manera sigilosa

Iniciado por Mortal_Poison

Respuestas: 6
Vistas: 1803
Último mensaje Junio 06, 2018, 03:54:48 pm
por PikachuDorado
Subir archivos con Servlet

Iniciado por Gus Garsaky

Respuestas: 2
Vistas: 3593
Último mensaje Septiembre 25, 2015, 06:15:35 pm
por LKI
Buscador de archivos

Iniciado por rollth

Respuestas: 1
Vistas: 1941
Último mensaje Enero 17, 2016, 03:11:59 pm
por Hu3c0
Como convertir un jar a exe usando IExpress

Iniciado por BigBear

Respuestas: 1
Vistas: 1524
Último mensaje Febrero 26, 2014, 05:42:21 pm
por algorf