如何找到 FileItemIterator 返回的 FileItemStream 的编码?

发布于 2024-08-18 01:57:29 字数 968 浏览 5 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澜川若宁 2024-08-25 01:57:29

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文