Question

en utilisant le code java dans windows-je télécharger plusieurs fichiers à partir d'un répertoire placé dans un serveur. ces fichiers dans le serveur sont générés séparément. donc je sais pas le nom de ces fichiers. est-il possible de le télécharger en utilisant JAVA et l'enregistrer dans un dossier spécifique.

J'utilise tomcat apache.

Je lis tous les autres threads liés à télécharger le fichier java. Mais aucun d'entre eux satisfaire mes besoins.

Était-ce utile?

La solution

Use java.net.URL and java.net.URLConnection classes.

Autres conseils

  try {
        // Get the directory and iterate them to get file by file...
        File file = new File(fileName);

        if (!file.exists()) {
            context.addMessage(new ErrorMessage("msg.file.notdownloaded"));
            context.setForwardName("failure");
        } else {
            response.setContentType("APPLICATION/DOWNLOAD");
            response.setHeader("Content-Disposition", "attachment"+ 
                                     "filename=" + file.getName());
            stream = new FileInputStream(file);
            response.setContentLength(stream.available());
            OutputStream os = response.getOutputStream();      
            os.close();
            response.flushBuffer();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

Hope you got some idea...

You can use HttpURLConnection to download file over HTTP, HTTPS

It is only possible if server lists directory contents. if it does, your can make an HTTP request to:

http://server:port/folder

that would give you list of files.

Once you have that, you can download individual files by parsing output if this http request.

Hi you can use this following code snippet to down the file directly :

    URL oracle = new URL("http://www.example.com/file/download?");
    BufferedReader in = new BufferedReader(
    new InputStreamReader(oracle.openStream()));

    String inputLine;
    while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);
    in.close();

Kindly refer about openStream in this [URL] : http://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html

If it is server, then the process must be like using the FTP credentials you have to dosnload the files. This java file download example may help you.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top