如何阅读内容处置标题的内容? [英] How can I read the content of the content-disposition header?

查看:115
本文介绍了如何阅读内容处置标题的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TEMPORARY已解决: 在Apache FileUpload API中关闭InputStream

我想阅读内容处置标题的内容,但请求。 getHeader(content-disposition)总是返回null并且 request.getHeader(content-type)只返回第一行,就像这样的multipart / form-data的; boundary = AaB03x

I want to read the content of the content-disposition header but request.getHeader ("content-disposition") always return null and request.getHeader ("content-type") only returns the first line, like this multipart/form-data; boundary=AaB03x.

我收到以下标题:

Content-Type: multipart/form-data; boundary=AaB03x

--AaB03x
Content-Disposition: form-data; name="submit-name"

Larry
--AaB03x
Content-Disposition: form-data; name="files"; filename="file1.txt"
Content-Type: text/plain

... contents of file1.txt ...
--AaB03x--

我想阅读所有内容处置标题。怎么样?

I want to read all the content-disposition headers. How?

谢谢。

EDIT1 :我真正想要解决的问题是当客户端发送超过最大大小的文件时,因为当你调用request.getPart(something)时,你传递给它的部分名称并不重要,因为它即使请求不包含此参数名,也总会抛出IllegalStateException。

EDIT1: What I really want to solve is the problem when the client sends a file that exceeds the maximum size because when you call request.getPart ("something") it doesn't matter what part name you pass to it because it always will throw an IllegalStateException even if the request does not contain this parameter name.

示例:

Part part = request.getPart ("param");
String value = getValue (part);
if (value.equals ("1")){
    doSomethingWithFile1 (request.getPart ("file1"))
}else if (value.equals (2)){
    doSomethingWithFile2 (request.getPart ("file2"))
}

private String getValue (Part part) throws IOException{
    if (part == null) return null;

    BufferedReader in = null;
    try{
        in = new BufferedReader (new InputStreamReader (part.getInputStream (), request.getCharacterEncoding ()));
    }catch (UnsupportedEncodingException e){}

    StringBuilder value = new StringBuilder ();
    char[] buffer = new char[1024];
    for (int bytesRead; (bytesRead = in.read (buffer)) != -1;) {
        value.append (buffer, 0, bytesRead);
    }

    return value.toString ();
}

我不能这样做,因为如果客户端发送的文件超过了max size第一次调用getPart会抛出异常(参见 getPart()Javadoc ),所以我不知道我收到了哪个文件。

I can't do this because if the client sends a file that exceeds the max size the first call to getPart will throw the exception (see getPart() Javadoc), so I can't know which file I've received.

那是为什么我要阅读内容处置标题。我想读取参数param以了解哪个文件抛出异常。

That's why I want to read the content-disposition headers. I want to read the parameter "param" to know which file has thrown the exception.

EDIT2 :嗯,使用发布Servlet 3.0规范的API,您无法控制之前的情况,因为如果文件抛出异常,则无法读取文件字段名称。这是使用包装器的负面部分,因为很多功能都消失了......同样使用FileUpload,您可以动态设置MultipartConfig注释。

EDIT2: Well, with the API that publishes the Servlet 3.0 specification you can't control the previous case because if a file throws an exception you can't read the file field name. This is the negative part of using a wrapper because a lot of functionalities disappear... Also with FileUpload you can dynamically set the MultipartConfig annotation.

如果文件超过最大值文件大小api抛出一个 FileSizeLimitExceededException 例外。该异常提供了两种方法来获取字段名称和文件名。

If the file exceeds the maximum file size the api throws a FileSizeLimitExceededException exception. The exception provides 2 methods to get the field name and the file name.

但是!!我的问题还没有解决,因为 我想读取与文件一起发送的另一个参数的值,格式相同。 (前一个param的值例子)

But!! my problem is not still solved because I want to read the value of another parameter sent together with the file in the same form. (the value of "param" in the previous example)

EDIT3 :我正在研究这个问题。一旦我编写代码,我就会在这里发布它!

EDIT3: I'm working on this. As soon as I write the code I'll publish it here!

推荐答案

request.getHeader( content-disposition)将在您的情况下返回null,因为 Content-Disposition 标题出现在HTTP POST正文中,因此需要它们单独处理。实际上, Content-Disposition 只是一个有效的HTTP响应头。作为请求的一部分,它永远不会被视为标题。

request.getHeader ("content-disposition") will return null in your case, as the Content-Disposition headers appear in the HTTP POST body, thereby requiring them to be treated separately. In fact, Content-Disposition is only a valid HTTP response header. As part of a request, it will never be treated as a header.

最好使用Commons FileUpload等文件上传库或内置文件 - 上传Servlet规范3.0的功能以读取 Content-Disposition 标题(间接)。实现Servlet Spec 3.0所需的文件上载功能所需的Java EE 6容器通常使用Apache Commons FileUpload。

You're better off using a file upload library like Commons FileUpload or the in-built file-upload features of the Servlet Spec 3.0 to read the Content-Disposition header (indirectly). Java EE 6 containers that are required to implement the file-upload functionality required of Servlet Spec 3.0, often use Apache Commons FileUpload under the hood.

如果你想忽略这些库有一些正当理由,而是自己阅读标题,然后我建议查看 parseHeaderLine getParsedHeaders 方法 FileUploadBase Apache Commons FileUpload类。请注意,这些方法实际上是从与 HttpServletRequest 关联的 InputStream 中读取的,并且您无法读取该流两次。如果您想首先在代码中读取 Content-Disposition 标头,稍后使用Apache Commons FileUpload来解析请求,则必须传递 ServletRequestWrapper 如果对FileUpload API的原始请求,则包装副本。反向序列还要求您创建原始请求的副本,并传递 ServletRequestWrapper 将此副本包装到FileUpload API。总的来说,这是糟糕的设计,因为在内存(或磁盘)上复制整个流只是为了读取请求体两次是没有意义的。

If you want to ignore these libraries for some valid reason and instead read the headers yourself, then I would recommend looking at the parseHeaderLine and getParsedHeaders methods of the FileUploadBase class of Apache Commons FileUpload. Do note that these methods actually read from the InputStream associated with the HttpServletRequest, and you cannot read the stream twice. If you want to read the Content-Disposition header in your code first, and later use Apache Commons FileUpload to parse the request, you will have to pass a ServletRequestWrapper that wraps a copy if the original request to the FileUpload API. The reverse sequence also requires you create a copy of the original request and pass a ServletRequestWrapper wrapping this copy to the FileUpload API. Overall, this is poor design, as it makes no sense to replicate an entire stream in memory (or on disk) only to read the request body twice.

这篇关于如何阅读内容处置标题的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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