【发布时间】: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