ckeditor to increase in the jsp image upload function (2)
Advertisements
Before the reference http://blog.163.com/ytrtfhj @ 126/blog/static/890531092010226023136 / and
http://topic.csdn.net/u/20100127/11/d2254308-db90-4b1f-adca-b36bd8956264.html
Are trying to open ckeditor default image upload, but failed to successfully uploaded into the background, this may be due to their callback function call error.
Thank http://sarin.javaeye.com/blog/599499 the author, his most simple and beautiful way, but also solve the fundamental problem.
Summarized under the upload feature is very simple in fact open, front only the following code:
// This is followed by the parameters are to upload files, pictures, and flash
CKEDITOR.replace( 'context',{filebrowserUploadUrl : 'ckupload.do?type=file',
filebrowserImageUploadUrl : 'ckupload.do?type=image',
filebrowserFlashUploadUrl : 'ckupload.do?type=flash'
});
Background components can be apache commons io package in to upload fileupload and operation simple to use struts1.2 write back, the bag comes with the lack of attention struts1.2 ways to add an attachment package.
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 {
// Allows you to upload the file extensions
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(" Upload ");
ServletContext servletContext = request.getSession().getServletContext();
// Upload file path set
String path = servletContext.getRealPath("/upload") + "/";
String typeStr = request.getParameter("type");
// If the type is empty, the default file to upload
if (typeStr == null) {
typeStr = "file";
}
path += typeStr;
// Determine whether the folder exists, does not exist, create , At this point the path with the file name has not been , As http://127.0.0.1:8080/xx/upload Format
File dirTest = new File(path);
if (!dirTest.exists()) {
dirTest.mkdirs();
}
System.out.println(path);
// Use Apache Common component fileupload Upload files
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
// True path of the file name and file
String newName = "";
String fileUrl = "";
String fileName = ""; // Only the file name
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 In the file domain name Value is upload
FileItem uplFile = (FileItem) fields.get("upload");
// Get the file name and do the deal
String fileNameLong = uplFile.getName();
fileNameLong = fileNameLong.replace('\\', '/');
String[] pathParts = fileNameLong.split("/");
fileName = pathParts[pathParts.length - 1];
// Get the file extension
String ext = getExtension(fileName);
// Set upload file names to the time named
fileName = new Date().getTime() + "." + ext;
// Get the file name ( No extension )
String nameWithoutExt = getNameWithoutExtension(fileName);
File pathToSave = new File(path, fileName);
fileUrl = path + "/" + fileName;
if (extIsAllowed(ext)) {
int counter = 1;
while (pathToSave.exists()) {
newName = nameWithoutExt + "_" + counter + "." + ext;
fileUrl = path + "/" + newName;
pathToSave = new File(path, newName);
counter++;
}
uplFile.write(pathToSave);
} else {
System.out.println(" Invalid file type : " + ext);
}
}catch (Exception ex) {
ex.printStackTrace();
}
PrintWriter out = response.getWriter();
// CKEditorFuncNum Is displayed when the location of the callback, this parameter must be
// Does not exist d:\ The address, to url
String url = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/upload/" + typeStr + "/" + fileName;
String callback = request.getParameter("CKEditorFuncNum");
out.println("<script type=\"text/javascript\">");
out.println("window.parent.CKEDITOR.tools.callFunction(" + callback
+ ",'" + url + "',''" + ")");
out.println("</script>");
out.flush();
out.close();
return null;
}
/**
* Way to get the file name
*/
private static String getNameWithoutExtension(String fileName) {
return fileName.substring(0, fileName.lastIndexOf("."));
}
/**
* Method for extension
*/
private String getExtension(String fileName) {
return fileName.substring(fileName.lastIndexOf(".") + 1);
}
/**
* To determine whether to allow the method of extension
*/
private boolean extIsAllowed(String ext) {
ext = ext.toLowerCase();
List extList = Arrays.asList(this.exts);
return extList.contains(ext)?true:false;
}
}
Related Posts of ckeditor to increase in the jsp image upload function (2)
-
hibernate generic generic DAO
package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *
-
Servlet brief introduction
Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...
-
First Hibernate Example
Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co
-
Struts2 + hibernate + spring problem user log in
dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q
-
The level Hibernate cache
Hibernate cache level: (1) a cache is very short and the session life cycle consistent, also known as session-level cache-level cache or transaction-level cache (2) Ways of Supporting level cache: get (); load (); iterator (); only entity object cach ...












