【问题标题】:Download from URL into a Directory从 URL 下载到目录
【发布时间】:2014-01-03 22:43:49
【问题描述】:

我最近一直在研究这个“更新程序”,它会自动将文件从 URL 下载到目录中,尽管它没有按计划工作。每当我运行这个程序时,我总是收到错误消息(当文件无法访问时)弹出给我,从而告诉我文件没有下载。这个脚本可能有什么问题?

感谢您的帮助。

@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);
     }
}

【问题讨论】:

  • “错误消息”是指异常堆栈跟踪吗?如果是这样,请在您的问题中包含例外情况。
  • 你真的需要了解SSCCE。简而言之:仅发布可让我们重现您的错误的代码。不多也不少。
  • @Vulcan 所有出现的都是我制作的错误消息,“无法下载最新的客户端。请检查您的互联网连接并重试。”我可以告诉你,要下载的文件实际上是真实的,因为我可以转到地址栏中的那个链接,它工作得很好。
  • @Pshemo 我已经编辑过了。

标签: java jframe inputstream bufferedreader outputstream


【解决方案1】:

浏览你的代码我看到了方法

/**
 * 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();
    }
}

很好地捕捉异常并向用户展示,但您从未告诉自己错误是什么。这就是为什么大多数程序都有错误日志的原因!

只需输入System.out.println(e);,我就可以看到以下错误消息:

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

太好了,现在我在 Google 中搜索 Java URL open Stream 403,瞧,Google 上的最高结果将是:Server returning 403 for url openStream() 该 SO 问题中的第一个答案将使您的程序正常运行

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);

请记住,在进行任何研究之前,请务必查看错误消息。

【讨论】:

    猜你喜欢
    • 2020-02-12
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 2013-07-26
    • 2011-01-01
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    相关资源
    最近更新 更多