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

    springmvc 4.3.9 还支持servlet3 文件上传
    ------------------------------------------------------------
    <body>
    <h3>单文件上传</h3>
    <form action="upload.do" method="post" enctype="multipart/form-data">
    作者:<input type="text" name="author"><br>
    文件:<input type="file" name="ufile"><br>
    <input type="submit" value="提交">
    </form>

    @WebServlet("/upload.do") @MultipartConfig
    public class Upload extends HttpServlet{
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    req.setCharacterEncoding("utf-8");
    String n = req.getParameter("author");
    System.out.println(n);

    Part pf = req.getPart("ufile");
    String nnnn = pf.getSubmittedFileName();
    System.out.println(nnnn);


    String path = getServletContext().getRealPath("/upload/");
    System.out.println(path);
    File f = new File(path);
    if(!f.exists()){
    f.mkdirs();
    }
    pf.write(path+nnnn);
    }
    }

    spring mvc 文件 上传
    -----------------------------------------------------------------------
    1、项目pom.xml 文件加入依赖
    <!-- springmvc commons 文件上传 -->
    <dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.2</version>
    </dependency>
    会自动引入如下两个包
    commons-fileupload.jar
    commons-io.jar

    2、打开src/main/resources/mvc.xml springmvc核心配置文件
    <!-- 文件上传支持 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- 104857600 代表100MB -->
    <property name="maxUploadSize" value="104857600" />
    <property name="maxInMemorySize" value="40960" />
    <property name="defaultEncoding" value="UTF-8"></property>
    </bean>

    3、表单文件
    <h3>单文件上传</h3>
    <form action="up.action" method="post" enctype="multipart/form-data">
    作者:<input type="text" name="author"><br>
    文件:<input type="file" name="ufile"><br>
    <input type="submit" value="提交">
    </form>

    4、controller控制器 up.action 编写如下
    控制器方法参数注解如下
    此处ufile就是表单文件名称 <input type="file" name="ufile"/>

    @RequestParam("ufile") CommonsMultipartFile f,

    package com.fz.controller;
    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;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.File;
    import java.io.IOException;
    /**
    * Created by webrx on 2017-06-28.
    */
    @Controller
    public class UploadFile {
    @RequestMapping("/up.action")
    public void upload(@RequestParam("ufile") CommonsMultipartFile f, HttpServletResponse resp, HttpServletRequest req) throws IOException {
    String path = req.getServletContext().getRealPath("/upload/user/"); //项目根 /upload/user/ 此目录下
    System.out.println(path);
    File pa = new File(path);
    if(!pa.exists()){
    pa.mkdirs();
    }

    System.out.println(f.getBytes());
    System.out.println(f.getContentType());
    System.out.println(f.getName());//取得表单名称
    System.out.println(f.getSize()); //文件大小
    System.out.println(f.getOriginalFilename());//上传的原始文件名
    }
    }

    5、springmvc 多文件上传及表单内容接收
    <!-- springmvc commons 文件上传 -->
    <dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.2</version>
    </dependency>
    会自动引入如下两个包
    commons-fileupload.jar
    commons-io.jar

    src/main/resources/mvc.xml springmvc 框架核心配置文件加入bean配置
    <!-- 文件上传支持 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- 104857600 代表100MB -->
    <property name="maxUploadSize" value="104857600" />
    <property name="maxInMemorySize" value="40960" />
    <property name="defaultEncoding" value="UTF-8"></property>
    </bean>


    <h3>SpringMVC 多文件上传</h3>
    <form action="ups.action" method="post" enctype="multipart/form-data">
    作者:<input type="text" name="author"><br>
    文件:<input type="file" name="face" multiple><br>
    <input type="submit" value="提交">
    </form>

    //@RequestMapping("/ups.action") @ResponseBody
    //public void uploads(@RequestParam("face") CommonsMultipartFile fs[],HttpServletRequest req) throws IOException {
    @RequestMapping("/ups.action")
    public ModelAndView uploads(@RequestParam("face") List<CommonsMultipartFile> fs, HttpServletRequest req) throws IOException {
    String path = req.getServletContext().getRealPath("/upload/booktxt/");
    System.out.println(path);
    File pa = new File(path);
    if(!pa.exists()){
    pa.mkdirs();
    }

    //System.out.println(fs.length);
    System.out.println(fs.size());
    ModelAndView m = new ModelAndView("ok");
    List<String> info = new ArrayList<String>();
    for(CommonsMultipartFile cmf : fs){
    System.out.println(cmf.getOriginalFilename());
    System.out.println(cmf.getSize());
    cmf.transferTo(new File(path,cmf.getOriginalFilename()));
    info.add(String.format("文件名:%s,大小写:%d字节",cmf.getOriginalFilename(),cmf.getSize()));
    }
    m.addObject("fs",info);
    return m;
    }

    springmvc 多文件,单文件,上传时可以使用 req直接接表单字符串值
    //@RequestMapping("/ups.action") @ResponseBody
    //public void uploads(@RequestParam("face") CommonsMultipartFile fs[],HttpServletRequest req) throws IOException {
    @RequestMapping("/ups.action")
    public ModelAndView uploads(@RequestParam("face") List<CommonsMultipartFile> fs, HttpServletRequest req) throws IOException {
    String path = req.getServletContext().getRealPath("/upload/booktxt/");
    System.out.println(path);
    File pa = new File(path);
    if(!pa.exists()){
    pa.mkdirs();
    }

    //System.out.println(fs.length);
    System.out.println(fs.size());
    ModelAndView m = new ModelAndView("ok");
    List<String> info = new ArrayList<String>();
    for(CommonsMultipartFile cmf : fs){
    System.out.println(cmf.getOriginalFilename());
    System.out.println(cmf.getSize());
    cmf.transferTo(new File(path,cmf.getOriginalFilename()));
    info.add(String.format("文件名:%s,大小写:%d字节",cmf.getOriginalFilename(),cmf.getSize()));
    }
    m.addObject("fs",info);

    //接表单值 <input type="text" name="author" /> req.setAttribute("author",xxx);
    m.addObject("author",req.getParameter("author"));
    return m;
    }

    怕什么真理无穷,进一步有一步的欢喜
  • 相关阅读:
    快排原理讲解
    Kafka原理详解
    java中的基本数据类型转换
    centos7关闭防火墙
    安装Linux基本工具
    Kibana笔记
    虚拟机配置net模式
    2019-10-12,html+php+mysql简单留言板,作业
    2019-10-11:渗透测试,基础学习,php+mysql连接,笔记
    2019-10-10:渗透测试,基础学习,mysql语法基础,笔记
  • 原文地址:https://www.cnblogs.com/Mkady/p/7200842.html
Copyright © 2011-2022 走看看