gpt4 book ai didi

java - GwtUpload Servlet 错误

转载 作者:行者123 更新时间:2023-12-01 17:35:01 26 4
gpt4 key购买 nike

我正在尝试实现 GwtUpload 库的基本示例,如 here 所示。 .

在我的服务器代码中,出现以下错误:

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

我不明白为什么会发生这种情况。 DiskFileItem 是 FileItem 的子类,应该可以工作。我已经在调试器中单步调试并确认这两个类确实是它们看起来的样子,但分配失败了。

更奇怪的是,当我尝试在监 window 口中调用 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();
}
}
}

最佳答案

确保类路径上没有多个版本的 commons-fileupload。

关于java - GwtUpload Servlet 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7404891/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com