java代码从服务器下载文件

发布于 2024-10-24 08:09:13 字数 175 浏览 8 评论 0原文

在Windows中使用java代码我需要从服务器中的目录下载几个文件。服务器中的这些文件是单独生成的。所以我不知道这些文件的名称。有没有办法使用JAVA下载它并将其保存在特定的文件夹中。

我正在使用 apache tomcat。

我阅读了与 java 文件下载相关的所有其他线程。但他们都不满足我的要求。

using java code in windows i need to download several files from a directory placed in a server. those files in server are generated separately. so i'll not know the name of those files. is there any way to download it using JAVA and saving it in a specific folder.

i am using apache tomcat.

I read all other threads related to java file download. But none of them satisfy my requirement.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

毁我热情 2024-10-31 08:09:14

仅当服务器列出目录内容时才可能。如果是这样,您可以向:

http://server:port/folder

发出 HTTP 请求,这将为您提供文件列表。

完成后,您可以通过解析此 http 请求的输出来下载单个文件。

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.

夜清冷一曲。 2024-10-31 08:09:14

如果是服务器,则该过程必须类似于使用 FTP 凭据来加载文件。这个java文件下载示例 可能对你有帮助。

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.

江湖正好 2024-10-31 08:09:13
  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();
            }
        }
    }

希望你有一些想法...

  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...

牛↙奶布丁 2024-10-31 08:09:13

使用 java.net.URL 和 java.net.URLConnection 类。

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

白日梦 2024-10-31 08:09:13

您好,您可以使用以下代码片段直接下载文件:

    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();

请参考此 [URL] 中有关 openStream 的信息:

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

月下伊人醉 2024-10-31 08:09:13

You can use HttpURLConnection to download file over HTTP, HTTPS

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文