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

    1 通过commons-fileupload来实现 导入相关jar包

    commons-fileupload,commons-io

    2 配置springmvc的配置解析器

    mvc:

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
                <property name="defaultEncoding" value="utf-8"></property>
                <property name="maxUploadSize" value="10485760000"></property>
                <property name="maxInMemorySize" value="40960"></property>
         </bean>

    3 jsp页面

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'index.jsp' starting page</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
      </head>
      
      <body>
        <form action="upload.do" method="post" enctype="multipart/form-data">
          file:<input type="file" name="file"/> <input type="submit" value="上传"/>
          </form>
      </body>
    </html>

    4 controller代码

    @Controller
    public class FileUploadController {
        @RequestMapping("/upload")
        public String fileupload(@RequestParam("file")CommonsMultipartFile file,HttpServletRequest req) throws IOException{
            //获取文件名
            //file.getOriginalFilename();
            //获取上传文件的路径
            String path = req.getRealPath("/fileupload");
            InputStream is = file.getInputStream();
            OutputStream os = new FileOutputStream(new File(path,file.getOriginalFilename()));
            int len = 0;
            byte[] buffer = new byte[400];
            while((len=is.read(buffer))!=-1){
                os.write(buffer,0,len);
                os.close();
                is.close();
            }
                
            return "/index.jsp";
        }
    
    }

    批量上传的代码

    @RequestMapping("/batch")
        public String fileupload(@RequestParam("file")CommonsMultipartFile file[],
                HttpServletRequest req) throws IOException{
            //获取文件名
            //file.getOriginalFilename();
            //获取上传文件的路径
            String path = req.getRealPath("/fileupload");
            for (int i = 0; i < file.length; i++) {            
            InputStream is = file[i].getInputStream();
            OutputStream os = new FileOutputStream(new File(path,file[i].getOriginalFilename()));
            int len = 0;
            byte[] buffer = new byte[400];
            while((len=is.read(buffer))!=-1)
                os.write(buffer,0,len);
                os.close();
                is.close();        
            }
                
            return "/index.jsp";
        }
  • 相关阅读:
    CSRF小结
    代码注入小结
    文件上传漏洞小结
    解决Burpsuite_pro_v1.6破解版https证书导入问题
    Java HTTP 组件库选型看这篇就够了
    趣图:我正在演示一个功能,但没有达到预期效果
    阅读源码的利器——Intellij-IDEA-Replace-in-Path-使用技巧
    分享一些好用的 Chrome 插件!
    趣图:程序员发量的变化过程
    Spring循环依赖的三种方式
  • 原文地址:https://www.cnblogs.com/alloevil/p/6072035.html
Copyright © 2011-2022 走看看