【问题标题】:Set default File to upload using Apache commons file upload使用 Apache commons 文件上传设置要上传的默认文件
【发布时间】:2014-04-04 05:28:57
【问题描述】:

我正在使用 apache commons file upload 1.1 。目前我正在使用parseRequest(request) 来解析请求中的项目。

现在我还有一个上传文件的请求。如果用户不上传任何文件,则类似于 default 文件。

这可能吗?

提前致谢

【问题讨论】:

    标签: java file-upload default apache-commons-fileupload


    【解决方案1】:

    parseRequest 返回FileItems 的列表。所以,当列表为空时,没有文件上传。

    因此,您只需要测试列表是否为空。以Using FileUpload为例

    ServletFileUpload upload = new ServletFileUpload(factory);
    List<FileItem> items = upload.parseRequest(request);
    if (items.isEmpty()) {
        // process some default file
    } else {
        // process uploaded file
    }
    

    更新:

    根据Processing the uploaded items,您可以在请求中混合文件和常规表单字段。您可以遍历参数,在看到文件上传时设置标志,然后采取相应措施

    // Process the uploaded items
    boolean fileUploaded = false;
    Iterator<FileItem> iter = items.iterator();
    while (iter.hasNext()) {
        FileItem item = iter.next();
    
        if (item.isFormField()) {
            processFormField(item);
        } else {
            processUploadedFile(item);
            fileUploaded = true;
        }
    }
    
    if (!fileUploaded) {
        // process some default file
    }
    

    【讨论】:

    • 但我需要从表单中检索除 file 之外的其他属性,因为它将是 Null 。我需要getParameter,例如类型、月份等。这可能吗?
    猜你喜欢
    • 1970-01-01
    • 2010-11-11
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 2011-01-10
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多