Witam, otoz znalazlem w siecie bardzo przyjemny kod na Pobieranie plikow z URL i zapis do wyznaczonej sciezki, jednak zauwazylem pewien problem.
Problemem okazalo sie pobieranie przykladowo paczki .zip z Dropboxa.
Ma ktos moze pomysl jak mozna poprawić kod by pobieralo?
public class HttpDownloader {
public static void main(String[] args) {
String fileURL = "https://www.dropbox.com/s/vc0dbvag0lff47a/Paczka.zip?dl=1";
String saveDir = "E:/Download";
try {
HttpDownloadUtility.downloadFile(fileURL, saveDir);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public class HttpDownloadUtility {
private static final int BUFFER_SIZE = 4096;
public static void downloadFile(String fileURL, String saveDir)
throws IOException {
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "";
String disposition = httpConn.getHeaderField("Content-Disposition");
String contentType = httpConn.getContentType();
int contentLength = httpConn.getContentLength();
if (disposition != null) {
int index = disposition.indexOf("filename=");
if (index > 0) {
fileName = disposition.substring(index + 10,
disposition.length() - 1);
}
} else {
fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
fileURL.length());
}
System.out.println("Content-Type = " + contentType);
System.out.println("Content-Disposition = " + disposition);
System.out.println("Content-Length = " + contentLength);
System.out.println("fileName = " + fileName);
InputStream inputStream = httpConn.getInputStream();
String saveFilePath = saveDir + File.separator + fileName;
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
int bytesRead = -1;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
System.out.println("File downloaded");
} else {
System.out.println("No file to download. Server replied HTTP code: " + responseCode);
}
httpConn.disconnect();
}
}
no boli
, czy raczej opisujesz dokładnie, co, gdzie i jak boli? Dlaczego zatem tutaj napisałeś tylko, żejest problem
, bez jego dokładnego opisu? (na przykład co tak właściwie jest problemem.)