diff --git a/README.md b/README.md index c1ff47b823ea322e6bd78edfa03a692d7a0eac29..4fbd083586a66db7da0bab511c34468435d04e32 100644 --- a/README.md +++ b/README.md @@ -107,12 +107,12 @@ unzip dossier1.zip curl -X PUT -H "Authorization: Bearer valid-token-1" -H "X-FTP-User: user" -H "X-FTP-Pass: password" --upload-file fichier2 http://localhost:8080/ftps/mon-ftp/fichier2 ``` -- upload dossier (ne fonctionne pas bien pour le moment je crois) : +- upload dossier : ```shell -curl -X PUT -H "Authorization: Bearer valid-token-1" -H "X-FTP-User: user" -H "X-FTP-Pass: password" --upload-file dossier2.zip http://localhost:8080/ftps/mon-ftp/dossier2.zip +zip -r dossier2.zip dossier2 -curl -X POST -H "Authorization: Bearer valid-token-1" -H "X-FTP-User: user" -H "X-FTP-Pass: password" -H "Content-Type: application/octet-stream" --data-binary @dossier2.zip http://localhost:8080/ftps/mon-ftp/upload-dir/ +curl -X POST -H "Authorization: Bearer valid-token-1" -H "X-FTP-User: user" -H "X-FTP-Pass: password" -H "Content-Type: application/octet-stream" --data-binary @dossier2.zip http://localhost:8080/ftps/mon-ftp/upload-dir/dossier2 ``` **Note comprise entre 12 et 13 si—en plus—le proxy FlopBox, permet de télécharger (download) et téléverser (upload) un gros fichier binaire (image, vidéo, etc.):** diff --git a/src/main/java/fil/sr2/flopbox/FTPService.java b/src/main/java/fil/sr2/flopbox/FTPService.java index 8997b02a3764126b36af0d0c6685f7b5c9d6bb58..6319864e81687bce113a114df0192cf914381260 100644 --- a/src/main/java/fil/sr2/flopbox/FTPService.java +++ b/src/main/java/fil/sr2/flopbox/FTPService.java @@ -13,6 +13,7 @@ import java.util.zip.ZipInputStream; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; import java.util.Arrays; +import java.io.ByteArrayInputStream; public class FTPService { @@ -350,12 +351,22 @@ public class FTPService { } } - private void uploadFileEntry(FTPClient ftp, String path, InputStream is) + private void uploadFileEntry(FTPClient ftp, String path, ZipInputStream zis) throws IOException, FTPException { String fileName = path.contains("/") ? path.substring(path.lastIndexOf('/') + 1) : path; - try (InputStream bais = is) { - if (!ftp.storeFile(fileName, bais)) { + + // Lire le contenu du fichier dans un tableau d'octets + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + byte[] buffer = new byte[4096]; + int bytesRead; + while ((bytesRead = zis.read(buffer)) != -1) { + baos.write(buffer, 0, bytesRead); + } + + // Uploader le contenu depuis le tableau d'octets + try (InputStream bis = new ByteArrayInputStream(baos.toByteArray())) { + if (!ftp.storeFile(fileName, bis)) { throw new FTPException("Échec upload: " + path, 500); } }