如何使用 O'Reilly 的多部分表单 servlet 库获取多值参数?

发布于 2024-11-07 04:05:58 字数 1327 浏览 1 评论 0原文

我正在使用 O'Reilly 的 Servlet 多部分表单库 来处理文件上传。我发现它有用且可靠,但我面临着一个无法解决的问题。它与多值参数有关(例如多重选择)。 我正在解析参数,如下所示:

List<Units> unitsParams = new ArrayList<Units>();
while (mp != null && (part = mp.readNextPart()) != null) {
    if (part.isFile()) {//Es un fichero.
        FilePart filePart = (FilePart) part;
        if (filePart.getContentType().equals("image/jpeg")) {
            InputStream pis = filePart.getInputStream();
            // It's a file, handle it
        }
    } else if (part.isParam()) { // Es un parametro
        // Handle the actual params
        String namePar = part.getName();
        ParamPart paramPart = (ParamPart) part;
        String valorPar = paramPart.getStringValue();
        boolean fin = null == valorPar;
        if (!fin) {
            if (namePar.equals("id")) {
                id = valorPar;
            } else if (namePar.equals("name")) {
                orgName = valorPar;
            } else if (namePar.equals("unitSelect")) {
                unitsParams.add(valorPar);
            }
        }
    }

对于多值参数,我认为它会迭代(发送不同的部分)与我发送的参数 unitSelect 的值的数量一样多,但我只能得到一个值。

有人使用过这个库并遇到过这个问题吗?我试图避免更改库,因为文件上传工作正常,并且更改少数 servlet 的大部分代码很痛苦。

欢迎任何建议。

谢谢。

I'm using O'Reilly's multipart form library for Servlets to be able to process file uploading. I found it useful and realiable, but I'm facing an issue I can't solve. It's about multiple-value parameters (For example a multiple select).
I'm parsing the parameters like following:

List<Units> unitsParams = new ArrayList<Units>();
while (mp != null && (part = mp.readNextPart()) != null) {
    if (part.isFile()) {//Es un fichero.
        FilePart filePart = (FilePart) part;
        if (filePart.getContentType().equals("image/jpeg")) {
            InputStream pis = filePart.getInputStream();
            // It's a file, handle it
        }
    } else if (part.isParam()) { // Es un parametro
        // Handle the actual params
        String namePar = part.getName();
        ParamPart paramPart = (ParamPart) part;
        String valorPar = paramPart.getStringValue();
        boolean fin = null == valorPar;
        if (!fin) {
            if (namePar.equals("id")) {
                id = valorPar;
            } else if (namePar.equals("name")) {
                orgName = valorPar;
            } else if (namePar.equals("unitSelect")) {
                unitsParams.add(valorPar);
            }
        }
    }

For a multiple value parameter, I thought it would iterate (send different parts) as many times as the number of values for the param unitSelect I was sending, but I can get just one value.

Has anybody used this library and faced this issue? I'm trying to avoid changing the library, since the file upload is working perfectly and it's a pain to change a sigificant part of the code for few servlets.

Any suggestion is welcome.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

孤云独去闲 2024-11-14 04:05:58

我不使用 O'Reilly multipart/form-data 解析器,因为 Apache Commons FileUpload使用更广泛,并且仍在积极维护,并且自 Servlet 3.0 以来,您甚至可以使用内置方法,例如 getPart(),而无需任何第 3 方库。

但是,在检查了 Javadocs 和示例之后,您似乎最好使用 MultipartRequest 类来收集参数。它提供了 getParameter() 和 getParameterValues() 方法。

MultipartRequest multipartRequest = new MultipartRequest(request, saveDirectory);
File file = multipartRequest.getFile("file");
String id = multipartRequest.getParameter("id");
String name = multipartRequest.getParameter("name");
String[] unitSelect = multipartRequest.getParameterValues("unitSelect");
// ...

另请参阅:

I don't use O'Reilly multipart/form-data parser for the reason being that Apache Commons FileUpload is more widely used and still actively maintained, and that since Servlet 3.0 you can even use built-in methods such as getPart() without need for any 3rd party library.

However, after checking the Javadocs and examples it seems like that you'd better use the MultipartRequest class instead to collect the parameters. It offers getParameter() and getParameterValues() methods.

MultipartRequest multipartRequest = new MultipartRequest(request, saveDirectory);
File file = multipartRequest.getFile("file");
String id = multipartRequest.getParameter("id");
String name = multipartRequest.getParameter("name");
String[] unitSelect = multipartRequest.getParameterValues("unitSelect");
// ...

See also:

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