GwtUploadライブラリの基本的な例を実装しようとしています(here)。私のサーバーコードでGwtUploadサーブレットのエラー
、私は次のエラーを取得する:
Exception java.lang.ClassCastException: org.apache.commons.fileupload.disk.DiskFileItem cannot be cast to org.apache.commons.fileupload.FileItem
なぜこれが起こっている私が把握することはできません。 DiskFileItemはFileItemのサブクラスであり、動作するはずです。私はデバッガでステップを踏んで、2つのクラスが表示されていることを確認しましたが、割り当ては失敗します。
さらに奇妙なことに、ウォッチウィンドウでFileItemメソッドを呼び出そうとすると問題はありませんが、コード内でアクセスしようとするとエラーが発生します。ここで
は私のサーブレットのコードです:
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();
}
}
}
ありがとうございました。それが問題でした。 –
申し訳ありません。私はあまりにも早く話しました。 –
このように見えますが、結局正解でした! –