简体   繁体   English

将默认文件设置为使用Apache Commons文件上传进行上传

[英]Set default File to upload using Apache commons file upload

I am using apache commons file upload 1.1 . 我正在使用apache commons file upload 1.1 Currently i am using parseRequest(request) to parse the items from the request . 目前,我正在使用parseRequest(request)来解析parseRequest(request)的项目。

Now i have an additional request to upload a file . 现在,我还有一个其他要求上传文件。 something like default file if user doesn't upload any. 如果用户不上传,则类似于default文件。

Is that possible ? 那可能吗 ?

Thanks in advance 提前致谢

parseRequest returns a list of FileItem s. parseRequest返回FileItem的列表。 So, when the list is empty there was no file uploaded. 因此,当列表为空时,没有文件上传。

Therefore, you just need to test if the list is empty. 因此,您只需要测试列表是否为空。 Taking an example from Using FileUpload 使用FileUpload为例

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

Update: 更新:

According to Processing the uploaded items , you can have files and regular form fields mixed in the request. 根据处理上载的项目 ,您可以在请求中混合使用文件和常规表单字段。 You can iterate through the parameters, set a flag when you see a file upload, and act accordingly afterwards 您可以遍历参数,在看到文件上传时设置标志,然后进行相应操作

// 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM