JFinal 3ff0 +CKEditor文件上传后缀验证,大小验证。
2014-07-02 13:57
741 查看
public void index(){ String extName = "";// 扩展名 String fileName = "";// 文件名 String newFileName = "";// 新文件名 try { String strPath = JFinal.me().getServletContext().getRealPath("/upload/news/"); UploadFile upload = getFile("bigPic", strPath, 2097152); String uploadFileName = upload.getFileName(); String CKEditorFuncNum = getPara("CKEditorFuncNum"); // 获取扩展名 if (uploadFileName.lastIndexOf(".") > -1) { extName = getExtention(upload.getFileName()); fileName = uploadFileName.substring(0,uploadFileName.lastIndexOf(".")); } newFileName = fileName+"-"+Tools.getFileName() + extName; if (!".jpg.gif.png,bmp.JPG.GIF.PNG.BMP".contains(extName)) { FileKit.delete(upload.getFile()); renderHtml("<script type=\"text/javascript\">alert(\"请上传图片文件。\");</script>"); } else { File f = upload.getFile(); InputStream is = new FileInputStream(f); OutputStream os = new FileOutputStream(new File(strPath + File.separator + newFileName)); try { byte[] buffer = new byte[1024 * 1024]; while (is.read(buffer) > 0) { os.write(buffer); } } catch (Exception e) { e.printStackTrace(); } finally { if (is != null) { is.close(); } if (os != null) { os.close(); } } // 删除原文件 FileKit.delete(upload.getFile()); // 返回给ckeditor renderHtml("<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ", '"+getBasePath(getRequest())+"upload/news/" + newFileName + "', '');</script>"); } }catch (RuntimeException e) { // TODO: handle exception renderHtml("<script type=\"text/javascript\">alert(\"上传的文件有误,请重新上传。\");javascript:history.go(-1);</script>"); } catch (Exception e) { e.printStackTrace(); renderHtml("<script type=\"text/javascript\">alert(\"上传文件失败,请联系管理员。\");</script>"); } }
相关文章推荐
- js实现图片文件校验,验证上传的文件是图片和其后缀名,大小
- Struts Validator 上传文件大小的验证
- 用javascrit验证上传文件大小
- struts文件上传(上传文件大小设置、验证文件大小)
- javascrip客户端验证文件大小及文件类型并重置上传
- 关于163邮箱,上传附件,本地验证文件大小的问题。
- ASP.NET上传图片,服务器端验证,宽度,高度,文件类型,尺寸,文件大小,图片上传,图片格式检查
- 客户端验证上传文件大小
- javascrip客户端验证文件大小及文件类型并重置上传
- Js验证文件上传大小限制
- struts文件上传(上传文件大小设置、验证文件大小)
- js实现图片文件校验,验证图片的后缀名,大小
- javascrip客户端验证文件大小及文件类型并重置上传
- 通过客户端验证上传图片文件大小的ASP源码
- 用javascrit验证上传文件大小
- JavaScript验证上传文件格式及大小
- JQUERY验证上传文件大小
- “js+flash上传组件”客户端验证文件大小