尝试上传文件,但不是由请求发送

时间:2016-11-16 08:21:46

标签: java jsp file-upload struts2 jquery-file-upload

我尝试使用类型文件的输入上传文件,我还有另一个我想提交的参数

在我的表单代码下面:

<form  id="fileupload" action="uploadMassContentsFromExcelSheetNew"  method="post" 
  enctype="multipart/form-data" onsubmit="return doMassValidate();"  >          

      <input type="hidden" id="fileName" name="fileName" value=""/>
      <input type="hidden" id="serviceIdMass" name="serviceIdMass"   value=""/>
      <input type="hidden" id="contentTypeMass" name="contentTypeMass"   value=""/> 



     <div id='fileDiv' class="row fileupload-buttonbar">
        <div class="col-md-12 col-xs-12 col-lg-12">
        <div class="input-group input-group-file">
        <input type="text" class="form-control" 
                        disabled="disabled" id="uploadFile" name="uploadFile">
        <span class="input-group-btn">
             <span class="btn btn-primary btn-file">
        <i class="icon md-upload" aria-hidden="true"></i>
      <input type="file" name="files" id="uploadBtn">
          </span>
          </span>   
        <p style="word-wrap: break-word;">
            <small id="fileError" style="display: none;"
                            class="help-block" data-fv-validator="notEmpty"
                            data-fv-result="VALIDATED">
                <s:property value="getText('content.file.error')" />
            </small>
        </p>

        </div>
        </div>
     </div> 

            <button  name="sub" type="submit" >submit</button>

</form>

JS代码:

function doMassValidate(){
    var hasSelectedFile=checkIfHasSelectedFile();
    if(!hasSelectedFile){
        fileErrorMessage();
        $("#fileError").hide();

        return false;
    }
    else{
        removeErrorClass('fileDiv');
        return true;
    }
}
function checkIfHasSelectedFile(){
    var hasNotUploadFile=   $('#uploadBtn').val(); 
    if(hasNotUploadFile=="" || hasNotUploadFile=='' )
         return false;
     else return true;

}

这是我的行动:

public String uploadMassContentsFromExcelSheetNew() {
    HSSFSheet sheet = null;
    HSSFWorkbook workbook = null;
    FileInputStream inputStream = null;
    result=new JSONObject();

    try {
            if (fileName != null){
            fileName=fileName.trim();

        if  (fileName.endsWith(".xls") || fileName.endsWith(".xlsx") )  {

    HttpServletRequest request = ServletActionContext.getRequest();
                System.out.println("Parts*******:"+request.getParts()); //return   null


                MultiPartRequestWrapper multiWrapper=(MultiPartRequestWrapper)ServletActionContext.getRequest();
            String fileNameParam=servletRequest.getParameter("fileName");//i get the file name value
String fileParam=servletRequest.getParameter("files");// i cant get param with name "files" when i inspect the servletRequest

            Part filePart =request.getPart("files");//return null
            File[] f=multiWrapper.getFiles("files"); //return null
            String fileName=multiWrapper.getFileNames("files")[0];
            Enumeration fileParameterNames = multiWrapper.getParameterNames();
            String param = request.getParameter("files");
        }
            }
    }
    catch(Exception ex){
        ex.printStackTrace();
    }
}       

我正在使用fileUpload拦截器:

<interceptor-ref name="fileUpload"/>
<interceptor-ref name="basicStack"/>

这是请求:

------WebKitFormBoundaryjBpjIWpSdq4RzL0X
Content-Disposition: form-data; name="fileName"

C:\fakepath\SMSMassploadOladDate.xls
------WebKitFormBoundaryjBpjIWpSdq4RzL0X
Content-Disposition: form-data; name="serviceIdMass"

647
------WebKitFormBoundaryjBpjIWpSdq4RzL0X
Content-Disposition: form-data; name="contentTypeMass"

1

------WebKitFormBoundaryjBpjIWpSdq4RzL0X
Content-Disposition: form-data; name="files"; filename=""
Content-Type: application/octet-stream

不应该将文件内容类型设置为&#34; application/ms-excel,application/vnd.ms-excel&#34; 而不是application/octet-stream?和文件名不应该为空?

我真的不知道文件有什么问题?

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,在struts.xml中我只是扩展&#34; struts-default&#34;,它应该是扩展&#34; struts-default,json-default&#34;。 它也与blueimp插件相关,我试图使用,&#34; main.js&#34;调用了库,使我的代码不起作用。