如何使用 Apache Commons 以多部分形式读取其他参数 [英] How can I read other parameters in a multipart form with Apache Commons

查看:24
本文介绍了如何使用 Apache Commons 以多部分形式读取其他参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件上传表单,该表单被回发到 servlet(使用 multipart/form-data 编码).在 servlet 中,我尝试使用 Apache Commons 来处理上传.但是,我在表单中还有一些其他字段,它们只是普通字段.如何从请求中读取这些参数?

I have a file upload form that is being posted back to a servlet (using multipart/form-data encoding). In the servlet, I am trying to use Apache Commons to handle the upload. However, I also have some other fields in the form that are just plain fields. How can I read those parameters from the request?

例如,在我的 servlet 中,我有这样的代码要在上传的文件中读取:

For example, in my servlet, I have code like this to read in the uplaoded file:

    // Create a factory for disk-based file items
    FileItemFactory factory = new DiskFileItemFactory();
    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);
    // Parse the request
    Iterator /* FileItem */ items = upload.parseRequest(request).iterator();
    while (items.hasNext()) {
        FileItem thisItem = (FileItem) items.next();
        ... do stuff ...
    }

推荐答案

你可以试试这样的:

while (items.hasNext()) {
        FileItem thisItem = (FileItem) items.next();
        if (thisItem.isFormField()) {
            if (thisItem.getFieldName().equals("somefieldname") {
                String value = thisItem.getString();
                // Do something with the value
            }
        }

    }

这篇关于如何使用 Apache Commons 以多部分形式读取其他参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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