【问题标题】:Servlet receives 0Bytes of fileServlet 收到 0 字节的文件
【发布时间】:2013-10-08 13:12:39
【问题描述】:

我正在处理多个文件上传。上传多个文件时出现奇怪的问题。

案例:当我选择多个文件时,如果其中一个文件比其他文件大,它不会提交给 servlet。我正在选择以下文件(图片)。我用断点一步一步去,发现error.txt的大小在servlet中是0Bytes。如果我通过选择正确上传来上传它。

其他文件很大时也有同样的问题,它不是 error.txt 特有的。我刚把案子给你了

仅供参考:我正在使用gwtupload 插件进行多次上传。这是项目的链接:https://code.google.com/p/gwtupload


更新:

@Manolo 为什么在 servlet 中使用 cont 变量?

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

【问题讨论】:

  • 您使用的是哪个版本的 gwtupload?
  • 我试过gwtupload-0.6.6.jargwtupload-0.6.7-SNAPSHOT.jar
  • 所以当您启用多个(这是一个实验性功能)时它会在 gwtupload 中发生,或者它总是使用 MultiUplader 发生?
  • 我在几个页面中使用了MultiUplader,并且到处都面临同样的问题。
  • 我在同一页面中使用了 3 - 4 MultiUploader。它会导致问题吗?

标签: java gwt servlets gwtupload


【解决方案1】:

经过大量尝试,我找到了解决方案,所以让我告诉你我在代码中出错的地方。

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);
        }
      }
    }

我在处理后使用removeItem(request, item.getFieldName()); 删除每个文件。不知何故,它删除了error.txt 文件。我不知道为什么。但是在删除那条线之后。它对我有用。

【讨论】:

  • 很明显,您正在将项目添加到 receivedFiles 哈希表中,同时您运行removeItem 删除它们,所以最后您没有保存任何文件。您只需在 for 循环后运行 removeSessionFileItems(request); 即可删除会话文件以供下次上传。
  • @Manolo 感谢您的支持。我也写博客。它的 www.javaquery.com 。我一定会写你的插件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-28
  • 2019-09-12
  • 2012-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多