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

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

问题描述

有没有什么方便的方法可以从传入的请求中读取和解析数据.

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

例如客户端发起发布请求

E.g client initiate post 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") 获取参数.以下代码

I’m not able to get param using request.getParameter("paramName"). The following code

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 3Tomcat 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天全站免登陆