weixin_33690367 2014-10-16 14:08 采纳率: 0%
浏览 13

公用文件上传

I am trying to upload a file from a jsp/ajax using apache commons file upload libs. Below is the code:

JSP:

function importFileListForm()  
{
var html = "<div><h3>Add File</h3></div>";
html += '<table style="margin-top:20px;" width="100%"><tbody>'; 
html += '<tr><td>Choose File</td><td><input id="fileId" type="file" name="fileName" required size="50"/></td><td></td></tr>';
html += '</tbody></table>';

$("#dialog-modal-content").html(html);
$(function() {
    $( "#dialog-modal" ).dialog({
      height: 300,
      width: 600,
      modal: true,
      buttons: {
        "Cancel": function() {
            $(this).dialog("close");
        },
        "Submit": function() {
            importFileConfirm($("#fileId").val());
        }
      }
    }); 
});
}

function importFileConfirm(file)
{
var url = "/config/fileManageAction.do?method=importFile&file=" +file;
var req = new FormData();
req.append("f", file);
$.ajax({
    type: 'POST',
    data: req,
    url: url,
    processData: false,
    contentType: false,
    success: function(data) {console.log(data);}

});
}

My method in action class:

      DiskFileItemFactory factory = new DiskFileItemFactory();
      factory.setSizeThreshold(maxSize);
      factory.setRepository(new File("/production/cpu/temp.xlsx"));

      ServletFileUpload upload = new ServletFileUpload(factory);
      // maximum file size to be uploaded.
      upload.setSizeMax( maxSize );
      try{
          List fileItem = upload.parseRequest(request);
          java.lang.System.out.println("List  "+fileItem.size());

    //    Part filePart = request.getPart("file");
    //    java.lang.System.out.println("File Part "+filePart);

         // Process the uploaded file items
          Iterator i = fileItem.iterator();
          while(i.hasNext()){
              FileItem fi = (FileItem)i.next();
              if(!fi.isFormField()){
                  java.lang.System.out.println("Fi "+fi);
                  String fieldname = fi.getFieldName();
                  java.lang.System.out.println("FieldName "+fieldname);
                  String filename =  FilenameUtils.getName(fi.getName());
                  java.lang.System.out.println("File Name "+fi.getString());
                  InputStream filecontent = fi.getInputStream();
              }else{
                  java.lang.System.out.println("not a form field");
                  java.lang.System.out.println("Fi "+fi);
                  String fileName = fi.getName();
                  java.lang.System.out.println("File Name: "+fileName);
              }


          }
      }catch(Exception e){
          java.lang.System.out.println("Exception occured "+e);
          e.printStackTrace();
      }

 //   }

 }

But in my file item i am only getting the field name and the fi.getName() is returning null. Can some one throw some light on what i am missing?.

I cannot use getParts() functions as my tomcat doesn't support it.

  • 写回答

1条回答 默认 最新

  • weixin_33725126 2014-10-16 14:19
    关注

    You have to add the File object to the FormData object. $("#fileId").val() gives you the filename, to get the file use $("#fileId")[0].files[0]

    req.append("f", $("#fileId")[0].files[0]);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题