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>

 

posted @ 2016-07-05 17:22  左立军  阅读(15952)  评论(0编辑  收藏  举报