JQuery的Ajax提交
鉴于项目需要,开始边看Demo边使用JQuery。现将项目中暂时遇到的三种使用JQuery进行Ajax提交的方式做个总结。因为没有系统学习,有点山寨,只求在项目中实现功能。
1.URL+GET参数提交
这种方式是最普遍的,只要包含jquery.js就可以正常使用。
/* * 产品图片上传 * * author : Emerson * * Yiihee , Inc. */import org.ofbiz.base.util.*;import org.ofbiz.base.util.string.*;import org.ofbiz.entity.*;import java.text.SimpleDateFormat;import java.util.*;import java.io.*;import org.apache.commons.fileupload.disk.*;import org.apache.commons.fileupload.servlet.*;import org.apache.commons.fileupload.*;configProperties = UtilProperties.getProperties("opencommon.properties");String imageUploadServerPath = configProperties.get("openb2c.image.upload.server.path");//SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss"); //Date date = new Date(); //String filename = sf.format(date);String fileName;File uploadPath = new File(imageUploadServerPath);//上传文件目录 if (!uploadPath.exists()) { uploadPath.mkdirs(); } // 临时文件目录 File tempPathFile = new File(imageUploadServerPath+"\\temp\"); if (!tempPathFile.exists()) { tempPathFile.mkdirs(); } try { // Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); // Set factory constraints factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb factory.setRepository(tempPathFile);//设置缓冲区目录 // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Set overall request size constraint upload.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB List items = null; items = upload.parseRequest(request);//得到所有的文件 if(items==null||items.size()==0){ String msg = "ImgEmptyErr"; context.put("result", msg); return; } Iterator i = items.iterator(); //此处实际只有一个文件 while (i.hasNext()) { FileItem fi = (FileItem) i.next(); fileName = fi.getName(); if (!UtilValidate.isEmpty(fileName)) { File fullFile = new File(fi.getName()); //File fullFile = new File(filename); File savedFile = new File(uploadPath, fullFile.getName()); int j = 0; while(savedFile.exists()){ j++; savedFile = new File(uploadPath, savedFile.getName().substring(0,savedFile.getName().lastIndexOf(".")-1)+"("+j+")"+savedFile.getName().substring(savedFile.getName().lastIndexOf("."),savedFile.getName().length())); } fi.write(savedFile); fileName = savedFile.getName(); }else{ String msg = "ImgEmptyErr"; context.put("result", msg); return; } } context.put("result", fileName); } catch (Exception e) { Debug.log("上传产品图片发生错误:"+e); String msg = "sysErr"; context.put("result", msg); return; }