Servlet接收0Bytes的文件 [英] Servlet receives 0Bytes of file

查看:144
本文介绍了Servlet接收0Bytes的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



大小写:当我选择多个文件时,如果其中一个文件大小比较大,提交给servlet。我选择下面的文件(图片)。我使用了断点并一步一步地发现servlet中的 error.txt 的大小为 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 ++;


解决方案

  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.

这篇关于Servlet接收0Bytes的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆