且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Servlet接收0Bytes的文件

更新时间:2023-12-03 14:25:04

  for(FileItem item:sessionFiles){
if(false == item.isFormField()){
尝试{
///创建一个放置在默认系统临时文件夹中的临时文件
File file = File.createTempFile(upload-,.bin );
item.write(file);

///用收到的文件保存一个列表
receivedFiles.put(item.getFieldName(),file);
receivedContentTypes.put(item.getFieldName(),item.getContentType());

///向客户端发送定制消息。
response + =文件另存为+ file.getAbsolutePath();

//下方会产生问题。
removeItem(request,item.getFieldName());

} catch(Exception e){
throw new UploadActionException(e);
}
}
}

我将每个文件在处理它之后使用 removeItem(request,item.getFieldName()); 。不知何故,它删除 error.txt 文件。我不知道为什么。但删除该行后。它为我工作。


I'm working on multiple file upload. Having strange issue when uploading multiple files.

Case: When I select multiple files, if one of file size is big compare to other its not submitted to servlet. I'm selecting below files(Image). I used breakpoint and went to step by step and found that error.txt's size is 0Bytes in servlet. If I upload this by selecting as single its uploading properly.

Having same issue with other file when its big, its not specific to error.txt. I just gave you the case

FYI: I'm using gwtupload plugin for multiple upload. Here is the link of project : https://code.google.com/p/gwtupload


Update:

@Manolo Why you used cont variable in servlet?

 int cont = 0;
 for (FileItem item : sessionFiles) {
    if (false == item.isFormField()) {
       cont ++;

After tons of try I found the solution so let me show you where I made mistake in code.

for (FileItem item : sessionFiles) {
      if (false == item.isFormField()) {
        try {
          /// Create a temporary file placed in the default system temp folder
          File file = File.createTempFile("upload-", ".bin");
          item.write(file);

          /// Save a list with the received files
          receivedFiles.put(item.getFieldName(), file);
          receivedContentTypes.put(item.getFieldName(), item.getContentType());

          /// Send a customized message to the client.
          response += "File saved as " + file.getAbsolutePath();

          // Below line is creating the problem.
          removeItem(request, item.getFieldName());

        } catch (Exception e) {
          throw new UploadActionException(e);
        }
      }
    }

I'm removing each file using removeItem(request, item.getFieldName()); after processing it. Somehow its removing the error.txt file. I don't know why. But after removing that line. It worked for me.