2011-10-04 4 views
8

Tôi mới sử dụng CXF và Spring để tạo các dịch vụ web RESTful.RESTful tạo tệp nhị phân

Đây là vấn đề của tôi: Tôi muốn tạo một dịch vụ tạo ra loại "bất kỳ" (có thể là hình ảnh, tài liệu, txt hoặc thậm chí pdf), và cũng là một XML. Cho đến nay tôi nhận được mã này:

@Path("/download/") 
@GET 
@Produces({"application/*"}) 
public CustomXML getFile() throws Exception; 

Tôi không biết bắt đầu từ đâu nên hãy kiên nhẫn.

EDIT:

Toàn bộ quy tắc ứng Bryant Luk (cảm ơn!)

@Path("/download/") 
@GET 
public javax.ws.rs.core.Response getFile() throws Exception { 
    if (/* want the pdf file */) { 
     File file = new File("..."); 
     return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM) 
      .header("content-disposition", "attachment; filename =" + file.getName()) 
      .build(); 
    } 

    /* default to xml file */ 
    return Response.ok(new FileInputStream("custom.xml")).type("application/xml").build(); 
} 
+0

Hãy thử bắt đầu bằng cách giải thích vấn đề của bạn là gì. Cho đến nay, bạn đã chỉ mô tả những gì bạn đã làm, nhưng bạn chưa đề cập đến những gì xảy ra khi mã chạy, những lỗi bạn gặp phải, v.v. –

+0

Bạn đang cố gắng làm cho khung công tác gọi hàm getFile của bạn() 'cho mọi yêu cầu trong'/download', vì vậy nó có thể tạo tệp được yêu cầu? Tôi nghĩ * những gì bạn đang yêu cầu, trong trường hợp đó, là cách thực hiện 'getFile()' có thể tìm ra những gì đã thực sự được yêu cầu. – Wyzard

+0

@Wyzard vâng, tôi hy vọng không nhiều yêu cầu triển khai và loại chú thích –

Trả lời

15

Nếu nó sẽ trả lại bất kỳ tập tin, bạn có thể muốn thực hiện phương pháp của bạn nhiều hơn "chung chung" và trả về một javax.ws .rs.core.Response mà bạn có thể đặt tiêu đề Loại Nội dung theo chương trình:

@Path("/download/") 
@GET 
public javax.ws.rs.core.Response getFile() throws Exception { 
    if (/* want the pdf file */) { 
     return Response.ok(new File(/*...*/)).type("application/pdf").build(); 
    } 

    /* default to xml file */ 
    return Response.ok(new FileInputStream("custom.xml")).type("application/xml").build(); 
} 
0

Chúng tôi cũng sử dụng CXF và Spring và đây là API thích hợp hơn.

import javax.ws.rs.core.Context; 

@Path("/") 
public interface ContentService 
{ 
    @GET 
    @Path("/download/") 
    @Produces(MediaType.WILDCARD) 
    InputStream getFile() throws Exception; 
} 

@Component 
public class ContentServiceImpl implements ContentService 
{ 
    @Context 
    private MessageContext context; 

    @Override 
    public InputStream getFile() throws Exception 
    { 
     File f; 
     String contentType; 
     if (/* want the pdf file */) { 
      f = new File("...pdf"); 
      contentType = MediaType.APPLICATION_PDF_VALUE; 
     } else { /* default to xml file */ 
      f = new File("custom.xml"); 
      contentType = MediaType.APPLICATION_XML_VALUE; 
     } 
     context.getHttpServletResponse().setContentType(contentType); 
     context.getHttpServletResponse().setHeader("Content-Disposition", "attachment; filename=" + f.getName()); 
     return new FileInputStream(f); 
    } 
}