zoukankan      html  css  js  c++  java
  • springMVC文件上传

    单个文件上传

    1.导入需要的jar包(fileupload,commons-io)

     

    2.网页表单提交格式post,编码要enctype="multipart/form-data",上传文件要有name

    <%@ page language="java" contentType="text/html; charset=utf-8"

        pageEncoding="utf-8"%>

    <!DOCTYPE html>

    <html>

    <head>

    <meta charset="utf-8">

    <title>register</title>

    </head>

    <body>

    <form action="/SpringMVC/user/register" method="post" enctype="multipart/form-data">

           name:<input type="text" name="name"/><br/>

           password:<input type="password" name="password"/><br/>

           phone:<input type="text" name="phone"/><br/>

           age:<input type="text" name="age"/><br/>

           photo:<input type="file" name="photo"/><br/>

           <input type="submit" value="提交"/><br/>

    </form>

    </body>

    </html>

    3.在Controller里处理

           @RequestMapping("register")

           public ModelAndView register(User user,MultipartFile photo,HttpServletRequest request) {

                  ModelAndView mv=new ModelAndView("register");

                  //1.通过request获取文件真实上传路径

                  String pathname=request.getServletContext().getRealPath("/photo");

                  //2.通过真实路径创建文件

                  File file=new File(pathname);

                  if(!file.exists()) {//判断是否存在,若不存在则新建

                         file.mkdirs();

                  }

                  //3.通过MultipartFile对象获取文件名

                  String fileName=phot.getOriginalFilename();

                  user.setPhoto(fileName);

                  File targetFile=new File(pathname+"/"+fileName);

                 

                  try {

                         //4.通过FileUtils把MultipartFile的文件复制到目标文件

                         FileUtils.writeByteArrayToFile(targetFile, phot.getBytes());

                  } catch (IOException e) {

                         e.printStackTrace();

                  }

                  request.setAttribute("user", user);

                  return mv;

           }

    4.在springMVC中配置文件上传解析器

             <!-- 配置文件上传解析器 -->

             <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

                       <!-- 设置最大文件字节 -->

                       <property name="maxInMemorySize" value="200000"></property>

             </bean>

    注意:网页上传文件的name要和MultipartFile对象名一致(否则报500),要和实体类中放文件路径的属性不一样否则会报400

    多个文件上传(在单个文件上传基础)

    1. jar包
    2. 网页表单要enctype="multipart/form-data",type="file"添加属性multiple="multiple"
    3. controller层接收MultipartFile[] myfile数组对象

    package com.zhiyou100.kfs.controller;

    import java.io.File;

    import java.io.IOException;

    import javax.servlet.http.HttpServletRequest;

    import org.apache.commons.io.FileUtils;

    import org.springframework.stereotype.Controller;

    import org.springframework.web.bind.annotation.RequestMapping;

    import org.springframework.web.bind.annotation.ResponseBody;

    import org.springframework.web.multipart.MultipartFile;

    import org.springframework.web.servlet.ModelAndView;

    import com.zhiyou100.kfs.bean.User;

    @Controller

    @RequestMapping("user")

    public class UserController {

          

           @RequestMapping("upload")

           public String upload(ModelAndView mv,User user,MultipartFile[] myfile,HttpServletRequest request) {

                  mv.setViewName("tes");

                  //1.通过request获取文件真实上传路径

                  String pathname=request.getServletContext().getRealPath("/photo");

                  System.out.println(pathname);

                  //2.通过真实路径创建文件

                  File file=new File(pathname);

                  if(!file.exists()) {//判断是否存在,若不存在则新建

                         file.mkdirs();

                  }

                  for(MultipartFile f:myfile) {

                         if(f.getSize()>0) {

                                //3.通过MultipartFile对象获取文件名

                                String fileName=f.getOriginalFilename();

                                user.setPhone(fileName);

                                System.out.println(user);

                                File targetFile=new File(pathname+"/"+fileName);

                               

                                try {

                                       //4.保存文件到目标目录

                                       f.transferTo(targetFile);

                                       //4.通过FileUtils把MultipartFile的文件复制到目标文件

    //                                 FileUtils.writeByteArrayToFile(targetFile, f.getBytes());

                                } catch (IOException e) {

                                       e.printStackTrace();

                                }

                         }

                  }

                 

                  return "index:"+user.getUserId();

           }

    }

  • 相关阅读:
    19牛客暑期多校 round2 H 01矩阵内第二大矩形
    NOIP2017滚粗记
    Left 4 Dead 2(求生之路2) 游戏打不开 游戏闪退 的一种可能性以及解决方法
    Luogu P1156 垃圾陷阱
    Luogu P1376 机器工厂
    Luogu P1842 奶牛玩杂技
    Luogu P1880 石子合并
    Luogu P1441 砝码称重(fj省选)
    Luogu P1077 摆花
    Luogu P1282 多米诺骨牌
  • 原文地址:https://www.cnblogs.com/kfsrex/p/11461914.html
Copyright © 2011-2022 走看看