Tôi làm cách nào để sử dụng thư viện để tải xuống tệp và in ra các byte được lưu? Tôi đã thử sử dụngTải xuống tệp bằng cách sử dụng commons java apache?
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);
}
}
nhưng tôi không thể hiển thị byte hoặc thanh tiến trình. Tôi nên sử dụng phương pháp nào?
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);
}
}
Làm cách nào để mở rộng apache để sử dụng? fl = new File (System.getProperty ("user.home") thay thế ("\\", "/") + "/Desktop/Screenshots.zip"); dl = URL mới ("http://ds-forums.com/kyle-tests/uploads/Screenshots.zip"); OutputStream os = new FileOutputStream (fl); InputStream là = dl.openStream(); CountingOutputStream count = new CountingOutputStream (os); dl.openConnection().getHeaderField ("Content-Length"); IOUtils.copy (là, os); // bắt đầu chuyển Tôi có làm đúng không? – Kyle
Bạn có nhớ thêm mã ở trên vào câu hỏi của mình không? Thật khó đọc. Tôi sẽ cố gắng giúp đỡ. – gigadot
Tôi đã thêm mã cảm ơn bạn đã trợ giúp. – Kyle