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)含有某变量的一类值;

  1. @RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)  
  2. public String findOwner(@PathVariable String ownerId, Model model) {  
  3.   Owner owner = ownerService.findOwner(ownerId);    
  4.   model.addAttribute("owner", owner);    
  5.   return "displayOwner";   
  6. }  

C)含有正则表达式的一类值;

  1. @RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}")  
  2.   public void handle(@PathVariable String version, @PathVariable String extension) {      
  3.     // ...  
  4.   }  

2)consumes、produces、params、headers



0 0