如何获取正在上载的文件的InputStream的MIME类型? [英] How can I get MIME type of an InputStream of a file that is being uploaded?

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

问题描述

简单问题:如何在不保存文件的情况下获取 InputStream 的MIME类型(或内容类型),以获取用户上传到我的servlet的文件?

Simple question: how can I get MIME type (or content type) of an InputStream, without saving file, for a file that a user is uploading to my servlet?

推荐答案

这取决于您从哪里获取输入流。如果从servlet获取它,则可以通过作为doPost参数的HttpServerRequest对象访问它。如果您正在使用像Jersey这样的某种其他API,则可以使用@Context注入请求。如果您通过套接字上传文件,则您有责任将MIME类型指定为协议的一部分,因为您不会继承http标头。

It depends on where you are getting the input stream from. If you are getting it from a servlet then it is accessable through the HttpServerRequest object that is an argument of doPost. If you are using some sort of rest API like Jersey then the request can be injected by using @Context. If you are uploading the file through a socket it will be your responsibility to specify the MIME type as part of your protocol as you will not inherit the http headers.

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

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