利用JadePool中的万能表单解析工具cn.jadepool.web.ProcessForm可以轻松解析表单提交的全部信息,甚至可以轻松获取一个未知结构的表单由那些字段组成。以下是在xheditor上传图片文件中的应用。
1、解析表单:
String allowFilter = "bmp|png|jpg|gif|swf|wma|mp3|avi|doc|psd|zip|jar|war|pdf|mpg|txt|sql"; ProcessForm form = ProcessForm.instance();//cn.jadepool.web.ProcessForm form.setFilter(allowFilter); form.setAllowFileSize(allowSize); form.parse(request); Map _m = form.getRecordFullInfo(); System.out.println(_m);// {filedata_filename=5830.jpg, filedata_ext=jpg, filedata_contenttype=image/pjpeg, filedata_size=154470, filedata_height=768, filedata_width=1366, filedata=[B@3eba9ff8, filedata_originfilename=5830.jpg}通过System.out.println(_m);语句轻松地将xheditor上传文件的表单结构呈现了出来,Map对象_m中的键名就是原表单中的字段名。
2、保存上传的文件
String realPath = application.getRealPath("") + java.io.File.separator + "page" + java.io.File.separator + "upload";// String webPath = request.getContextPath() + "/page/upload";// String filename = (String) _m.get("filedata_filename"); //Integer fileSize = (Integer) _m.get("filedata_size"); File f = new File(realPath); f.mkdirs(); f = new File(realPath + java.io.File.separator + filename); byte[] filedata = (byte[]) _m.get("filedata"); form.writeFile(filedata, f);3、将上传的信息保存到数据库中
//以下代码,将上传的文件信息保存到数据库picture表中 //Jade j = new Jade(); //_m.put("news_file", urlFile); //j.save("picture", _m); //j.commit();
附:在线编辑文件newsdoc.html和处理上传文件upload.jsp完整的源代码
1、newsdoc.html文件
<!DOCTYPE html> <!-- 万能表单解析工具cn.jadepool.web.ProcessForm在xheditor上传文件中的应用 胡开明 2015-03-27 xheditor在线编辑器,请到相关网站下载 --> <html> <head> <title>万能表单解析工具cn.jadepool.web.ProcessForm在xheditor上传文件中的应用</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="Content-Type" content="text/html; charset=GBK" /> <!--请修改以下三个文件的路径--> <script type="text/javascript" src="../resources/xheditor/1.2.1/jquery/jquery-1.4.4.min.js"></script> <script type="text/javascript" src="../resources/xheditor/1.2.1/xheditor-1.2.1.min.js"></script> <script type="text/javascript" charset="utf-8" src="../resources/xheditor/1.2.1/xheditor_lang/zh-cn.js"></script> <script type="text/javascript"> $(pageInit); function pageInit() { $('#elm1').xheditor({upLinkUrl: "upload.jsp", upLinkExt: "zip,rar,txt", upImgUrl: "upload.jsp", upImgExt: "jpg,jpeg,gif,png", onUpload: insertUpload}); } function insertUpload(arrMsg) { //alert("上传成功!"); } </script> </head> <body> <textarea id="elm1" name="elm1" style="width: 800px;height:300px;">请上传文件进行测试...</textarea> </body> </html>
<%-- 万能表单解析工具ProcessForm类的应用,在工程中下载并添加jadepool-1.XX.jar包 Document : upload.jsp Created on : 2015-3-26, 22:48:02 Author : 胡开明 Description: 专为xhEditor编写的上传处理文件,为了兼容xheditor,本JSP文件编码采用utf-8,如果用GBK很可能导致上传接口失败。 --%> <%@page contentType="text/html" pageEncoding="utf-8"%> <%@page import="java.io.File,java.util.*,cn.jadepool.sql.*,cn.jadepool.web.*,cn.jadepool.util.*" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charsetutf-8"> <title>JSP Page</title> <% String realPath = application.getRealPath("") + java.io.File.separator + "page" + java.io.File.separator + "upload";// String webPath = request.getContextPath() + "/page/upload";// boolean isTooBig = false; int allowSize = 20000000;//20m String returnMsg = "{err:'',msg:''}";//xhEditor规定的返回字符串JSON格式 try { String allowFilter = "bmp|png|jpg|gif|swf|wma|mp3|avi|doc|psd|zip|jar|war|pdf|mpg|txt|sql"; ProcessForm form = ProcessForm.instance();//cn.jadepool.web.ProcessForm form.setFilter(allowFilter); form.setAllowFileSize(allowSize); form.parse(request); Map _m = form.getRecordFullInfo(); //System.out.println(_m);// {filedata_filename=5830.jpg, filedata_ext=jpg, filedata_contenttype=image/pjpeg, filedata_size=154470, filedata_height=768, filedata_width=1366, filedata=[B@3eba9ff8, filedata_originfilename=5830.jpg} String filename = (String) _m.get("filedata_filename"); //Integer fileSize = (Integer) _m.get("filedata_size"); File f = new File(realPath); f.mkdirs(); f = new File(realPath + java.io.File.separator + filename); byte[] filedata = (byte[]) _m.get("filedata"); form.writeFile(filedata, f); String urlFile = webPath + "/" + filename; returnMsg = "{err:'',msg:'" + urlFile + "'}";//xhEditor规定的返回字符串JSON格式 //以下代码,将上传的文件信息保存到数据库picture表中 //Jade j = new Jade(); //_m.put("news_file", urlFile); //j.save("picture", _m); //j.commit(); } catch (Exception e) { e.printStackTrace(); } finally { out.println(returnMsg); } %> </head> <body> </body> </html>