private Boolean checkImg(File upload) { // 上傳圖片以前先判斷圖片大小 Boolean flag = false; String info = ""; if (upload == null) { info = "<script>alert('添加失敗,上傳文件不存在');history.back();</script>"; } else if (upload.length() > MERIMG_MAX_SIZE) { info = "<script>alert('添加失敗,上傳文件大小不能超過20M!');history.back();</script>"; } else if (upload.length() == 0) { info = "<script>alert('添加失敗,上傳文件大小爲0');history.back();</script>"; } else { flag = true; } if (!flag) { ServletActionContext.getResponse().setContentType( "text/html;charset=UTF-8"); PrintWriter out; try { out = ServletActionContext.getResponse().getWriter(); out.print(info); out.close(); } catch (Exception e) { e.printStackTrace(); } } return flag; }