我试图将进度监视器输入流放入我的代码中,但我所做的一切都做不到。我对 Java 编程非常陌生,在代码中实现某些功能时遇到一些困难。
除了无法使用输入流之外,代码可以按照我需要的方式正常工作。所以我想查看下载进度,因为它有异常(exception)情况,以防下载失败或服务器停机。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javatar.language.download;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JOptionPane;
/**
*
* @author Bruno
*/
public class UrlInput {
public static void main(String[] args) {
String userSys = System.getenv("USERNAME");
String sysDrive = System.getenv("SYSTEMDRIVE");
String downPath = sysDrive + "/Users/" + userSys + "/Downloads";
try {
URL url = new URL("http://akamai-gamecdn.blackdesertonline.com/live001/game/config/config.language.version");
try ( // read text returned by server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))) {
String line;
while ((line = in.readLine()) != null) {
String versao = line;
JOptionPane.showMessageDialog(null, "Actual version BDO-NA: " + versao);
String fileURL = "http://akamai-gamecdn.blackdesertonline.com/live001/game/language/BDOLanguage_" + versao + ".zip";
String saveDIR = downPath;
SysDownload.downloadFile(fileURL, saveDIR);
}
}
} catch (MalformedURLException e) {
JOptionPane.showMessageDialog(null, "Malformed URL: " + e.getMessage());
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Error I/O: " + e.getMessage());
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javatar.language.download;
import java.io.*;
import java.net.*;
import javax.swing.*;
/**
*
* @author Bruno
*/
public class SysDownload {
private static final int BUFFER_SIZE = 4096;
public static void downloadFile(String fileURL, String saveDIR) throws IOException {
URL link = new URL(fileURL);
HttpURLConnection conn = (HttpURLConnection) link.openConnection();
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "";
String server = conn.getHeaderField("Server");
String connectionType = conn.getContentType();
int contentLenght = conn.getContentLength();
JOptionPane.showMessageDialog(null, "Server name: " + server);
if (server != null) {
int index = server.indexOf("filename");
if (index > 0) {
fileName = server.substring(index + 10, server.length() - 1);
} else {
fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1, fileURL.length());
}
try (InputStream inputStream = conn.getInputStream()) {
String savePath = saveDIR + File.separator + fileName;
try (FileOutputStream outputStream = new FileOutputStream(savePath)) {
int bytesRead = - 1;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
}
}
JOptionPane.showMessageDialog(null, "File " + fileName + " has been downloaded.\nSee users Download folder.");
} else {
JOptionPane.showMessageDialog(null, "None file downloaded.\nServer HTTP code: " + responseCode + JOptionPane.ERROR_MESSAGE);
}
conn.disconnect();
}
}
}
最佳答案
首先咨询the documentation of ProgressMonitorInputStream .
- 构造函数需要三个参数。与 JOptionPane 一样,第一个参数是对话框父级,在您的应用程序中它显示为
null
。 - 第二个参数是消息。就您而言,
“下载”+链接
可能就足够了。 - 第三个参数是要监视的InputStream。这应该是您要从中下载的输入流。
ProgressMonitor的最大值应该是下载的大小,您可以从 getContentLength() 获取该大小。 URLConnection 方法。
您不需要编写循环来将 InputStream 保存到文件中。您可以使用Files.copy为此。
所以,把它们放在一起,看起来像这样:
try (ProgressMonitorInputStream inputStream =
new ProgressMonitorInputStream(null,
"Downloading " + link,
conn.getInputStream())) {
inputStream.getProgressMonitor().setMaximum(conn.getContentLength());
Files.copy(inputStream, Paths.get(savePath));
}
关于java - 如何在我的代码中放置输入流进度监视器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58329018/