在jsp或multipart / form-data中上传文件

时间:2013-01-06 07:49:35

标签: java jsp servlets file-upload

实际上我试图在使用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. ");

然后它的工作。为什么它会发生,如果有一次它被使用,所以它无法检索文件。这背后的原因是什么 有没有办法使用普通上传来计算文件...还有他们的名字吗?

0 个答案:

没有答案