2011-10-04 13 views
8

Sono nuovo utilizzando CXF e Spring per creare servizi web RESTful.RESTful produce file binario

Questo è il mio problema: voglio creare un servizio che produca "qualsiasi" tipo di file (può essere immagine, documento, txt o anche pdf) e anche un XML. Finora ho ricevuto questo codice:

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

Non so esattamente da dove cominciare, per favore sii paziente.

EDIT:

completo del codice di Bryant Luk (grazie!)

@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

Provare dall'inizio spiegando qual è il problema. Finora hai descritto solo ciò che hai fatto, ma non hai menzionato cosa succede quando il codice viene eseguito, quali errori hai riscontrato, ecc. –

+0

Stai provando a fare in modo che il framework chiami 'getFile() 'per ogni richiesta in'/download', quindi può produrre il file richiesto? Io * penso * quello che stai chiedendo, in questo caso, è come l'implementazione di 'getFile()' può scoprire cosa è stato effettivamente richiesto. – Wyzard

+0

@Wyzard si, spero non ci sia molto da chiedere per l'implementazione e il tipo di annotazione –

risposta

15

Se tornerà qualsiasi file, si potrebbe desiderare di fare il metodo più "generico" e restituire un javax.ws .rs.core.Response quale è possibile impostare l'header Content-Type di programmazione:

@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

Usiamo anche CXF e primavera, e questo è il mio API preferibile.

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