我最近一直在研究这个“更新程序”,它会自动将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);
}
}
答案 0 :(得分:0)
对代码进行布局我看到了方法
/**
* 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
现在很棒,我在谷歌搜索Java URL open Stream 403
,谷歌的最终结果将是最终结果: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);
请记住,在进行任何研究之前,请始终查看错误消息。