如何使用 O'Reilly 的多部分表单 servlet 库获取多值参数?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不使用 O'Reilly multipart/form-data 解析器,因为 Apache Commons FileUpload使用更广泛,并且仍在积极维护,并且自 Servlet 3.0 以来,您甚至可以使用内置方法,例如
getPart()
,而无需任何第 3 方库。但是,在检查了 Javadocs 和示例之后,您似乎最好使用
MultipartRequest
类来收集参数。它提供了 getParameter() 和 getParameterValues() 方法。另请参阅:
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 offersgetParameter()
andgetParameterValues()
methods.See also: