ライブラリを使用してファイルをダウンロードし、保存されたバイトをプリントアウトするにはどうすればよいですか?私は使用しようとしましたjava apache commonsを使用してファイルをダウンロードしますか?
import static org.apache.commons.io.FileUtils.copyURLToFile;
public static void Download() {
URL dl = null;
File fl = null;
try {
fl = new File(System.getProperty("user.home").replace("\\", "/") + "/Desktop/Screenshots.zip");
dl = new URL("http://ds-forums.com/kyle-tests/uploads/Screenshots.zip");
copyURLToFile(dl, fl);
} catch (Exception e) {
System.out.println(e);
}
}
しかし、私はバイトまたはプログレスバーを表示することはできません。どちらの方法を使うべきですか?
public class download {
public static void Download() {
URL dl = null;
File fl = null;
String x = null;
try {
fl = new File(System.getProperty("user.home").replace("\\", "/") + "/Desktop/Screenshots.zip");
dl = new URL("http://ds-forums.com/kyle-tests/uploads/Screenshots.zip");
OutputStream os = new FileOutputStream(fl);
InputStream is = dl.openStream();
CountingOutputStream count = new CountingOutputStream(os);
dl.openConnection().getHeaderField("Content-Length");
IOUtils.copy(is, os);//begin transfer
os.close();//close streams
is.close();//^
} catch (Exception e) {
System.out.println(e);
}
}
私はそれを使うためにどのようにApacheを拡張するのですか? fl = new File(System.getProperty( "user.home")。replace( "\\"、 "/")+ "/Desktop/Screenshots.zip"); dl =新しいURL( "http://ds-forums.com/kyle-tests/uploads/Screenshots.zip"); OutputStream os =新しいFileOutputStream(fl); InputStream is = dl.openStream(); CountingOutputStream count =新しいCountingOutputStream(os); dl.openConnection()。getHeaderField( "Content-Length"); IOUtils.copy(is、os); //転送を開始します 私はそれを正しくやっていますか? – Kyle
あなたの質問に上記のコードを追加してもよろしいですか?読むのは難しいです。私は助けようとします。 – gigadot
コードを追加していただきありがとうございます。 – Kyle