サーバーコード(HttpServlet内)から、ファイルが大きすぎる場合は例外をスローします。
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
...
// Check if the blob has correct size, otherwise delete it
final BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey);
long size = blobInfo.getSize();
if(size > 0 && size <= BasicConstants.maxImageSize){
res.sendRedirect("/download?blob-key=" + blobKey.getKeyString());
} else { // size not allowed
bs.delete(blobKey);
throw new RuntimeException(BasicConstants.fileTooLarge);
}
クライアントコードから、このスニペットで例外を正常にキャッチするために欠落しています。
try {
uploadForm.submit(); // send file to BlobStore, where the doPost method is executed
} catch (Exception ex) {
GWT.log(ex.toString());
}
ただし、この他のクライアントコードスニペットから、まったく信頼できない醜い回避策で例外がスローされたときをどういうわけか検出しています。
uploadForm.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
@Override
public void onSubmitComplete(SubmitCompleteEvent event) {
// This is what gets the result back - the content-type *must* be
// text-html
String imageUrl =event.getResults();
// This ugly workaround apparently manages to detect when the server threw the exception
if (imageUrl.length() == 0) { // file is too large
uploadFooter.setText(BasicConstants.fileTooLarge);
} else { // file was successfully uploaded
...
}
Eclipseの開発モードビューは、「キャッチされない例外」タイプのエラーを報告します。これは、私がそれを検出するのに本当に悪い仕事をしていることを示唆しています。
誰かが例外を適切にキャッチする方法を教えてもらえますか、そして私が使用している回避策が意味をなすかどうか。
ありがとう!