上篇转载的文章不适用与struts2,捣鼓了半天也没出来
后来是参考下面这篇文章弄出来的,供大家参考。
由于struts2的上传是封装好的,所以前一篇博文的例子无法使用。
希望大家注意。
前篇博文地址[url]http://lever0066.iteye.com/blog/1721705[/url]
如果不是struts2还是可以使用的
补充:
下面文章也非原创,转载地址忘了,十分抱歉
在image.js中找到了控制上传的代码.
搜索"Upload"关键字.
会找到页面上上传的功能Element,这里有一个hidden属性,默认为true,改为false就可以在页面上显示出"上传"功能
ps:建议将Link(图片链接),和advanced(高级),都设置为true,只留下第一个info,和upload,之所以只留这两个功能,
1:info中的源地址与Link中的源地址作用相同.
2.原本info是有一个浏览服务器文件的功能的,但因为安全性的考虑,故舍弃,改为上传后手动设置预览图.
3.高级功能作用不实际,并且难操作,故屏蔽.
image.js修改如下:
其中upload功能块,只保留一个文件上传,和一个上传按钮...
config.js内容如下:
文件上传的name为 upload,(目前还不知道怎么改,直接修改image.js中的id,会提示在页面上找不到W)
所以如果用struts2的话,应该在Action中对应为upload..
action代码如下:
struts.xml配置如下:
后来是参考下面这篇文章弄出来的,供大家参考。
由于struts2的上传是封装好的,所以前一篇博文的例子无法使用。
希望大家注意。
前篇博文地址[url]http://lever0066.iteye.com/blog/1721705[/url]
如果不是struts2还是可以使用的
补充:
下面文章也非原创,转载地址忘了,十分抱歉
在image.js中找到了控制上传的代码.
搜索"Upload"关键字.
会找到页面上上传的功能Element,这里有一个hidden属性,默认为true,改为false就可以在页面上显示出"上传"功能
ps:建议将Link(图片链接),和advanced(高级),都设置为true,只留下第一个info,和upload,之所以只留这两个功能,
1:info中的源地址与Link中的源地址作用相同.
2.原本info是有一个浏览服务器文件的功能的,但因为安全性的考虑,故舍弃,改为上传后手动设置预览图.
3.高级功能作用不实际,并且难操作,故屏蔽.
image.js修改如下:
{id:'Upload',hidden:false,filebrowser:'uploadButton',label:b.lang.image.upload,
elements:[{type:'file',id:'upload',label:b.lang.image.btnUpload,style:'height:40px',size:38},
{type:'fileButton',id:'uploadButton',filebrowser:'info:txtUrl',label:b.lang.image.btnUpload,'for':['Upload','upload']}
]
}
其中upload功能块,只保留一个文件上传,和一个上传按钮...
config.js内容如下:
CKEDITOR.editorConfig = function( config )
{
// Define changes to default configuration here. For example:
// config.language = 'fr';
// config.uiColor = '#AADC6E';
config.language = 'zh-cn'; // 配置语言
config.uiColor = '#FFF'; // 背景颜色
config.width = '900px'; // 宽度
config.height = '250px'; // 宽度
config.skin = 'office2003'; // 界面v2, kama, office2003
config.filebrowserUploadUrl="/admin/uploadFile.do";
/*config.toolbar = [
['Source','Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
['NumberedList','BulletedList','-','Outdent','Indent','TextColor'],
['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','Maximize', 'ShowBlocks','-','Undo','Redo'],
['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak', 'Link', 'Unlink'],
['Styles','Format','Font','FontSize','Preview']
];
*/
//配置操作界面上的工具按钮
//增加中文字体
config.font_names = '宋体/宋体;黑体/黑体;仿宋/仿宋_GB2312;楷体/楷体_GB2312;隶书/隶书;幼圆/幼圆;' + config.font_names ;
};
文件上传的name为 upload,(目前还不知道怎么改,直接修改image.js中的id,会提示在页面上找不到W)
所以如果用struts2的话,应该在Action中对应为upload..
action代码如下:
@Controller
@Scope("prototype")
public class UploadFileAction extends BaseAction {
private static final long serialVersionUID = 1L;
@Autowired
private UploadFileService uploadFileService;
private File upload;
private String uploadFileName;
private String dir = "img";
/**
* 上传文件
*/
public void uploadFile(){
String path = getRealPath();
String fileServerPath = uploadFileService.uploadFile(path, dir,upload, uploadFileName);
String CKEditorFuncNum = getRequest().getParameter("CKEditorFuncNum");
String alt_msg = "";
if(StringUtils.isNotBlank(fileServerPath)) {
alt_msg = "上传成功!";
} else {
alt_msg = "上传失败!";
}
getResponse().setContentType("text/html");
getOut().print("<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction("
+ CKEditorFuncNum
+ ", '"
+ fileServerPath
+ "' , '"
+ alt_msg
+ "');</script>");
getOut().flush();
}
public void uploadFailure(){
String CKEditorFuncNum = getRequest().getParameter("CKEditorFuncNum");
String alt_msg = "请上传大小不超过256K文件!";
String fileServerPath = "";
getResponse().setContentType("text/html");
getOut().print("<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction("
+ CKEditorFuncNum
+ ", '"
+ fileServerPath
+ "' , '"
+ alt_msg
+ "'); history.go(-1);</script>");
getOut().flush();
}
//get set方法
}
struts.xml配置如下:
<package name="admin" extends="struts-default" namespace="/admin">
<action name="uploadFile" class="uploadFileAction" method="uploadFile">
<interceptor-ref name="fileUpload">
<param name="maximumSize">256000</param>
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/jpg ,image/x-png, image/pjpeg,
application/x-shockwave-flash, application/zip, video/x-sgi-movie,application/rar
</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="input" type="chain">uploadFailure</result>
</action>
<action name="uploadFailure" class="uploadFileAction" method="uploadFailure">
</action>
<action name="uploadProgress" class="uploadFileAction" method="uploadProgress">
</action>
</package>