第一步:需要导入commons-io-1.4.jar和commons-fileupload-1.3.1.jar
第二步:导入文件ckeditor和uploadify文件,已经配好的文件下载地址:http://pan.baidu.com/s/1ge9ZWDt(里面有ckeditor和uploadify两个文件夹,直接解压放入项目根目录下,如果下载不了可以联系我)
第三步:新建页面导入并写入下面内容
<link href="${base}/thirdparty/uploadify/css/uploadify.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript">
document.write('<script type="text/javascript" src="${base}/thirdparty/uploadify/jquery.uploadify.js?v=' + (1000 + Math.ceil(Math.random() * 10000)) + '"><\/script>')
</script>
下面是我的上传图片js代码
<script type="text/javascript">
$(function() {
$("#file_upload").uploadFile({
'storeName': 'picture',
'swf': '${base}/thirdparty/uploadify/uploadify.swf',
'uploadLimit': 6
});
$("#submitForm").validate();
});
</script>
页面中使用富文本编辑器
<textarea rows="7" cols="45" id="content" name="content" >${(bean.content)!}</textarea>
<script type="text/javascript">
$(function() {
<!--页面中新建一个ckeditor,注意上传图片的action地址-->
CKEDITOR.replace( 'content' ,
{disallowedContent: 'img{width,height};',
filebrowserUploadUrl : '${backBase}/news/upload.do',
filebrowserImageUploadUrl : '${backBase}/news/upload.do?Type=Image',
filebrowserFlashUploadUrl : '${backBase}/news/upload.do?Type=Flash',
filebrowserMediaUploadURL : '${backBase}/news/upload.do?Type=Media'
} ); });
</script>
第四步:springmvc中上传文件到其他服务器(注意springmvc上传文件的其他配置)
@RequestMapping("/upload.do")
public void upload(HttpServletRequest request,HttpServletResponse response){
MultipartHttpServletRequest mr = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = mr.getFileMap();//不知道上传的是什么文件的时候使用这种方式
Set<Entry<String, MultipartFile>> entrySet = fileMap.entrySet();
for(Entry<String, MultipartFile> entry : entrySet){
//上传上来的图片
MultipartFile pic = entry.getValue();
String originalFilename = pic.getOriginalFilename();
String realName = request.getSession().getServletContext().getRealPath(pic.getOriginalFilename());
int i = realName.indexOf(originalFilename);
String dir = realName.substring(0, i);
File file = new File(dir,originalFilename);//临时文件
if(!file.exists()){
file.mkdirs();
try {
pic.transferTo(file);
String actionUrl = PropertyUtils.getPropertyValue(new File(realPathResolver.get(Constants.GWOLD_CONFIG)), NEWS_UPLOAD);
String imgUrl = UploadUtils.SubmitPost(actionUrl, originalFilename, "", dir);//这里是调用其他公司接口,返回图片地址的方法
com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
String jsonString = jsonObject.toJSONString(imgUrl);
ResponseUtils.renderText(response, jsonString);//返回地址
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
file.delete();//删除临时文件
}
}
}
}