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

     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    
         <property name="defaultEncoding" value="utf-8"></property>   
          <!-- 配置文件的最大上传容量限制 --> 
         <property name="maxUploadSize" value="50242440"></property>    
        </bean>
    <%@ 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>文件上传下载</title>
    </head>
    <body>
        <form action="${pageContext.request.contextPath }/file/upload.html"
            method="post" enctype="multipart/form-data">
            选择文件:<input type="file"  multiple="multiple" name="files" width="120px" > <input
                type="submit" value="上传">
        </form>
    </body>
    </html>
    /***
    * 文件的批量上传
    * @param files
    * @param request
    * @return
    * @throws Exception
    * @throws IOException
    */
    @RequestMapping(value="/file/upload.html",method=RequestMethod.POST)
    public String upload(@RequestParam("files") MultipartFile[] files,MultipartHttpServletRequest request) throws Exception, IOException{
    //获取文件夹的名字
    List<String[]> filess=new ArrayList<String[]>();
    String path = request.getSession().getServletContext().getRealPath("/upload");
    StringBuffer stringbuffer=new StringBuffer();
    //对传进来的文件数组,进行 循环复制
    for(MultipartFile multipartFile:files){
    //判断文件是否为空
    if(!multipartFile.isEmpty()) {
    //将多个文件名拼接在一个字符串中,用;分隔
    stringbuffer.append(multipartFile.getOriginalFilename());    
    stringbuffer.append(";");
    File dir=new File(path, multipartFile.getOriginalFilename());
    //将文件名和对应的路径存放在数组中
    String[] files1={multipartFile.getOriginalFilename(),dir.toPath().toString()};
    //将一个文件的标识信息存入集合中
    filess.add(files1);
    //System.out.println(dir.toPath());
    //文件不存则在创建
    if(!dir.exists()&&!dir.isDirectory()){ 
    dir.mkdirs(); 
    } 
    //文件进行复制
    multipartFile.transferTo(dir);
    }
    }
    String s=stringbuffer.substring(0, stringbuffer.length()-1);
    //将文件信息集合存入数据库中
    fileOperateService.insertfiles(filess);
    //跳转到调用文件显示的界面
    return "redirect:/file/showall.html"; 
    }



  • 相关阅读:
    汇编学习笔记(3)[bx]和loop
    C++面试题-概念篇(一)
    命名空间的冷思考
    背包以及装备模块封装的思考
    虚函数,纯虚函数以及虚继承
    组件化开发在游戏开发当中的思考和汇总
    Netty和MINA之间的比较思考
    学习C++与Java之间的区别
    C++服务器年前总结
    C++Builder如何将当前时间与字符串相互转换
  • 原文地址:https://www.cnblogs.com/itzyz/p/11516755.html
Copyright © 2011-2022 走看看