开发者

Error with GwtUpload Servlet

I'm trying to implement the Basic example for the GwtUpload library, as found here.

In my server code, I get the following error:

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

I 开发者_JAVA技巧can't figure out why this is happening. DiskFileItem is a subclass of FileItem and should work. I've stepped through in the debugger and confirmed that the two classes are what they appear to be, yet the assignment fails.

Even more strangely, when I attempt to call the FileItem methods in the watch window, I have no problems, but if I attempt to access them in the code, I get the error.

Here is my Servlet code:

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();
        }
    }
}


Make sure you don't have multiple versions of commons-fileupload on the classpath.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜