2012-02-15 25 views
0

我試圖實現dojox.form.Uploader以上傳文件。我在JAVA中創建了一個簡單的REST服務。 從Firefox和Chrome中,服務獲取表單並保存數據,但IE8並非如此。dojox.form.Uploader - 提交 - 沒有達到IE8的REST服務

在IE8中,與FF/Chr中的觸發器相比,我得到完全不同的響應對象。它實際上是一個包含文件信息的數組,所有這些都包含「錯誤」 - 「服務器超時」消息。事實上,表單提交甚至沒有達到該服務。

我剛剛開始使用JAVA REST服務,所以請原諒所有明顯的錯誤。

感謝堆。 M.

客戶端代碼:

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Uploading test</title> 
<link rel="stylesheet" type="text/css" 
    href="https://ajax.googleapis.com/ajax/libs/dojo/1.6/dojo/resources/dojo.css" /> 
<link rel="stylesheet" type="text/css" 
    href="https://ajax.googleapis.com/ajax/libs/dojo/1.6/dijit/themes/tundra/tundra.css" /> 
<link rel="stylesheet" type="text/css" 
    href="https://ajax.googleapis.com/ajax/libs/dojo/1.6/dijit/themes/nihilo/nihilo.css" /> 
<link rel="stylesheet" type="text/css" 
    href="https://ajax.googleapis.com/ajax/libs/dojo/1.6/dijit/themes/soria/soria.css" /> 
<link rel="stylesheet" type="text/css" 
    href="https://ajax.googleapis.com/ajax/libs/dojo/1.6/dojox/form/resources/FileUploader.css" /> 
<link rel="stylesheet" type="text/css" 
    href="https://ajax.googleapis.com/ajax/libs/dojo/1.6/dojox/form/resources/UploaderFileList.css" /> 
<link rel="stylesheet" type="text/css" 
    href="https://ajax.googleapis.com/ajax/libs/dojo/1.6/dojox/form/resources/FileInput.css" /> 
<script type="text/javascript">djConfig = { parseOnLoad:true, isDebug:true, dojoBlankHtmlUrl: 'blank.html' };</script> 
<script type="text/javascript" 
    src="https://ajax.googleapis.com/ajax/libs/dojo/1.6/dojo/dojo.xd.js"></script> 
<script> 
     dojo.require("dojox.form.Uploader"); 
     dojo.require("dojox.form.uploader.FileList"); 
     dojo.require("dijit.form.Button"); 
     dojo.require("dijit.form.TextBox"); 
     dojo.require("dijit.form.CheckBox"); 
     dojo.require("dojo.io.iframe"); 
     dojo.require("dojox.form.FileInput"); 
     dojo.require("dojox.form.uploader.plugins.Flash"); 
     dojo.require("dojox.form.uploader.plugins.HTML5");  

     function prepareForm(){ 
      var form = dojo.byId("myform");    

      // Disable the button at startup 
      //dijit.byId('submitId').set("disabled", true); 

      // Connect to the onChange event of file upload stuff. 
      dojo.connect(dojo.byId("uploader"), "onchange", function(){ 
       //checkExtension(); 
      }); 

     // Connect to the onChange event of file upload stuff. 
      dojo.connect(dijit.byId("uploader"), "onComplete", function(response){ 
       dojo.byId("response").innerHTML = "Form posted with status : " + response; 
      });       
     } 
     dojo.ready(prepareForm); 

    </script> 
</head> 
<body class="soria"> 
    <b>Simple Form:</b> 
    <br> 
    <form method="post" action="jersey/fileupload2" id="myForm" 
     enctype="multipart/form-data"> 
     <legend>file upload test</legend> 
     <input name="uploadedfile" multiple="true" type="file" id="uploader" 
      dojoType="dojox.form.Uploader" label="Select XLSX Files" 
      style="width: 150px;"> 
     <div id="files" dojoType="dojox.form.uploader.FileList" 
      uploaderId="uploader" style="width: 300px;"></div> 
     <br /> <input type="submit" label="Submit" 
      dojoType="dijit.form.Button" id="submitId" /> 
    </form> 
    <br> 
    <b>Result</b> 
    <div id="response"></div> 

</body> 
</html> 

服務器端代碼

@Path("fileupload2")public class FileUploadResource2 { 
@POST 
@Consumes("multipart/form-data") 
@Produces("text/html") 
public String loadFile(@Context HttpServletRequest request) { 
    String resultStatus="{response:'fail'}"; 
    String fileRepository="C:\\TEMP\\";  
    if (ServletFileUpload.isMultipartContent(request)) { 
     FileItemFactory factory = new DiskFileItemFactory(); 
     ServletFileUpload upload = new ServletFileUpload(factory); 
     List<FileItem> items = null; 
     try {   
      items = upload.parseRequest(request); 
     } catch (FileUploadException e) {    
      e.printStackTrace(); 
     } 
     if(items!=null) { 
     Iterator<FileItem> iter = items.iterator(); 
     while (iter.hasNext()) { 
      FileItem item = iter.next();     
      if(!item.isFormField() && item.getSize() > 0) { 
        String fileName = processFileName(item.getName()); 
        resultStatus="{response:'ok.'}"; 
        try { 
         //throw new Exception("error happened."); 
         item.write(new File(fileRepository+fileName)); 
        } catch (Exception e) { 
         resultStatus="{response:'failed.'}"; 
         //e.printStackTrace(); 
         //return "{result:'" + e.fillInStackTrace() + "'}";       
        } 

      }    
     } 
     }   
    } 
    return resultStatus; 
} 

private String processFileName(String fileNameInput) { 
    String fileNameOutput=null; 
    fileNameOutput = fileNameInput.substring(fileNameInput.lastIndexOf("\\")+1,fileNameInput.length()); 
    return fileNameOutput; 
} 

}

回答

0

這是我的解決方案:

protected void doPost(HttpServletRequest request, 
     HttpServletResponse response) throws ServletException, IOException { 

    try { 
     String dir = getServletConfig().getInitParameter(
       "javax.servlet.context.tempdir"); 
     logger.info("FileUpload: doPost called, directory for the upload: " 
       + dir); 

     boolean isMultipart = ServletFileUpload.isMultipartContent(request); 
     logger.info("Is multipart: " + isMultipart); 

     ServletOutputStream out = response.getOutputStream(); 
     response.setContentType("text/html"); 
     response.setCharacterEncoding("utf-8"); 

     if (isMultipart) { 

      // Create a new file upload handler 
      ServletFileUpload upload = new ServletFileUpload(); 
      // Parse the request 
      FileItemIterator iter = upload.getItemIterator(request); 

      ArrayList<FileInfo> fileInfos = new ArrayList<FileInfo>(); 
      boolean html5 = false; 

      while (iter.hasNext()) { 
       FileItemStream item = iter.next(); 

       String name = item.getFieldName(); 
       InputStream stream = item.openStream(); 
       if (item.isFormField()) { 
        logger.info("Form field " + name + " with value " 
          + Streams.asString(stream) + " detected."); 
       } else { 
        logger.info("File field " + name + " with file name " 
          + item.getName() + " detected."); 
        if (name.contains("uploadedfiles")) 
         html5 = true; 


        // Process the input stream 
        File file = new File(dir + item.getName()); 
        FileUtils.copyInputStreamToFile(stream, file); 

        FileInfo e = new FileInfo(); 
        e.setName(item.getName()); 
        e.setFile(item.getName()); 
        e.setExt(item.getContentType()); 

        fileInfos.add(e); 

       } 

      } 

      printJSON(out, html5, fileInfos); 

     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

private void printJSON(ServletOutputStream out, boolean html5, ArrayList<FileInfo> fileInfos) 
     throws IOException { 
    if (!html5) 
     out.print("<textarea>"); 
    out.print("["); 

    for (FileInfo fileInfo : fileInfos) { 
     out.print("{"); 
     out.print("\"name\": \"" + fileInfo.getName() + "\","); 
     out.print("\"file\": \"" + fileInfo.getFile() + "\","); 
     out.print("\"type\": \"" + fileInfo.getExt() + "\""); 
     out.print("}"); 
    } 

    out.print("]"); 
    if (!html5) 
     out.print("</textarea>"); 
} 

FileInfo的是一個簡單的類w ^以下字段:

private String name; 
private String file; 
private String ext;