js里开启上传功能。
<script type="text/javascript">
$(function() {
CKEDITOR.replace('news.content',{filebrowserUploadUrl : 'uploadfile?type=file',filebrowserImageUploadUrl : 'uploadfile?type=image',filebrowserFlashUploadUrl : 'uploadfile?type=flash' });
})
</script>
Struts2.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<constant name="struts.multipart.saveDir" value="/tmp"></constant>
<constant name="struts.multipart.maxSize" value="524288000" />
<package name="news" extends="struts-default">
<action name="uploadfile" class="com.year.action.UploadFileAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
image/png,image/gif,image/jpeg,application/x-MS-bmp,image/gif,audio/midi,audio/x-mpeg,video/3gpp,video/x-msvideo,video/mp4,application/x-shockwave,application/x-rar-compressed,application/zip
</param>
<param name="maximumSize">524288000</param>
</interceptor-ref>
<param name="savePath">/upload</param>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>
UploadFileAction.java
package com.year.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class UploadFileAction extends ActionSupport {
/**
*
*/
private static final int BUFFER_SIZE = 16 * 1024;
private static final long serialVersionUID = 7272172660237286783L;
private File upload;
private String uploadContentType;
private String uploadFileName;
private String CKEditorFuncNum;
private String CKEditor;
private String langCode;
private String savePath;
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getCKEditorFuncNum() {
return CKEditorFuncNum;
}
public void setCKEditorFuncNum(String cKEditorFuncNum) {
CKEditorFuncNum = cKEditorFuncNum;
}
public String getCKEditor() {
return CKEditor;
}
public void setCKEditor(String cKEditor) {
CKEditor = cKEditor;
}
public String getLangCode() {
return langCode;
}
public void setLangCode(String langCode) {
this.langCode = langCode;
}
private String getWebPath() {
HttpServletRequest request = ServletActionContext.getRequest();
String path = request.getContextPath();
String basePath = request.getScheme() + "//" + request.getServerName()
+ ":" + request.getServerPort() + path + "/";
return basePath;
}
private static void copy(File src, File dist) {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dist),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private String getNameWithoutExtension(String fileName) {
return fileName.substring(0, fileName.lastIndexOf("."));
}
private String getExtension(String fileName) {
return fileName.substring(fileName.lastIndexOf(".") + 1);
}
@Override
public String execute() throws Exception {
SimpleDateFormat fileFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
SimpleDateFormat dirFormat = new SimpleDateFormat("yyyyMM");
Date date = new Date();
String fileName = fileFormat.format(date) + "."
+ getExtension(uploadFileName);
System.out.println(fileName);
if (null == type) {
type = "file";
}
String dirPath = getSavePath() + "/" + type + "/"
+ dirFormat.format(date);
String webPath = ServletActionContext.getRequest().getRealPath(dirPath);
File dirTest = new File(webPath);
if (!dirTest.exists()) {
dirTest.mkdirs();
}
String filePath = webPath + File.separator + fileName;
File file = new File(filePath);
copy(upload, file);
String path = ServletActionContext.getRequest().getContextPath();
String url = path + dirPath + "/" + fileName;
PrintWriter out = ServletActionContext.getResponse().getWriter();
/*
* 关键:url为:/工程名/文件位置
* 例: /news/upload/image/201305/20130509151420333.jpg
*/
out.write("<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction("
+ this.CKEditorFuncNum + ",'" + url + "','');</script>");
return Action.NONE;
}
}