最近写的springMVC上传文件例子,有兴趣的同学可以参考一下:
Java类:
package com.spring.controller;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UploadImageController {
@RequestMapping(value="upload/file",method = {RequestMethod.GET,RequestMethod.POST})
public ModelAndView uploadImages(@RequestParam(value = "file", required = false) MultipartFile file,
HttpServletRequest request, ModelMap model) {
ModelAndView mav =new ModelAndView();
String path = request.getSession().getServletContext().getRealPath("images/uploadImages");
String fileName = file.getOriginalFilename();
// String fileName = new Date().getTime()+".jpg";
System.out.println(path);
File targetFile = new File(path, fileName);
if(!targetFile.exists()){
targetFile.mkdirs();
}
//����
try {
file.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
//model.addAttribute("fileUrl", request.getContextPath()+"/uploadImages/"+fileName);
mav.addObject("fileUrl", request.getContextPath()+"/images/uploadImages/"+fileName);
mav.setViewName("uploadSuccess");
return mav;
}
}
jsp上传页面:
<body>
<form action="upload/file" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="上传" />
</form>
<img alt="" src="images/10.jpg">
<img alt="" src="images/uploadImages/11.gif">
</body>
jsp上传成功页面:
<body>
上传成功<br>
<img src="${fileUrl}" alt=""/>
<img src="${fileUrl}" alt=""/>
<a href="${fileUrl}">查看</a>
<a href="${fileUrl}">查看</a>
</body>
还要在spring配置文件中加入:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件大小的参数 -->
<property name="maxUploadSize" value="1000000"/>
</bean>
SpringMVC访问静态资源的配置:css,js,img
注意web.xml文件:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern><!-- 拦截所有请求 ,会导致img,js,css等静态文件显示不了 ,要访问到静态文件要在配置文件中做处理-->
<!-- <url-pattern>*。do</url-pattern> --> <!-- 只拦截。do的请求,静态文件可以访问 -->
</servlet-mapping>
注意applicationContext.xml配置文件加入如下代码;
<mvc:annotation-driven />
<!-- 取消对images和js两个文件夹的拦截,可以访问静态文件的文件夹 -->
<mvc:resources location="/images/" mapping="/images/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:default-servlet-handler />
<!-- 自动扫描bean,把作了注解的类转换为bean -->
<context:component-scan base-package="com" />
运行效果:
运行效果2:
分享到:
相关推荐
简化springMvc设置,实现上传,下载功能。
本例子提供了2中 springMVC 上传的例子,通过 ajax 提交的方式,通过 Form 提交的方式,同时获取上传后文件名称,支持多个文件上传。
springmvc+ajax上传例子
springMVC 注解模式 完整事例 小例子 完整的小例子 放在eclipse或者myeclipse就直接可以运行的 包含简单的增删改查 上传文件 导出excle等小功能 登录验证等 简单实用 初学者首选
资源包括了springMvc常见的例子并附有源代码 上传文件 下载文件 处理ajax请求 返回json数据 踢掉在线用户 全注解形式
简单的springmvc框架 主要是用注解,实现用户信息保存,文件上传功能,适合初级学习使用。
整理了一份springMVC上传文件的文档,方便大家阅读下载
springMVC文件上传的一个例子,保存以后备用,亲测可用。
springMVC-mybatis-maven 框架实现附件上传, 文档提供以图片上传为例子, 存储为磁盘存储,简单的上传功能, 需要jdk 1.7 以上 , mysql数据库,付简单简表语句
springmvc 例子; 使用框架:spring(包含hibernate),jquery。 ajax,文件上传,拦截器.....
该文档主要使用springmvc和struts2实现了图片上传和下载功能的例子
spring mvc开发的例子,里面还有个spring上传文件。
JQuery uploadify 实现文件批量上传完整例子,所有js,和uploadify插件js 都放在这个例子中
版本管理,服务根路径工具类,文件上传工具类 1.0.10 集成ueditor在线编辑器 1.0.11 地址联动 1.0.12 Excel工具类 Word工具类 Java NIO实现socket工具类 分布式session jdk升级到1.7 嵌入式redis服务(只支持linux) ...
最近因为使用到springmvc和mybatis做项目,所以自己用myeclipse和mysql搭建了个小demo上传以共享。这个demo整合springmvc和mybatis,使用注解方式而且配置文件很完全,附带了mysql的数据库脚本。供有需要学习的人做个...
Spring MVC 模式例子 包括上传下载文件 整理好的
本篇文章主要介绍了spring mvc(注解)上传文件的简单例子,具有一定的参考价值,有兴趣的可以了解一下。
SpringMVC-Ueditor项目是一个Spring MVC与百度文本编辑器ueditor集成的例子(ps:部分代码来自网络),本例相比官方的jsp例子实现上更为优雅,并且将文件上传和下载的接口放在Controller中去实现更方便维护,包括...
版本管理,服务根路径工具类,文件上传工具类 1.0.10 集成ueditor在线编辑器 1.0.11 地址联动 1.0.12 Excel工具类 Word工具类 Java NIO实现socket工具类 分布式session jdk升级到1.7 嵌入式redis服务(只支持linux) ...
版本管理,服务根路径工具类,文件上传工具类 1.0.10 集成ueditor在线编辑器 1.0.11 地址联动 1.0.12 Excel工具类 Word工具类 Java NIO实现socket工具类 分布式session jdk升级到1.7 嵌入式redis服务(只支持linux) ...