如何从REST服务获取文件内容类型?

时间:2017-08-03 10:33:27

标签: rest jersey dropwizard

我有一个REST服务来上传文件,有没有办法知道我可能会得到什么样的内容?使用我的REST服务的用户可能会上传PDF,Word或Excel文档。

 @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Path("/details/documents")
    public Response uploadDocument(@FormDataParam("file") File documentContent, 
                                   @FormDataParam("documentName") String documentName) 
                                   throws Exception { 
 //more implementation code here to upload the file.
}

1 个答案:

答案 0 :(得分:1)

您可以使用File,而不是FormDataBodyPart作为方法参数,然后调用getMediaType()。如果您想将部分正文设为File,只需使用getValueAs(File.class),或者如果您希望输入流只使用InputStream.class

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("/details/documents")
public Response uploadDocument(@FormDataParam("file") FormDataBodyPart part) { 
    MediaType mediaType = part.getMediaType();
    File file = part.getValueAs(File.class);
}
相关问题