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

    }
}


多文件上传

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

@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>

复制代码

转载于:https://my.oschina.net/u/3270404/blog/843221

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值