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

    不考虑细节,只实现简单的文件上传操作

    一  引入common-fileupload.jar和common-io.jar文件

    二  在SprinMVC核心配置文件中配置解析器

    <!-- 配置文件上传 -->                                                                                         
    <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver">
        <!-- 解决文件名的中文乱码问题 -->                                                                               
        <property name="defaultEncoding" value="utf-8"/>                                                    
        <!-- 设置单个文件的大小限制 -->                                                                                
        <property name="maxUploadSizePerFile" value="1024"/>                                                
    </bean>                                                                                                 

    三  在视图层创建form表单

    <form action="fileupload" method="post" enctype="multipart/form-data">
        <input type="file" name="file">                                   
        <input type="submit" value="上传">                                  
    </form>                                                               

    四  在controller中处理

    @RequestMapping("/fileupload")                                                   
    public String fileUpload(MultipartFile file, HttpServletRequest request) {       
        ServletContext application = request.getServletContext();                    
        // 获取项目的绝对路径,并创建文件上传目录                                                       
        String savePath = application.getRealPath("/WEB-INF/upload");                
        File newFile = new File(savePath);                                           
        if (!newFile.exists() && !newFile.isDirectory()) {                           
            newFile.mkdir();                                                         
        }                                                                            
        // 获取文件的扩展名                                                                  
        String fileExtName = FilenameUtils.getExtension(file.getOriginalFilename()); 
        // 生成新的文件名                                                                   
        String newFileName = UUID.randomUUID().toString() + "." + fileExtName;       
        try {                                                                        
            // 打开读取文件的流                                                              
            InputStream is = file.getInputStream();                                  
            // 打开写入新文件的流                                                             
            FileOutputStream fo = new FileOutputStream(savePath + "/" + newFileName);
            IOUtils.copy(is, fo);                                                    
            fo.close();                                                              
            is.close();                                                              
        } catch (IOException e) {                                                    
            // TODO Auto-generated catch block                                       
            e.printStackTrace();                                                     
        }                                                                            
                                                                                     
        return null;                                                                 
    }                                                                                

    MVC的transferTo方法也可实现流的传输工作

    @RequestMapping("/fileupload02")                                                
    public String fileUpload02(MultipartFile file, HttpServletRequest request) {    
        ServletContext application = request.getServletContext();                   
        // 获取项目的绝对路径,并创建文件上传目录                                                      
        String savePath = application.getRealPath("/WEB-INF/upload");               
        File folder = new File(savePath);                                           
        if (!folder.exists() && !folder.isDirectory()) {                            
            folder.mkdir();                                                         
        }                                                                           
        // 获取文件的扩展名                                                                 
        String fileExtName = FilenameUtils.getExtension(file.getOriginalFilename());
        // 生成新的文件名                                                                  
        String newFileName = UUID.randomUUID().toString() + "." + fileExtName;      
        File newFile = new File(savePath + "/" + newFileName);                      
        try {                                                                       
            file.transferTo(newFile);                                               
        } catch (IllegalStateException | IOException e) {                           
            e.printStackTrace();                                                    
        }                                                                           
        return null;                                                                
    }                                                                               
  • 相关阅读:
    setoptsocket函数
    C++右值引用
    const char* char const* char*const
    select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
    gitee搭建应用
    C++ 实现基本运算+-*/
    C++学习笔记(一)mutable function
    创造型设计模式-----抽象工厂模式
    openPhase gcc迁移
    SQLPLUS 远程连接数据库
  • 原文地址:https://www.cnblogs.com/qingyaxuan/p/6537426.html
Copyright © 2011-2022 走看看