码迷,mamicode.com
首页 > 编程语言 > 详细

java spring mvc restful 上传文件

时间:2016-07-05 18:33:28      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

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

    }
}

 

java spring mvc restful 上传文件

标签:

原文地址:http://www.cnblogs.com/zuolijun/p/5644411.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!