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();

       }

}

原文地址:https://www.cnblogs.com/kfsrex/p/11461914.html