如何在Jersey中获取上传文件的MIME类型 [英] How to get MIME type of uploaded file in Jersey

查看:665
本文介绍了如何在Jersey中获取上传文件的MIME类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @POST 
@Secure
@Consumes (multipart / form-data)
public上传(@Context final HttpHeaders hh,
@FormDataParam(fileaaa)final FormDataContentDisposition处置,
@FormDataParam(fileaaa)final InputStream输入,

如何获取上传文件的MIME类型?
$ b

如果我执行 disposition.getType ,这会得到表单的MIME类型;在这种情况下, form-data 。



我知道这些信息在哪里,HTTP消息应该是这样的:

  ----------------------------- 7d01ecf406a6 
内容处理:form-data; name =input_text

mytext

----------------------- ------ 7d01ecf406a6
Content-Disposition:form-data; name =fileaaa;
filename =C:\ Inetpub\wwwroot\Upload\pic.gif
Cont ent-Type:image / gif

(二进制内容)
--------------------------- --7d01ecf406a6--


解决方案

注入一个 BodyPart 参数我的方法。 getMediaType()在正文部分给我我需要的:

$ $ p $ $ b $ @ $
@Secure
@消费(MediaType.MULTIPART_FORM_DATA)
public响应上传(/ * other parms * /,@FormDataParam(fileaaa)final FormDataBodyPart body){
String mimeType = body.getMediaType()。toString();
//处理上载
}


I have a standard upload endpoint in Jersey:

@POST
@Secure
@Consumes("multipart/form-data")
public Response upload( @Context final HttpHeaders hh,
            @FormDataParam("fileaaa") final FormDataContentDisposition disposition,
            @FormDataParam("fileaaa") final InputStream input,

How can I get the MIME type of the uploaded file?

If I do disposition.getType this gets me the MIME type of the form; in this case form-data.

I know the information is there somewhere; the HTTP message should be something like:

-----------------------------7d01ecf406a6
Content-Disposition: form-data; name="input_text"

mytext

-----------------------------7d01ecf406a6
Content-Disposition: form-data; name="fileaaa";
filename="C:\Inetpub\wwwroot\Upload\pic.gif"
Content-Type: image/gif

(binary content)
-----------------------------7d01ecf406a6--

解决方案

I solved this by letting Jersey inject a BodyPart parameter in my method. getMediaType() on the body part gives me what I needed:

@POST
@Secure
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response upload(/*other parms */,  @FormDataParam("fileaaa") final FormDataBodyPart body) {
   String mimeType = body.getMediaType().toString();
   //handle upload
}

这篇关于如何在Jersey中获取上传文件的MIME类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆