How do I get the downloaded file with Java and Jersey?

I use Jersey to create RESTful services, and currently I'm stuck on something that I thought should not be too heavy.

I will be able to upload the file that I want to download, but I do not know how to save it.

I searched the Internet for answers, but I did not find anything useful to fill the gaps in my knowledge.

Can you please give me a hit, how to proceed to save the file to a location on hdd? Any code samples would be much 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!

+5
source share
2 answers

I got an 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();
+10
source

Using Rest easy Client 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());
0
source

All Articles