SSM框架中的文件上传(八)

一、单文件的上传

  1、页面代码  

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>

  <form action="${add}" method="post" enctype="multipart/form-data">
      <input name="gid" type="hidden" value="${goods.gid }">
      商品名称:<input name="gname" value="${goods.gname }"><br>
      商品价格:<input name="price" value="${goods.price }"><br>
      类型:<select name="type.tid">
      <c:forEach items="${types }" var="t">
          <option
                  <c:if test="${goods.tid==t.tid }">selected</c:if>
                  value="${t.tid }">${t.tname }</option>
      </c:forEach>
  </select><br>
      商品图片:<input type="file" name="pic" ><!-- multiple -->
      <c:if test="${goods!=null&&goods.img!=null }">
          <img src="imgs/${goods.img }" width="80">
      </c:if>
      <br>
      <input name="img" type="hidden" value="${goods.img }">
      <br>
      <button type="submit">${goods==null?'增加':'修改' }</button>
  </form>
  </body>
</html>

  2、控制器代码

 @RequestMapping("add")
    public String add(Goods goods, @RequestParam("pic") MultipartFile file, HttpServletRequest request, HttpSession session){

        String path = request.getSession().getServletContext().getRealPath("/imgs/");
        String oldName = file.getOriginalFilename();
        String ext = oldName.substring(oldName.lastIndexOf("."));
        String newName = UUID.randomUUID().toString()+ext;
        File f =new File(path,newName);
        try {
            file.transferTo(f);
            goods.setImg(newName);
            goodsService.add(goods);
        } catch (IOException e) {
            e.printStackTrace();
        }
        request.setAttribute("types",goodsService.findAllTypes());
        return "add";
    }

  3、spring核心配置文件

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="5000000"/>
        <property name="defaultEncoding" value="utf-8"/>
    </bean>
    <mvc:resources location="/imgs/" mapping="/imgs/**"/>
    <mvc:annotation-driven/>

二、多文件的上传

  1、只需修改控制器代码

   对象改为数组

@RequestMapping("add")
    public String add(Goods goods, @RequestParam("pic") MultipartFile[] files, HttpServletRequest request, HttpSession session){

        for(MultipartFile file:files){
            String path = request.getSession().getServletContext().getRealPath("/imgs/");
            String oldName = file.getOriginalFilename();
            String ext = oldName.substring(oldName.lastIndexOf("."));
            String newName = UUID.randomUUID().toString()+ext;
            File f =new File(path,newName);
            try {
                file.transferTo(f);
                goods.setImg(newName);
                goodsService.add(goods);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        request.setAttribute("types",goodsService.findAllTypes());
        return "add";
    }

猜你喜欢

转载自www.cnblogs.com/newbest/p/9226144.html