2011-09-13 38 views
0

我正在嘗試實現GwtUpload庫的基本示例,如發現hereGwtUpload Servlet錯誤

在我的服務器代碼,我得到以下錯誤:

Exception java.lang.ClassCastException: org.apache.commons.fileupload.disk.DiskFileItem cannot be cast to org.apache.commons.fileupload.FileItem

我想不通爲什麼發生這種情況。 DiskFileItem是FileItem的一個子類,應該可以工作。我已經在調試器中加入並確認這兩個類是他們看起來是的,但是分配失敗了。

更奇怪的是,當我嘗試在監視窗口中調用FileItem方法時,我沒有任何問題,但是如果我嘗試在代碼中訪問它們,則會出現錯誤。

這裏是我的servlet代碼:

public class GwtUploadServlet extends UploadAction 
{ 
    private static final long serialVersionUID = 1L; 

    /** 
    * Maintain a list with received files and their content types. 
    */ 
    Hashtable<String, String> receivedContentTypes = new Hashtable<String, String>(); 

    /** 
    * Maintain a list with received files. 
    */ 
    Hashtable<String, File> receivedFiles = new Hashtable<String, File>(); 

    /** 
    * Override executeAction to save the received files in a custom place and 
    * delete this items from session. 
    */ 
    @Override 
    public String executeAction(HttpServletRequest request, 
           List<FileItem> sessionFiles) throws UploadActionException 
    { 
     String response = ""; 
     int cont = 0; 
     for (int i = 0 ; i < sessionFiles.size(); i++) 
     { 
      if (false == sessionFiles.get(i).isFormField()) 
      { 
       cont++; 
       try 
       { 
        ///Create a temporary file placed in the default system 
        // temp folder 
        File file = File.createTempFile("upload-", ".bin"); 
        sessionFiles.get(i).write(file); 

        ///Save a list with the received files 
        receivedFiles.put(sessionFiles.get(i).getFieldName(), file); 
        receivedContentTypes.put(sessionFiles.get(i).getFieldName(), 
              sessionFiles.get(i).getContentType()); 

        ///Send a customized message to the client. 
        response += "File saved as " + file.getAbsolutePath(); 

       } 
       catch (Exception e) 
       { 
        throw new UploadActionException(e); 
       } 
      } 
     } 

     ///Remove files from session because we have a copy of them 
     removeSessionFileItems(request); 

     ///Send your customized message to the client. 
     return response; 
    } 

    /** 
    * Get the content of an uploaded file. 
    */ 
    @Override 
    public void getUploadedFile(HttpServletRequest request, 
      HttpServletResponse response) throws IOException 
    { 
     String fieldName = request.getParameter(PARAM_SHOW); 
     File f = receivedFiles.get(fieldName); 
     if (f != null) 
     { 
      response.setContentType(receivedContentTypes.get(fieldName)); 
      FileInputStream is = new FileInputStream(f); 
      copyFromInputStreamToOutputStream(is, response.getOutputStream()); 
     } 
     else 
     { 
      renderXmlResponse(request, response, ERROR_ITEM_NOT_FOUND); 
     } 
    } 

    /** 
    * Remove a file when the user sends a delete request. 
    */ 
    @Override 
    public void removeItem(HttpServletRequest request, String fieldName) 
      throws UploadActionException 
    { 
     File file = receivedFiles.get(fieldName); 
     receivedFiles.remove(fieldName); 
     receivedContentTypes.remove(fieldName); 
     if (file != null) 
     { 
      file.delete(); 
     } 
    } 
} 

回答

5

確保你沒有在類路徑中的commons-文件上傳的多個版本。

+0

謝謝。這是問題所在。 –

+1

對不起。我說得太快了。 –

+0

看起來這畢竟是正確的答案! –