在JSON w / Jackson中序列化java.io.File

时间:2016-08-08 09:38:21

标签: java json serialization jersey jackson

我有一个Web服务(Jersey 2),它返回几个DOCX和PDF文件。我不知道哪种方法最好:

  1. 将它们作为Octet-Stream返回。这是我在实现它之前所做的,如2:

        @GET
        @Path("solicitudUsuario/plantilla")
        @Produces(MediaType.APPLICATION_OCTET_STREAM)
        public Response descargarPlantillaSolicitudUsuario() throws ConfigurationException, URISyntaxException, IOException{
            File plantillaSolicitudUsuario = this.generalService.getPlantillaSolicitudUsuario();
            return Response.ok(plantillaSolicitudUsuario, MediaType.APPLICATION_OCTET_STREAM).header("Content-Disposition", "attachment; filename=\"" + plantillaSolicitudUsuario.getName() + "\"" ).build();
        }
    
  2. 在Json中包装文件。我正在使用Jackson作为json提供者。这就是我到目前为止所做的:

  3. 网络服务:

            @GET
            @Path("solicitudUsuario/plantilla")
            @Produces(MediaType.APPLICATION_JSON)
            public Response download() throws ConfigurationException, URISyntaxException, IOException{
                File plantillaSolicitudUsuario = this.generalService.getPlantillaSolicitudUsuario();
                FicheroDevolver f = FicheroDevolver.buildAsWord(plantillaSolicitudUsuario);
                String json = mWriter.writeValueAsString(f);
                return Response.ok(MediaType.APPLICATION_JSON_TYPE.withCharset("UTF-8")).entity(json).build();
            }
    

    串行:

            public class FicheroSerializer extends JsonSerializer<FicheroDevolver> {
    
            @Override
            public void serialize(com.ingartek.ws.zendesk_bizkaibus.model.FicheroDevolver value, JsonGenerator gen, SerializerProvider serializers)
                    throws IOException, JsonProcessingException {
                InputStream fis = FileUtils.openInputStream(value.getFichero());
    
                gen.useDefaultPrettyPrinter();
                gen.writeStartObject();
                    gen.writeFieldName("fichero");
                    //gen.writeBinary(fis, (int)value.getFicheroLength());
                    gen.writeRawValue(IOUtils.toString(fis, "UTF-8"));
    
                    gen.writeStringField("nombre", value.getNombre());
    
                    gen.writeStringField("mimeType", value.getMimeType());
                gen.writeEndObject();
    
                fis.close();
            }
    
            }
    

    数据包装器:

                @JsonSerialize(using = FicheroSerializer.class)
                public class FicheroDevolver {
    
                    /*
                     * Atributos
                     */
                    private File fichero;
                    private String nombre;
                    private String mimeType;    
    

    哪种方法最好?

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容:

    File file = new File(FILE_PATH);
    ResponseBuilder response = Response.ok((Object) file);
    response.header("Content-Disposition",
                "attachment; filename=new-android-book.pdf");
    return response.build();

javax.ws.rs.core.Response.ResponseBuilder