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;                                                                
}                                                                               

 

posted on 2017-03-12 11:51  乄清雅轩灬  阅读(227)  评论(0编辑  收藏  举报

导航