如何找到 FileItemIterator 返回的 FileItemStream 的编码?
我使用 Apache Commons FileUpload 在 Servlet 中接收上传的文件,如 http: //code.google.com/appengine/kb/java.html#fileforms
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iterator = upload.getItemIterator(request);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
if (!item.isFormField()) {
System.out.println("Got an uploaded file: " + item.getFieldName()
+ ", name = " + item.getName() + " type = " + item.getContentType());
}
我不确定 item.getContentType() 是否也会包含可能与请求编码不同的文本文件的文件编码,(例如 ISO-8859-1),或者它始终仅包含文件类型。在我的测试中,我只收到“text/plain”,但我期待从客户端发送的“text/plain; Encoding=ISO-8859-1”。
我的理解是否正确:item.getContentType() 应包含编码(如果是从客户端发送的)?
I use Apache Commons FileUpload to receive uploaded files in a Servlet, as described at http://code.google.com/appengine/kb/java.html#fileforms
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iterator = upload.getItemIterator(request);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
if (!item.isFormField()) {
System.out.println("Got an uploaded file: " + item.getFieldName()
+ ", name = " + item.getName() + " type = " + item.getContentType());
}
I am not sure if item.getContentType() also will coutain the file encoding for text files which could be different from the request encoding, (for example ISO-8859-1), or if it always only contains the file type only. In my tests I received only "text/plain" but I was expecting "text/plain; Encoding=ISO-8859-1" which has been sent from the client.
Is my understanding correct that the item.getContentType() should include the encoding (if it is sent from client)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FileItemStream.getContentType() 返回从浏览器的 POST 传递的任何内容。它可能是“text/plain”,也可能是“text/plain;Encoding=ISO-8859-1”,或者它可能完全是垃圾。内容类型只是一个字符串值,您相信浏览器会正确地为您提供(换句话说,根本不信任它)。
FileItemStream.getContentType() returns whatever was passed from the browser's POST. It could be "text/plain" or it could be "text/plain; Encoding=ISO-8859-1" OR it could be complete garbage. The content type is just a string value you are trusting the browser to give you properly (in other words, don't trust it at all).