SpringMVC知识一
来源:互联网 发布:js dragobject 编辑:程序博客网 时间:2024/06/04 17:59
一、MultipartFile上传文件
1.配置文件
<!-- 配置MultipartResolver 用于文件上传 使用spring的CommosMultipartResolver --> <bean id="multipartResolver"<span style="white-space:pre"></span>class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><span style="white-space:pre"></span><property name="defaultEncoding"><span style="white-space:pre"></span><value>UTF-8</value><span style="white-space:pre"></span></property><span style="white-space:pre"></span><property name="maxUploadSize"><span style="white-space:pre"></span><!-- 上传文件大小限制为31M,31*1024*1024 --><span style="white-space:pre"></span><value>32505856</value><span style="white-space:pre"></span></property><span style="white-space:pre"></span><property name="maxInMemorySize"><span style="white-space:pre"></span><value>4096</value><span style="white-space:pre"></span></property><pre name="code" class="java"><span></span><property name="uploadTempDir"><span></span><value>fileUpload/temp</value><span></span></property>
</bean>defaultEncoding:请求的编码格式,默认为ios-8859-1
maxUploadSize:上传文件的大小,单位为byte
uploadTempDir:上传文件的临时路径、
2.上传表单
记得在form标签中加入enctype="multipart/form-data"表示表单是要处理文件的
3.上传控制类
//通过Spring的autowired注解获取spring默认配置的request @Autowired private HttpServletRequest request; /*** * 上传文件 用@RequestParam注解来指定表单上的file为MultipartFile * * @param file * @return */ @RequestMapping("fileUpload") public String fileUpload(@RequestParam("file") MultipartFile file) { // 判断文件是否为空 if (!file.isEmpty()) { try {<span style="white-space:pre"></span>String fileName = file.getOriginalFilename(); <span style="white-space:pre"></span>//path为String,即用户想指定的保存路径 <span style="white-space:pre"></span>File targetFile = new File(path, fileName); <span style="white-space:pre"></span>if(!targetFile.exists()){ <span style="white-space:pre"></span>targetFile.mkdirs(); <span style="white-space:pre"></span>} <span style="white-space:pre"></span>file.transferTo(targetFile); // 第二种方式:文件保存路径 //String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/" + file.getOriginalFilename(); // 转存文件 //file.transferTo(new File(filePath)); } catch (Exception e) { e.printStackTrace(); } } // 重定向 return "redirect:/list.html"; } /*** * 读取上传文件中得所有文件并返回 * * @return */ @RequestMapping("list") public ModelAndView list() { String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/"; ModelAndView mav = new ModelAndView("list"); File uploadDest = new File(filePath); String[] fileNames = uploadDest.list(); for (int i = 0; i < fileNames.length; i++) { //打印出文件名 System.out.println(fileNames[i]); } return mav; }
4.常用方法
String getContenType():获取文件MIME类型
InputStream getInputStream():获取文件流
String getName():获取表单中文件组件的名字
String getOriginalFileName():获取上传文件的原名
long getSize():获取文件字节大小,单位byte
boolean isEmpty():是否为空
void transferTo(File dest):保存到一个目标中
二、@RequestMapping
参考:http://blog.csdn.net/kobejayandy/article/details/12690041
1.简介
RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
2.有六个属性:
1)value、method
value:指定请求的实际地址,指定的地址可以是URI Template模式
method:指定请求的method类型,GET、POST、PUT、DELETE等
value的uri值可为以下三类:
A)普通的具体值;
@RequestMapping(value="/add",method=RequestMethod.POST)@ResponseBodypublic Map add(SysMenu obj){Map map=new HashMap();return map;}
B)含有某变量的一类值;
- @RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
- public String findOwner(@PathVariable String ownerId, Model model) {
- Owner owner = ownerService.findOwner(ownerId);
- model.addAttribute("owner", owner);
- return "displayOwner";
- }
C)含有正则表达式的一类值;
- @RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}")
- public void handle(@PathVariable String version, @PathVariable String extension) {
- // ...
- }
- }
2)consumes、produces、params、headers
0 0
- SpringMVC知识一
- Springmvc知识一------配置文件&URL&注解组件
- SpringMVC 知识
- SpringMvc知识
- SpringMVC 知识
- SpringMVC知识二
- SpringMVC知识1:流程
- SpringMVC知识2:配置
- SpringMVC知识梳理
- 框架知识(1)--SpringMVC
- SpringMVC知识学习
- springmvc知识整体概览
- SpringMvc知识 (2)
- springmvc 复习知识图
- SpringMvc框架知识简介
- SpringMVC知识汇总
- SpringMvc知识汇总
- SpringMvc (一)
- Java中使用PrepateStatement并且like模糊查询
- Spring MVC的基本概念
- 解决新建maven工程出现Could not resolve archetype
- 数据库(SQL)之trigger(触发器)的使用以及视图(view)的基本实现
- 【C++】基础知识—引用和指针引用
- SpringMVC知识一
- web前台工作笔记1
- OC02_方法和继承
- Codevs_P1425 最长公共子串(KMP)
- 宽度优先搜索
- android(20)(从网络抓取html显示在本地,把流转换成String,请求网络,简单的消息处理机制,编码问题)
- oracle 动态语句实现复杂SQL的存储过程分布查询
- python基本 tuple
- Pycharm 安装 , Git 插件安装,django环境配置