struts2实现ckeditor中的文件上传
页面很简单:<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib prefix="s" uri="/struts-tags"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <base href="<%=basePath%>"> <title>editor</title><script type="text/javascript" src="resource/ckeditor/ckeditor.js"></script></head> <body> <textarea id="test"></textarea> <script> CKEDITOR.replace("test",{ filebrowserUploadUrl : 'ckeditor/ckupload.action' }); </script></body></html>
因为ckeditor默认上传文件的字段为upload
所在Action中的字段应为:
upload,uploadContentType,uploadFileName
为什么是这三个字段,大家应该都清楚的吧。
package com.zenntou.test.action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.io.PrintWriter;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionSupport;public class CkeditorUploadAction extends ActionSupport {private String uploadContentType;private String uploadFileName;private String CKEditorFuncNum;private String CKEditor;private String langCode;private File upload;/** * @return the upload */public File getUpload() {return upload;}/** * @param upload the upload to set */public void setUpload(File upload) {this.upload = upload;}/** * @return the cKEditorFuncNum */public String getCKEditorFuncNum() {return CKEditorFuncNum;}/** * @param cKEditorFuncNum the cKEditorFuncNum to set */public void setCKEditorFuncNum(String cKEditorFuncNum) {CKEditorFuncNum = cKEditorFuncNum;}/** * @return the cKEditor */public String getCKEditor() {return CKEditor;}/** * @param cKEditor the cKEditor to set */public void setCKEditor(String cKEditor) {CKEditor = cKEditor;}/** * @return the langCode */public String getLangCode() {return langCode;}/** * @param langCode the langCode to set */public void setLangCode(String langCode) {this.langCode = langCode;}/** * @return the uploadContentType */public String getUploadContentType() {return uploadContentType;}/** * @param uploadContentType the uploadContentType to set */public void setUploadContentType(String uploadContentType) {this.uploadContentType = uploadContentType;}/** * @return the uploadFileName */public String getUploadFileName() {return uploadFileName;}/** * @param uploadFileName the uploadFileName to set */public void setUploadFileName(String uploadFileName) {this.uploadFileName = uploadFileName;}@Overridepublic String execute() throws Exception {String strPath = ServletActionContext.getServletContext().getRealPath("/uploads");File path = new File(strPath);if(!path.exists()){path.mkdirs();}//FileImageInputStream is = new FileImageInputStream(this.upload);//FileImageOutputStream os = new FileImageOutputStream(new File(strPath + File.separator + this.uploadFileName));InputStream is = new FileInputStream(this.upload);OutputStream os = new FileOutputStream(new File(strPath + File.separator + this.uploadFileName));try {int len;byte[] buffer = new byte;while ((len=is.read(buffer)) > 0) {os.write(buffer,0,len);}} catch (Exception e) {e.printStackTrace();} finally {if(is!=null){is.close();}if(os!=null){os.close();}}PrintWriter out = ServletActionContext.getResponse().getWriter();//返回给ckeditorout.write("<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction("+this.CKEditorFuncNum+", 'uploads/" + this.uploadFileName+"', '');</script>");return Action.NONE;}}
因为要上传文件,所以要配置一下struts.multipart.saveDir
有两种方法:
在struts.xml配置文件里添加:
<constant name="struts.multipart.saveDir" value="d:\\temp"></constant>
或者
在src根目录添加struts.properties文件(如果还没有的话),
添加struts.multipart.saveDir = d:\\temp 一行内容
虽然比较简单,希望能给大家带来一定的帮助!
页:
[1]