• 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>
  • 相关阅读:
    01-Git 及其可视化工具TortoiseGit 的安装和使用
    IntelliJ IDEA中Warning:java:源值1.5已过时, 将在未来所有发行版中删除
    JVM学习笔记四_垃圾收集器与内存分配策略
    JVM学习笔记三_异常初步
    CentOs7 图形界面和命令行界面切换
    基本概念一
    JVM学习笔记二_对象的创建、布局和定位
    JVM学习笔记一_运行时数据区域
    个人阅读作业二
    软件工程随笔
  • 原文地址:https://www.cnblogs.com/xiaozhang666/p/11653568.html
Copyright © 2020-2023  润新知