spring-mvc文件上传

 index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    ${name}
    <form action="uplode.do" method="post" enctype="multipart/form-data">
        <input type="file" name="file"  />
        <input type="submit" value="上传"/>
    </form>
</body>
</html>

多文件上传

    <form action="bateh.do" method="post" enctype="multipart/form-data">
        1<input type="file" name="file"  />
        2<input type="file" name="file"  />
        <input type="submit" value="上传"/>
    </form>

iotest.java

package cn.zys.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

@Controller
public class IoTest {
    
    @RequestMapping("/uplode")
    public String uplode(@RequestParam("file")CommonsMultipartFile cmf,HttpServletRequest req) throws IOException{
        //获取文件名
        //获取文件的路径
        String path = req.getRealPath("/fileuplode");
        InputStream is = cmf.getInputStream();
        System.out.println(cmf.getOriginalFilename());
        System.out.println(path);
        OutputStream os = new FileOutputStream(new File(path,cmf.getOriginalFilename()));
        int len = 0;
        byte[] buffer = new byte[1024];
        while((len = is.read(buffer))!=-1)
            os.write(buffer,0,len);
        
        os.close();
        is.close();
        return "/yes.jsp";
    }
//多文件上传
@RequestMapping("/bateh")
    public String bateh(@RequestParam("file")CommonsMultipartFile cmf[],HttpServletRequest req) throws IOException{
        //获取文件名
        //获取文件的路径
        String path = req.getRealPath("/fileuplode");
        for(int i = 0; i<cmf.length; i++){
            InputStream is = cmf[i].getInputStream();
            System.out.println(cmf[i].getOriginalFilename());
            System.out.println(path);
            OutputStream os = new FileOutputStream(new File(path,cmf[i].getOriginalFilename()));
            int len = 0;
            byte[] buffer = new byte[1024];
            while((len = is.read(buffer))!=-1)
                os.write(buffer,0,len);
            
            os.close();
            is.close();
        }
        return "/yes.jsp";
    }
 
  
 

}

mvc.xml(一部分)

<!-- 文件上传配置 -->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
          <!--
        
            defaultEncoding:请求的编码格式必须和用户JSP的编码一致,以便正确读取表单中的内容。
            uploadTempDir:文件上传过程中的临时目录,上传完成后,临时文件会自动删除
            maxUploadSize:设置文件上传大小上限(单位为字节)-1为无限制
        
          -->
          <property name="defaultEncoding" value="UTF-8" />
          <property name="maxUploadSize" value="102400000" />
          <property name="maxInMemorySize" value="40960"></property>
          <!-- uploadTempDir可以不做设置,有默认的路径,上传完毕会临时文件会自动被清理掉 -->
        </bean>

猜你喜欢

转载自www.cnblogs.com/xiaozhang666/p/11653568.html
今日推荐