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

    1.添加jar包

     2.网页

    表单必须是post提交,编码必须是multipart/form-data,文件上传的文本框必须取名

    3.在springmvc中配置文件上传解析器

    <!-- 配置文件上传解析器 -->
            
            <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!-- 设置文件上传的大小,单位是字节 -->
            <property name="maxUploadSize" value="20971520"></property>
            </bean>

    4.控制层处理代码

        @RequestMapping("upload")
        public String upload(MultipartFile myFile,HttpServletRequest request) {
            //1.获取文件上传的保存路径
            String path = request.getServletContext().getRealPath("/update");
            System.out.println(path);
            //2.创建文件对象
            File file=new File(path);
            if(!file.exists()) {
                file.mkdirs();
            }
            
            //3.获取文件名
            String name=System.currentTimeMillis()+myFile.getOriginalFilename();
            File targetFile=new File(path+"/"+name);
            
            //4.把文件写在指定的目录下
            try {
                FileUtils.writeByteArrayToFile(targetFile, myFile.getBytes());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return "redirect:ajax.jsp";
        }
  • 相关阅读:
    [1] Report Fusioncharts
    [1] Entity Framework / Code First
    [1] Ninject
    [1] 插件架构(PLUG-IN)
    linux下为目录和文件设置权限
    解决Class 'swoole_server' not found
    Linux下 PHP 安装pecl_http方法
    php安装swoole扩展
    将PHP 5.3.3 (cli)升级到PHP 5.6.31 (cli)
    windows下composer安装
  • 原文地址:https://www.cnblogs.com/sh-0131/p/11462288.html
Copyright © 2011-2022 走看看