繁体   English   中英

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

[英]How do I get to store a downloaded file with Java and Jersey?

我使用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());

谢谢!

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM