2012-01-19 29 views
5

Tôi sử dụng Jersey để xây dựng các dịch vụ RESTful và hiện tại tôi đang mắc kẹt vào một thứ mà tôi nghĩ, không nên quá khó.Làm cách nào để lưu trữ tệp đã tải xuống bằng Java và Jersey?

Tôi quản lý để GET tệp tôi muốn tải xuống nhưng tôi không biết cách lưu tệp.

Tôi đã tìm kiếm câu trả lời trên web nhưng tôi không tìm thấy bất kỳ thứ gì đủ hữu ích để lấp đầy khoảng trống trong kiến ​​thức của mình.

Xin vui lòng cho tôi biết cách tiếp tục để lưu tệp ở vị trí trên hdd? Bất kỳ mẫu mã nào sẽ được đánh giá cao!

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

Cảm ơn bạn!

+0

Bạn đã thử sử dụng lớp 'Tệp' chưa? – Viruzzo

+0

@Viruzo: vâng, không có vấn đề làm thế nào ngu ngốc nó âm thanh tôi đã không quản lý để lưu các tập tin dụ = – karla

+0

Bạn có thể gửi mã có liên quan của bạn? – Viruzzo

Trả lời

10

tôi có câu trả lời cho câu hỏi của tôi:

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

Sử dụng phần còn lại dễ dàng Khách hàng này là những gì tôi đã làm.

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()); 
Các vấn đề liên quan