日期:2014-05-16 浏览次数:20453 次
//这后面加的参数分别是上传文件,图片和flash CKEDITOR.replace( 'context',{filebrowserUploadUrl : 'ckupload.do?type=file', filebrowserImageUploadUrl : 'ckupload.do?type=image', filebrowserFlashUploadUrl : 'ckupload.do?type=flash' });
package info.hellolihui.action; import java.io.File; import java.io.PrintWriter; import java.util.Date; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; public class CKUploadAction extends DispatchAction { //允许上传的文件后缀名 private final String[] exts = new String[]{"gif","png","jpeg","jpg","bmp"}; @Override public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("上传"); ServletContext servletContext = request.getSession().getServletContext(); // 设定上传文件路径 String path = servletContext.getRealPath("/upload") + "/"; String typeStr = request.getParameter("type"); //如果type为空则默认为上传文件 if (typeStr == null) { typeStr = "file"; } path += typeStr; // 判断文件夹是否存在,不存在则创建,此时的path还未加上文件名,为http://127.0.0.1:8080/xx/upload的格式 File dirTest = new File(path); if (!dirTest.exists()) { dirTest.mkdirs(); } System.out.println(path); //使用Apache Common组件中的fileupload进行文件上传 FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); // 文件名和文件真实路径 String newName = ""; String fileUrl = ""; String fileName = ""; //只有文件名 try { List items = upload.parseRequest(request); Map fields = new HashMap(); Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (item.isFormField()) fields.put(item.getFieldName(), item.getString()); else fields.put(item.getFieldName(), item); } // CEKditor中file域的name值是upload FileItem uplFile = (FileItem) fields.get("upload"); // 获取文件名并做处理 String fileNameLong = uplFile.getName(); fileNameLong = fileNameLo