2011-11-16 13 views
7

Ciao, volevo restituire un file da un server resteasy. A tale scopo, ho un collegamento sul lato client che sta chiamando un servizio di assistenza con ajax. Voglio restituire il file nel servizio di riposo. Ho provato questi due blocchi di codice, ma entrambi non funzionavano come volevo.Restituisci file dal server Resteasy

@POST 
    @Path("/exportContacts") 
    public Response exportContacts(@Context HttpServletRequest request, @QueryParam("alt") String alt) throws IOException { 

      String sb = "Sedat BaSAR"; 
      byte[] outputByte = sb.getBytes(); 


    return Response 
      .ok(outputByte, MediaType.APPLICATION_OCTET_STREAM) 
      .header("content-disposition","attachment; filename = temp.csv") 
      .build(); 
    } 

.

@POST 
@Path("/exportContacts") 
public Response exportContacts(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam("alt") String alt) throws IOException { 

    response.setContentType("application/octet-stream"); 
    response.setHeader("Content-Disposition", "attachment;filename=temp.csv"); 
    ServletOutputStream out = response.getOutputStream(); 
    try { 

     StringBuilder sb = new StringBuilder("Sedat BaSAR"); 

     InputStream in = 
       new ByteArrayInputStream(sb.toString().getBytes("UTF-8")); 
     byte[] outputByte = sb.getBytes(); 
     //copy binary contect to output stream 
     while (in.read(outputByte, 0, 4096) != -1) { 
      out.write(outputByte, 0, 4096); 
     } 
     in.close(); 
     out.flush(); 
     out.close(); 

    } catch (Exception e) { 
    } 

    return null; 
} 

Quando ho controllato dalla console di Firebug, entrambi questi blocchi di codice scritto "Sedat Basar" in risposta alla chiamata AJAX. Tuttavia, voglio restituire "Sedat BaSAR" come file. Come lo posso fare?

Grazie in anticipo.

+0

Forse si finisce per trovare una soluzione a questo? – rabs

risposta

12

Ci sono due modi per farlo.

1 ° - restituire un'istanza StreamingOutput.

@Produces(MediaType.APPLICATION_OCTET_STREAM) 
public Response download() { 
    InputStream is = getYourInputStream(); 

    StreamingOutput stream = new StreamingOutput() { 

     public void write(OutputStream output) throws IOException, WebApplicationException { 
      try { 
       output.write(IOUtils.toByteArray(is)); 
      } 
      catch (Exception e) { 
       throw new WebApplicationException(e); 
      } 
     } 
}; 

return Response.ok(stream, MediaType.APPLICATION_OCTET_STREAM).header("content-disposition", "attachment; filename=\"temp.csv\"").build(); 
} 

è possibile restituire la dimensione del file aggiungendo intestazione Content-Length, come il seguente esempio:

return Response.ok(stream, MediaType.APPLICATION_OCTET_STREAM).header("content-disposition", "attachment; filename=\"temp.csv\"").header("Content-Length", getFileSize()).build(); 

Ma se non si vuole tornare a un'istanza StreamingOutput, non c'è altra opzione.

2nd - Definire il flusso di input come risposta di entità.

@Produces(MediaType.APPLICATION_OCTET_STREAM) 
public Response download() { 
    InputStream is = getYourInputStream(); 

    return Response.code(200).entity(is).build(); 
} 
+0

Come posso restituire un file con nome UTF-8? – vanduc1102