文件上传(springMVC+ckeditor)
1、首先添加springMVC文件上传的jar
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
)
<!-- 上传文件拦截,设置最大上传文件大小 10M=10*1024*1024(B)=10485760 bytes -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760" />
</bean>
3、Controller代码
/*
* 图片命名格式
*/
private static final String DEFAULT_SUB_FOLDER_FORMAT_AUTO = "yyyyMMddHHmmss";
/*
* 放置上传图片的文件夹!
*/
private static final String UPLOAD_PATH="/upload/img/";
@RequestMapping("/uploadImg")
public void uplodaImg(@RequestParam("upload")MultipartFile file,HttpServletRequest request, HttpServletResponse response, @RequestParam("CKEditorFuncNum")String CKEditorFuncNum)
throws IllegalStateException, IOException {
PrintWriter out =response.getWriter();
String fileName=file.getOriginalFilename();
String uploadContentType =file.getContentType();
String expandedName ="";
if (uploadContentType.equals("image/pjpeg")
|| uploadContentType.equals("image/jpeg")) {
// IE6上传jpg图片的headimageContentType是image/pjpeg,而IE9以及火狐上传的jpg图片是image/jpeg
expandedName = ".jpg";
} else if (uploadContentType.equals("image/png")
|| uploadContentType.equals("image/x-png")) {
// IE6上传的png图片的headimageContentType是"image/x-png"
expandedName = ".png";
} else if (uploadContentType.equals("image/gif")) {
expandedName = ".gif";
} else if (uploadContentType.equals("image/bmp")) {
expandedName = ".bmp";
} else {
out.println("<script type=\"text/javascript\">");
out.println("window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum
+ ",''," + "'文件格式不正确(必须为.jpg/.gif/.bmp/.png文件)');");
out.println("</script>");
return ;
}
if (file.getSize()> 600 * 1024) {
out.println("<script type=\"text/javascript\">");
out.println("window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum
+ ",''," + "'文件大小不得大于600k');");
out.println("</script>");
return ;
}
DateFormat df = new SimpleDateFormat(DEFAULT_SUB_FOLDER_FORMAT_AUTO);
fileName = df.format(new Date())+expandedName;
/** 构建上传图片的保存目录* */
String saveDir = UPLOAD_PATH+ fileName; //相当于("/upload/img/"+fileName)
/** 得到文件保存目录的真实路径* */
/**一定要注意这里,这里的路径就是上传图片所在的项目根路径,在编译后的文件夹里面,前端可以根据wepapp下的路径直接取即可。
*不确定的话,就打印出这句话,然后看看下面的配置的前端是怎么取到的
*/
String imgRealPathDir = request.getSession().getServletContext().getRealPath(saveDir);
File targetFile = new File(imgRealPathDir);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
file.transferTo(targetFile);//注意,项目经常会在这里出现错误,错误的原因一般都是配的上传路径或者前端获取的路径不对,一定要注意这里
// 返回"图像"选项卡并显示图片 request.getContextPath()为web项目名,只适合jsp页面使用,不适用于后台Java代码
out.println("<script type=\"text/javascript\">");
out.println("window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum
+ ",'" + request.getContextPath()+"/upload/img/" + fileName + "','')");
out.println("</script>");
return ;
}
文件上传(springMVC+ckeditor)的更多相关文章
- 文件上传<springmvc>
使用commons-fileupload-1.3.1.jar和commons-io-2.4.jar web.xml <?xml version="1.0" encoding= ...
- SpringMvc入门五----文件上传
知识点: SpringMvc单文件上传 SpringMvc多文件上传 这里我直接演示多文件上传,单文件的上传就不说了,不过代码都是现成的. 效果预览: DEMO图: 添加文件上传j ...
- SpringMVC:学习笔记(8)——文件上传
SpringMVC--文件上传 说明: 文件上传的途径 文件上传主要有两种方式: 1.使用Apache Commons FileUpload元件. 2.利用Servlet3.0及其更高版本的内置支持. ...
- SpringMVC国际化与文件上传
点击阅读上一章 其实SpringMVC中的页面国际化与上一章的验证国际化基本一致. 1.对页面进行国际化 1)首先我们对Spring配置文件中添加国际化bean配置 <!-- 注册国际化信息,必 ...
- SSM框架之SpringMVC(5)文件上传
SpringMVC(5)文件上传 1.实现文件上传的前期准备 1.1.文件上传的必要前提 A form 表单的 enctype 取值必须是: multipart/form-data(默认值是:appl ...
- springmvc 文件上传实现(不是服务器的)
1.spring使用了apache-commons下的上传组件,因此,我们需要引用2个jar包 1)apache-commons-fileupload.jar 2 ) apache-commons-i ...
- SpringMVC文件上传和下载
上传与下载 1文件上传 1.1加入jar包 文件上传需要依赖的jar包 1.2配置部件解析器 解析二进制流数据. <?xml version="1.0" encoding=& ...
- springMVC文件上传
参考的地址:http://www.tuicool.com/articles/nMVjaiF 1.需要使用的jar. commons-fileupload.jar与commons-io-1.4.jar二 ...
- springMvc 使用ajax上传文件,返回获取的文件数据 附Struts2文件上传
总结一下 springMvc使用ajax文件上传 首先说明一下,以下代码所解决的问题 :前端通过input file 标签获取文件,通过ajax与后端交互,后端获取文件,读取excel文件内容,返回e ...
随机推荐
- Java性能优化权威指南-读书笔记(五)-JVM性能调优-吞吐量
吞吐量是指,应用程序的TPS: 每秒多少次事务,QPS: 每秒多少次查询等性能指标. 吞吐量调优就是减少垃圾收集器消耗的CPU周期数,从而将更多的CPU周期用于执行应用程序. CMS吞吐调优 CMS包 ...
- .tar.bz2文件解压命令
从网络上下载到的源码包, 最常见的是 .tar.gz 包, 还有一部分是 .tar.bz2包 要解压很简单 : .tar.gz 格式解压为 tar -zxvf xx. ...
- CityGML文件格式
1 LOD3中,wall是由cuboid组成的,一个墙面包括8个面,分为wall-1, wall-2...wall-8,door也是,因此他们都是multisurface (一般由8个面片组成). 在 ...
- zabbix 修改管理员用户密码
mysql> use zabbix mysql> desc users; +----------------+---------------------+------+-----+---- ...
- php 获取当前时间
<?php echo $showtime=date("Y-m-d H:i:s");?>
- Kafka学习笔记(一):概念介绍
Kafka是一个开源的,分布式的,高吞吐量的消息系统.随着Kafka的版本迭代,日趋成熟.大家对它的使用也逐步从日志系统衍生到其他关键业务领域.特别是其超高吞吐量的特性,在互联网领域,使用越来越广泛, ...
- .net学习笔记----Asp.net的生命周期之一应用程序生命周期
Http请求刚刚到达服务器的时候 当服务器接收到一个 Http请求的时候,IIS (Internet Information Services,互联网信息服务)首先需要决定如何去处理这个请求. 什么是 ...
- Visual Studio 2012 使用免费的Team Foundation Service(转载)
转载地址:http://www.cnblogs.com/chsword/archive/2012/12/14/visualstudio2012_tfs.html VS2012提供了在线的TFS服务,免 ...
- php动态安装mongo扩展
首先下载mongo扩展包 http://pecl.php.net/package/mongo 开始安装把 wget http://pecl.php.net/get/mongo-1.5.8.tgz t ...
- vijos 1038 括号+路径 ***
链接:点我 就是自己写不出来 #include <cstdio> #include <climits> #include <memory.h> using name ...