我使用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());
谢谢!
答案 0 :(得分:10)
我得到了我的问题的答案:
File s= response.getEntity(File.class);
File ff = new File("C:\\somewhere\\some.txt");
s.renameTo(ff);
FileWriter fr = new FileWriter(s);
fr.flush();
答案 1 :(得分:0)
使用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());