Voici mon code.J'écris ceci pour télécharger des mouches mp3, des fichiers vidéo & images. i utilisé FileOutputStream pour la gestion des fichiers .. Tous téléchargement de fichiers bien .. fichiers mp3 sont des images et des vidéos working..but sont corrompusTélécharger le fichier avec java - Fichiers corrompus
private void download(String fileURL, String destinationDirectory,String name) throws IOException {
// File name that is being downloaded
String downloadedFileName = name;
// Open connection to the file
URL url = new URL(fileURL);
InputStream is = url.openStream();
// Stream to the destionation file
FileOutputStream fos = new FileOutputStream(destinationDirectory + "/" + downloadedFileName);
// Read bytes from URL to the local file
byte[] buffer = new byte[4096];
int bytesRead = 0;
System.out.println("Downloading " + downloadedFileName);
while ((bytesRead = is.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
// Close destination stream
fos.close();
// Close URL stream
is.close();
}
Ce code devrait fonctionner correctement ... Mais vous devriez apprendre à ouvrir et fermer vos ressources correctement. En particulier, utilisez une instruction try-with-resources. – fge