zoukankan      html  css  js  c++  java
  • java spring mvc restful 上传文件

    spring mvc 配置文件

    <bean class="com.baiyyy.yfz.core.RestfulHandlerMethodMapping" />
            <bean id="multipartResolver"  
                class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
                <property name="defaultEncoding" value="utf-8" />  
                <property name="maxUploadSize" value="10485760000" />  
                <property name="maxInMemorySize" value="40960" />  
            </bean>

    package com.baiyyy.yfz.controller;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.Date;
    import java.util.Map;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.multipart.MultipartFile;
    import org.springframework.web.multipart.MultipartHttpServletRequest;
    import org.springframework.web.multipart.commons.CommonsMultipartFile;
    
    import com.baiyyy.yfz.core.BaseController;
    import com.baiyyy.yfz.util.DateUtil;
    import com.baiyyy.yfz.util.PictureUploadPath;
    
    /**
     * 基础服务接口
     * 
     * @author 左立军
     *
     */
    @RestController
    @RequestMapping("/upload")
    public class UploadController extends BaseController {
    
        /**
         * 图片路径配置
         */
        @Autowired
        private PictureUploadPath pictureUploadPath;
    
        @RequestMapping(value = "/picture", consumes = "multipart/form-data", method = RequestMethod.POST)
        public void picture(@RequestParam("fileUpload") CommonsMultipartFile file) {
    
            // 判断文件是否存在
            if (!file.isEmpty()) {
                String path = pictureUploadPath.uploadPicturePath + "/"
                        + DateUtil.convertDateToYYYYMMdd(new Date()) + "/";
                File dir = new File(path);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
    
                path += file.getOriginalFilename();
                File localFile = new File(path);
                try {
                    file.transferTo(localFile);
                } catch (IllegalStateException | IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
    
        }
    }


    多文件上传


    @RequestMapping(value = "/picture", consumes = "multipart/form-data", method = RequestMethod.POST)
    	public void picture(HttpServletRequest request) {
    
    		String uploadPicturePath = pictureUploadPath.getUploadPicturePath();
    		String pathname = uploadPicturePath + "/"
    				+ DateUtil.convertDateToYYYYMMdd(new Date()) + "/";
    		File file = new File(pathname);
    		if (!file.exists()) {
    			file.mkdirs();
    		}
    		MultipartHttpServletRequest muti = (MultipartHttpServletRequest) request;
    		System.out.println(muti.getMultiFileMap().size());
    
    		MultiValueMap<String, MultipartFile> map = muti.getMultiFileMap();
    		for (Map.Entry<String, List<MultipartFile>> entry : map.entrySet()) {
    
    			List<MultipartFile> list = entry.getValue();
    			for (MultipartFile multipartFile : list) {
    				try {
    					multipartFile.transferTo(new File(pathname
    							+ multipartFile.getOriginalFilename()));
    				} catch (IllegalStateException | IOException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    	}
    
    
    
    
     用到的包
    <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>1.3.2</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.5</version>
            </dependency>
  • 相关阅读:
    Ubuntu完全教程,让你成为Ubuntu高手!
    Centos7安装完毕后重启提示Initial setup of CentOS Linux 7 (core)的解决方法
    MS SQL操作Xml示例
    MY SQL sql_mode设置
    MS SQL " 无法启动分布式事务"问题的解决思路
    MS SQL常用系统表汇总
    SQL不同服务器数据库之间数据操作整理
    OPENQUERY用法
    SQL Compare 10.4.8.87 Edition 数据库比较工具 完全破解+使用教程
    各种主流 SQLServer 迁移到 MySQL 工具对比
  • 原文地址:https://www.cnblogs.com/zuolijun/p/5644411.html
Copyright © 2011-2022 走看看