簡體   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