zoukankan      html  css  js  c++  java
  • Spring Mvc:用MultiPartFile上传单个文件,多个文件

    1、单个文件上传步骤:

    添加Apache文件上传jar包

    首先需要下载两个apache上传文件的jar包,commons-fileupload-1.3.1jar,commons-io-2.4.jar

    具体使用版本,清根据项目进行选择

    2、配置MultipartResolver处理文件

    Spring mvc用的是MultipartFile来进行文件上传,所以我们需要配置MultipartResolver,用于处理表单中的file

    <bean id = "multipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="5400000">
        <property name="defaultEncoding" value="UTF-8" >
    </bean>
    

      

    属性介绍:

    maxUploadSize:最大上传文件的大小,单位为字节;

    defaultEncoding:请求的编码格式,默认为iso-8859-1

    3、编写文件上传控制器:

    @Controller
    public class FileUploadController {
        private static final UPLOAD_DIRECTORY =     propertiesUtil.get("fileupload.directory,""");    
    
        @RequestMapping(value="uploadFile",method=RequestMethod.POST);
        public ModelAndView uploadFile(@RequestParam("file") MultipartFile file) {
            // 判断文件是否为空
            if(!file.isEmpty()) {
                try{
                       // 判断文件目录是否存在,否则则自动生成
                       File directory = new File(UPLOAD_DIRECTORY);
                       if(!directory.exists()) {
                            directory.mkdir();
                      }
                      // 失败跳转视图
                      if(file.getSize() > 30000)
                      return new ModelAndView("uploadFail","msg",file)
                 }
            }
        }
    }
    

      

  • 相关阅读:
    python爬取网络上图片【小例子】
    python统计英文单词出现次数【小例子】
    python敏感字处理【小例子】
    platform操作系统信息
    迭代器
    xxxxx
    test
    Jquery
    Jquery二
    DOM文档对象模型
  • 原文地址:https://www.cnblogs.com/hengzhou/p/9590951.html
Copyright © 2011-2022 走看看