Frage

I am recently been working on this "Updater" which automatically downloads a file from a URL into a directory, although it is not working as planned. Whenever I run this program, I am always getting the error message (when the file cannot be accessed) popping up to me, thus telling me the file is not downloading. What may be wrong with this script?

Thanks for any help.

@SuppressWarnings("serial")
public class GameLauncher extends JFrame {

/**
 * Directory of the Download Client.
 * <p>
 * This is the location the Jar filed will be stored and executed at.
 */
static String directory = System.getProperty("user.home") + "/LRLauncher/LostRedemption.jar";

/**
 * Download link of the Download Client.
 */
static String downloadLink = "http://lostredemption.com/LostRedemption.jar";

/**
 * Creates the main frame of the application being initiated.
 * @throws IOException 
 */


  /**
 * Download the client.
 */
static void downloadFile() {
        try {
                URL url2 = new URL("http://lostredemption.com/LostRedemption.jar");
                InputStream in = new BufferedInputStream(url2.openStream());
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];
                int n = 0;
                while (-1 != (n = in .read(buf))) {
                        out.write(buf, 0, n);
                }
                out.close(); 
                in .close();
                byte[] response = out.toByteArray();
                FileOutputStream fos = new FileOutputStream(directory);
                fos.write(response);
                fos.close();
                //openDownloadClient();
        }
        catch (IOException e) {
                JOptionPane.showMessageDialog(null, "Unable to download latest client. Please check your internet connection and try again.", "Error", JOptionPane.ERROR_MESSAGE);
                closeProgram();
        }
}

/**
 * Open the Download Client.
 */
static void openDownloadClient() {
        File execute = new File(directory);
        try {
                Desktop.getDesktop().open(execute);
        }
        catch (IOException e) {
                e.printStackTrace();
        }
        closeProgram();
}

/**
 * Close the program.
 */
static void closeProgram() {
        System.exit(0);
     }
}
War es hilfreich?

Lösung

Glacing over your code I see the method

/**
 * Download the client.
 */
static void downloadFile() {
    try {
            URL url2 = new URL("http://lostredemption.com/LostRedemption.jar");
            InputStream in = new BufferedInputStream(url2.openStream());
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int n = 0;
            while (-1 != (n = in .read(buf))) {
                    out.write(buf, 0, n);
            }
            out.close(); 
            in .close();
            byte[] response = out.toByteArray();
            FileOutputStream fos = new FileOutputStream(directory);
            fos.write(response);
            fos.close();
            //openDownloadClient();
    }
    catch (IOException e) {
            JOptionPane.showMessageDialog(null, "Unable to download latest client. Please check your internet connection and try again.", "Error", JOptionPane.ERROR_MESSAGE);
            closeProgram();
    }
}

Nice job catching the exception and showing the user but you never told yourself what the error was. This is why most programs have error logs!

Just by simply putting a System.out.println(e); I can see this error message:

java.io.IOException: Server returned HTTP response code: 403 for URL: http://lostredemption.com/LostRedemption.jar

Great now I search Java URL open Stream 403 in Google and voila top result on Google will yeild: Server returning 403 for url openStream() the first answer in that SO question will get your program working

URL url2 = new URL("http://lostredemption.com/LostRedemption.jar");
HttpURLConnection httpcon = (HttpURLConnection) url2.openConnection();
httpcon.addRequestProperty("User-Agent", "Mozilla/4.0");
ReadableByteChannel rbc = Channels.newChannel(httpcon.getInputStream());
FileOutputStream fos = new FileOutputStream(directory);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

Remember always look at your error messages before you do any research.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top