2017-08-03 82 views
0

我有一個REST服務來上傳文件,有沒有辦法知道我可能會得到什麼類型的內容?使用我的REST服務的用戶可能會上傳PDF,Word或Excel文檔。如何從REST服務獲取文件內容類型?

@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. 
} 

回答

0

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