1

实际上,我试图在使用 jsp 中的通用上传库上传之前获取要上传的文件数,并且我创建了一个函数来获取文件数,这里是:

public static int getUploadFileCount(HttpServletRequest request) throws FileUploadException
{
    int Result = 0;

    DiskFileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List<FileItem> items = upload.parseRequest(request);
    for (FileItem item : items) 
    {
        if (!item.isFormField())
        Result++;
    }       

    return Result;
}

并在此处的业务逻辑中使用此功能:

public void doChangeProfilePhoto(HttpServletRequest request) throws FileUploadException
{
    if(UploadFileUtil.getUploadFileCount(request) != 1)
        throw new FileUploadException("There is Multiple/None File upload for Profile Photo. ");
    ProfileImage newProfileImage = new ProfileImage();
    newProfileImage.setFileName("sam.jpg");
    if(new UploadBPOProfilePhotoImpl().uploadPhoto(newProfileImage, request))
    {}
}

在调用 uploadPhoto 函数后的这段代码中,还使用相同的代码来一一检索文件,如下所示:

    DiskFileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List<FileItem> items = upload.parseRequest(request);
    for (FileItem item : items) // Did not get file if i already used this code once For getting the files count:
    {
        if (!item.isFormField())
        //did for makeing file and writing/saving it with a name
    }     

所以在这里我遇到的问题是,当我使用此代码时,当我使用相同的代码来计算文件时,它不会获取文件

如果我评论这两条线,我用它来获取 doChangeProfilePhoto 中的文件计数,如下所示:

//if(UploadFileUtil.getUploadFileCount(request) != 1)
//    throw new FileUploadException("There is Multiple/None File upload for Profile Photo. ");

然后它的工作。为什么会发生这种情况,如果有一次使用它,那么在它无法检索文件之后。它背后的原因是什么,有没有办法使用普通上传来计算文件......以及它们的名字?

4

0 回答 0