Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 729
karla Avatar asked Jan 19 '12 14:01

karla


1 Answers

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();
like image 53
karla Avatar answered Sep 28 '22 15:09

karla