controller:
@RequestMapping("/bannerAd/create.do")
public String create(HttpServletRequest request, @RequestParam("bannerFile") MultipartFile bannerFile
) throws Exception {
if (!isLoggedIn(request)){
return "redirect:/logout.do";
}
String fileName = "";
if (bannerFile != null && !bannerFile.isEmpty()) {
String uploadPath = request.getSession().getServletContext().getRealPath("/upload/banners");
fileName = UploadUtils.uploadFile(bannerFile, uploadPath);
}
BannerAdDTO bannerAd = new BannerAdDTO();
bannerAd.setFileUrl(fileName);
..........................
bannerAdService.createBannerAd(bannerAd);
return "redirect:/bannerAd/list.do";
}
Jsp:
<form id="formBannerAd" method="post" enctype="multipart/form-data" action="<c:url value='/bannerAd/create.do'/>">
<dl>
<dt>
<label for="bannerFile">Banner File : </label>
</dt>
<dd>
<input id="bannerFile" name="bannerFile
" type="file"/>
</dd>
..............................
</dl>
<div class="buttons">
<input type="button" value="Create" onclick="save();"/>
<input type="button" value="Cancel" onclick="#"/>
</div>
</form>
upload util:
public static String uploadFile(MultipartFile cFile, String uploadPath) throws Exception {
Assert.notNull(cFile, "cFile must not be null");
Assert.notNull(uploadPath, "moduleName must not be null");
String result = "";
if (cFile != null) {
if (!fileExists(uploadPath)) {
makePath(uploadPath);
}
String fileName = generateFileName(cFile.getOriginalFilename());
String fullName = uploadPath + File.separator + fileName;
DataOutputStream out = new DataOutputStream(new FileOutputStream(fullName));
InputStream is = null;
try {
is = cFile.getInputStream();
byte[] buffer = cFile.getBytes();
while (is.read(buffer) > 0) {
out.write(buffer);
}
result = fileName;
} catch (IOException e) {
e.printStackTrace();
return "";
} finally {
if (null != is) {
is.close();
}
if (null != out) {
out.close();
}
}
}
return result;
}
配置文件:action-servlet.xml
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding">
<value>utf-8</value>
</property>
</bean>
分享到:
相关推荐
13) spring-mock.jar需spring-core.jar,spring-beans.jar,spring-dao.jar,spring-context.jar,spring-jdbc.jarspring2.0和spring2.5及以上版本的jar包区别Spring 2.5的Jar打包 在Spring 2.5中, Spring Web MVC...
struts2.0+spring2.5+hibernate3.0整合框架,下载导入到eclipse下即可。。启动tomcat前先修改jdbc.properties文件。由于lib文件较大,所以请自行导入相关lib包。
NULL 博文链接:https://hanqunfeng.iteye.com/blog/606089
ssh2(struts2+spring2.5+hibernate3.3+ajax)带进度条文件上传(封装成标签)
struts2.1 + hibernate3.2 + spring 2.5 实现blob数据上传、下载
由于上传限制,lib包不能上传,大家可以下载spring2.5版本的支持库(下最大的那个zip包),全部加进去就行了,还有mysql的连接包,项目很简单,直接导入eclipse,就可以使用,包内有mysql的sql文件,框架已经搭好,...
本系统在exclipse下开发.用ext2.2 +spring2.5+ struts3开发的综合实例.实现增删改查功能.由于上传文件大小的限制,jar文件不能上传.请根据classpath的要求自行添加.
由于所用到的包比较多,文件上传的最大值被限制在20M,所以只好分成两个资源上传。 struts2.1.6 +spring2.5 +hibernate3.2 整合开发的 jar包 _ 01部分 struts2.1.6 +spring2.5 +hibernate3.2 整合开发的 jar包 _ 02...
NULL 博文链接:https://yale.iteye.com/blog/464132
hibernate3.2+spring2.5+flex3.0整合框架,自己搭建的,可供学习之用,该框架的搭建环境是在myeclipse6.5下,还有就是因为上传文件的大不限制,只好分两部分下,请见谅!
hibernate3.2+spring2.5+flex3.0整合框架,自己搭建的,可供学习之用,该框架的搭建环境是在myeclipse6.5下,还有就是因为上传文件的大不限制,只好分两部分下,请见谅!
spring注解(去掉了在XML文件中bean的配置) jpa注解(去掉了*.hbm.xml)文件的配置 该项目完全可以运行,包含了所有的JAR包,数据库默认使用ORACLE,MYSQL(需要改下jdbc.property配置文件,修改很方便) 该包绝对适用,...
由于所用到的包比较多,文件上传的最大值被限制在20M,所以只好分成两个资源上传。 struts2.1.6 +spring2.5 +hibernate3.2 整合开发的 jar包 _ 01部分 struts2.1.6 +spring2.5 +hibernate3.2 整合开发的 jar包 _ 02...
这是一个struts2+spring2.5+hibernate3.1整合登陆示例,适合于初学struts2.0的学者,数据库用的是mysql,具体的数据库文件在压缩包里,由于上传文件大小有限制,所以我把里面的lib包给删除了,读者可以自己加上去,...
一个简单struts2+spring2.5+hibernate3.3+struts-menu可用框架例子,有简单的文件上传和下载功能,注意在hibernate.cfg.xml中配置了数据库,可以注释掉,否则启动会报异常。。,等陆页...
包含有完整的jar包和源代码,这是专门为我们实验室定制开发的,包含了架构基于s2sh技术网站的参考实现(包括了全部基础部分:如分页,缓存,文件上传,连接池等等)希望对初学JavaEE WEB开的人有所帮助。...
ssh(struts2.0 spring2.5 hibernate3.2)整合作的一个用户登录验证以及用户数据生成excel mytest.users.sql 这个是整个例子的数据库脚本 直接在myeclipse里面导入sshShop.zip 就行了,由于csdn只能上传15MB的的文件,...
本程序采用struts2.0+spring2.5+hiberhate3架构开发。 主要展示技术包括:三层架构 freemarker模版 面向切面编程 邮件发送 定时任务 缓存管理 运行环境: 1 TOMCAT 5.5及以上版本. 2 JDK5.0及以上版本. 3 ...
支持上传图片和文件,文件作为附件可以下载,图片可直接在JSP页面中显示 支持回帖,并在主楼中显示 答辩用PPT 选做部分: 帖子搜索 用户查找 管理员对帖子加精、置顶 根据浏览量,自动在首页生成热门帖子列表...