方便的方法来解析Servlet中传入的multipart / form-data参数 [英] Convenient way to parse incoming multipart/form-data parameters in a Servlet

查看:241
本文介绍了方便的方法来解析Servlet中传入的multipart / form-data参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方便的方法来读取和解析传入请求中的数据。

Is there any convenient way to read and parse data from incoming request.

例如客户发起帖子请求

URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
PrintWriter writer = null;
try {
    OutputStream output = connection.getOutputStream();
    writer = new PrintWriter(new OutputStreamWriter(output, charset), true); // true = autoFlush, important!
    // Send normal param.
    writer.println("--" + boundary);
    writer.println("Content-Disposition: form-data; name=\"param\"");
    writer.println("Content-Type: text/plain; charset=" + charset);
    writer.println();
    writer.println(param);

我无法使用 request.getParameter(paramName获取参数)。以下代码

BufferedReader reader = new BufferedReader(new InputStreamReader(
    request.getInputStream()));
  StringBuilder sb = new StringBuilder();
  for (String line; (line = reader.readLine()) != null;) {
   System.out.println(line);

  }

但显示我的内容

-----------------------------29772313742745
Content-Disposition: form-data; name="name"
J.Doe
-----------------------------29772313742745
Content-Disposition: form-data; name="email"
abuse@spamcop.com
-----------------------------29772313742745

解析传入请求的最佳方法是什么?我不想编写自己的解析器,可能有一个现成的解决方案。

What is the best way to parse incoming request? I don’t want to write my own parser, probably there is a ready solution.

推荐答案

multipart / form-data 版本3.0之前的Servlet API默认不支持编码请求。默认情况下,Servlet API使用 application / x-www-form-urlencoded 编码来解析参数。使用不同的编码时, request.getParameter()调用都将返回 null 。当您已经使用Servlet 3.0( Glassfish 3 )时, Tomcat 7 等),然后你可以使用 HttpServletRequest#getParts() 。另请参阅此博客以获取扩展示例。

multipart/form-data encoded requests are indeed not by default supported by the Servlet API prior to version 3.0. The Servlet API parses the parameters by default using application/x-www-form-urlencoded encoding. When using a different encoding, the request.getParameter() calls will all return null. When you're already on Servlet 3.0 (Glassfish 3, Tomcat 7, etc), then you can use HttpServletRequest#getParts() instead. Also see this blog for extended examples.

在Servlet 3.0之前,事实上标准解析 multipart / form-data 请求将使用 Apache Commons FileUpload 。只需仔细阅读用户指南常见问题部分,即可了解如何使用它。在此处之前,我已使用代码示例发布了答案(它还包含一个针对Servlet 3.0的示例。

Prior to Servlet 3.0, a de facto standard to parse multipart/form-data requests would be using Apache Commons FileUpload. Just carefully read its User Guide and Frequently Asked Questions sections to learn how to use it. I've posted an answer with a code example before here (it also contains an example targeting Servlet 3.0).

这篇关于方便的方法来解析Servlet中传入的multipart / form-data参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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