如何使用 Java 和 Jersey 存储下载的文件?

发布于 2024-12-28 00:39:24 字数 974 浏览 0 评论 0原文

我使用 Jersey 来构建 RESTful 服务,目前我陷入了一些我认为应该不会太难的事情上。

我设法获取我想要下载的文件,但我不知道如何保存它。

我在网上搜索答案,但没有找到任何有用的内容足以填补我的知识空白。

您能否告诉我如何继续将文件保存在硬盘上的某个位置?任何代码示例都将受到高度赞赏!

              Client client = Client.create();

            WebResource imageRetrievalResource = client
                    .resource("http://server/");
            WebResource wr=imageRetrievalResource.path("instances/attachment");
              MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
              queryParams.add("item", "1");

              Builder wb=wr.accept("application/json,text/html,application/xhtml+xml,application/xml");

              client.addFilter(new HTTPBasicAuthFilter("user","pass"));

              ClientResponse response= wr.queryParams(queryParams).get(ClientResponse.class);

              String s= response.getEntity(String.class);
              System.out.println(response.getStatus());

谢谢你!

I use Jersey for building RESTful services and currently I'm stuck on something that, I thought, shouldn't be too hard.

I manage to GET the file I want to download, but I do not know how to save it.

I searched for answers on the web, but I didn't find anything useful enough to fill in the gaps in my knowledge.

Can you, please, give me a hit how to go on in order to save the file in a location on hdd? Any code samples are going to be highly appreciated!

              Client client = Client.create();

            WebResource imageRetrievalResource = client
                    .resource("http://server/");
            WebResource wr=imageRetrievalResource.path("instances/attachment");
              MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
              queryParams.add("item", "1");

              Builder wb=wr.accept("application/json,text/html,application/xhtml+xml,application/xml");

              client.addFilter(new HTTPBasicAuthFilter("user","pass"));

              ClientResponse response= wr.queryParams(queryParams).get(ClientResponse.class);

              String s= response.getEntity(String.class);
              System.out.println(response.getStatus());

Thank you!

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

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

发布评论

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

评论(2

掩饰不了的爱 2025-01-04 00:39:24

我得到了我的问题的答案:

      File s= response.getEntity(File.class);
      File ff = new File("C:\\somewhere\\some.txt");
      s.renameTo(ff);
      FileWriter fr = new FileWriter(s);
      fr.flush();

I got the answer to my question:

      File s= response.getEntity(File.class);
      File ff = new File("C:\\somewhere\\some.txt");
      s.renameTo(ff);
      FileWriter fr = new FileWriter(s);
      fr.flush();
栖竹 2025-01-04 00:39:24

使用 Rest easy Client 这就是我所做的。

    String fileServiceUrl = "http://localhost:8081/RESTfulDemoApplication/files";
    RestEasyFileServiceRestfulClient fileServiceClient = ProxyFactory.create(RestEasyFileServiceRestfulClient.class,fileServiceUrl);

    BaseClientResponse response = (BaseClientResponse)fileServiceClient.getFile("SpringAnnontationsCheatSheet.pdf");
    File s = (File)response.getEntity(File.class);
    File ff = new File("C:\\RestFileUploadTest\\SpringAnnontationsCheatSheet_Downloaded.pdf");
    s.renameTo(ff);
    FileWriter fr = new FileWriter(s);
    fr.flush();
    System.out.println("FileDownload Response = "+ response.getStatus());

Using Rest easy Client this is what I did.

    String fileServiceUrl = "http://localhost:8081/RESTfulDemoApplication/files";
    RestEasyFileServiceRestfulClient fileServiceClient = ProxyFactory.create(RestEasyFileServiceRestfulClient.class,fileServiceUrl);

    BaseClientResponse response = (BaseClientResponse)fileServiceClient.getFile("SpringAnnontationsCheatSheet.pdf");
    File s = (File)response.getEntity(File.class);
    File ff = new File("C:\\RestFileUploadTest\\SpringAnnontationsCheatSheet_Downloaded.pdf");
    s.renameTo(ff);
    FileWriter fr = new FileWriter(s);
    fr.flush();
    System.out.println("FileDownload Response = "+ response.getStatus());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文