Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Downloading files and verifying MD5 checksums

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Downloading files and verifying MD5 checksums

    So this code works for 95% of people who use my application but sometimes the file seems to partially corrupt or whatever as it claims the MD5 checksum is different and then tries downloading again. It only really seems to happen with slow connections but the file downloads fine if they open up their web browser and download it directly.

    Any suggestions?

    PHP Code:
    public void update(boolean hasInstallDirectory) {
        for (
    String nextMirror mirrors) {
            try {
                
    jout("Attempting to download...");
                
    java.net.URLConnection updateConnection null;
                try {
                    
    updateConnection = new java.net.URL(nextMirror).openConnection();
                } catch (
    MalformedURLException e1) {
                    
    jout("Mirror down: " mirrors.indexOf(nextMirror) + 1);
                } catch (
    IOException e1) {
                    
    jout("Mirror down: " mirrors.indexOf(nextMirror) + 1);
                }
                
    int l updateConnection.getContentLength();
                  
    byte[] arrayOfByte = new byte[l];
                  
    java.io.BufferedInputStream localBufferedInputStream null;
                try {
                    
    localBufferedInputStream = new java.io.BufferedInputStream(updateConnection.getInputStream());
                } catch (
    IOException e1) {
                    
    jout("An error has occured while reading from the mirror...");
                    
    jout("Please post a new thread in the support section of the forum.");
                }
                
    int i1 0;
                
    int i2 0;
                
    this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                
    jProgressBar1.setStringPainted(true);
                while (
    i2 l) {
                    try {
                        
    i1 localBufferedInputStream.read(arrayOfBytei2arrayOfByte.length i2);
                    } catch (
    IOException e) {
                        
    jout("An error has occured while reading from the mirror...");
                        
    jout("Please post a new thread in the support section of the forum.");
                    }
                    if (
    i1 == -1)
                        break;
                    
    i2 += i1;
                    
    jProgressBar1.setValue((int)(((float)i2 / (float)l) * 100F));
                }
                
    this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR ));
                
    java.io.FileOutputStream localFileOutputStream null;
                try {
                    
    localFileOutputStream = new java.io.FileOutputStream(JAR);
                } catch (
    FileNotFoundException e1) {
                    
    jout("An error has occured while opening the file output stream...");
                    
    jout("Please post a new thread in the support section of the forum.");
                }
                try {
                    
    localFileOutputStream.write(arrayOfByte);
                } catch (
    IOException e) {
                    
    jout("An error has occured writing to disk...");
                    
    jout("Please post a new thread in the support section of the forum.");
                    
    e.printStackTrace();
                }
                try {
                    
    localFileOutputStream.flush();
                    
    localFileOutputStream.close();
                    
    localBufferedInputStream.close();                        
                } catch (
    IOException e) {
                    
    jout("Error finalizing streams...");
                    
    jout("Please post a new thread in the support section of the forum.");
                    
    e.printStackTrace();
                }
                if (!
    requiresUpdate(JAR)) {
                    
    unpack();
                    
    jButton1.setEnabled(true);
                } else {
                    
    jout("Hash mismatch after download! Retrying...");
                    
    update(true);
                }
                break;
            } catch(
    Exception e) {
                
    jout("Generic Exception: " e.getMessage());
                continue;
            }
        }



  2. #2
    Junior Member
    Join Date
    Oct 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Downloading files and verifying MD5 checksums

    Anyone?

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Downloading files and verifying MD5 checksums

    I might be the only helper on the forum right now, and I'm actually looking at going to sleep. The bad news is that I don't actually have a grasp of the URL Connect and that sort of stuff. However, there are plenty of people on the forum that do and they will help you. Wait about 24 hours, as most of the helpers seem to come on around the same time, and that isnt for another 10-12 hours at the earliest. Regardless, I will be watching this post as I'm interested in learning about URL Connect and that sort of crap.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Downloading files and verifying MD5 checksums

    I'm not sure this is necessarily a problem with the code as it is the connection, especially since this works with 95% of users. As per the BufferedReader api, if the ready() method returns false the read will return, which could result in an unfinished read. You could try skipping the BufferedReader, and just use Reader read function.

Similar Threads

  1. need help on jar files
    By kaka09 in forum AWT / Java Swing
    Replies: 2
    Last Post: July 13th, 2010, 09:30 AM
  2. Downloading help with JDK 6
    By oneofthelions in forum Java Theory & Questions
    Replies: 1
    Last Post: December 13th, 2009, 07:36 AM
  3. Java program for downloading contents from the web
    By alley in forum Java Networking
    Replies: 8
    Last Post: June 17th, 2009, 01:13 PM
  4. Trouble in downloading java
    By captjade in forum Java Theory & Questions
    Replies: 6
    Last Post: March 3rd, 2009, 04:16 PM
  5. How to know number of user downloading an application?
    By jazz2k8 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 3rd, 2008, 04:34 AM