springboot上传单张或者多张图片,MultipartFile

package cn.edu.bcu.longtermroom.hotel.pc.services;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class UploadService {

    @Value("${web.img-upload-path}")
    private String uploadPath; //上传路径

    public List<String> uploadPic(MultipartFile[] multipartFiles,String secFile){
        List<String> urlList = new ArrayList<String>();
        String realpath = uploadPath+secFile;
        CreatFileDir(realpath);
        String filename = null;
        for (MultipartFile multipartFile : multipartFiles) {
            try {
                filename = UUID.randomUUID().toString().replaceAll("-", "")+multipartFile.getOriginalFilename();
                multipartFile.transferTo(new File(realpath,filename));
                urlList.add(secFile+filename);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return urlList;
    }

    protected void CreatFileDir(String filepath) {
        File dir = new File(filepath);
        if (!dir.exists())
            dir.mkdirs();
    }

}
package cn.edu.bcu.longtermroom.hotel.pc.resources;

import cn.edu.bcu.longtermroom.commons.enumeration.ResponseCodeEnum;
import cn.edu.bcu.longtermroom.commons.exception.GlobalException;
import cn.edu.bcu.longtermroom.hotel.pc.services.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags="品牌业务信息")
@RestController
@RequestMapping("/upload")
public class UploadResource {
    @Autowired
    private UploadService uploadService;

    private static final String brandFile="BRAND/";
    @ApiOperation(value="图片上传")
    @ApiResponses({
            @ApiResponse(code=200,message="上传成功"),
            @ApiResponse(code=401,message="上传失败"),
            @ApiResponse(code=500,message="服务器内部错误")
    })
    @PostMapping(value = "/uploadPic")
    public ResponseEntity uploadPic(@RequestParam(name = "pic") MultipartFile[] multipartFiles){
        Map<String, Object> resultMap = new HashMap<>();
        try{
            List<String> paths = uploadService.uploadPic(multipartFiles,brandFile);
            resultMap.put("path",paths);
            resultMap.put("message", ResponseCodeEnum.SUCCESS);
            return new ResponseEntity(resultMap, HttpStatus.OK);
        }catch (Exception ex){
            //抛出自定义异常,交于全局异常处理器向前端返回Json数据
            throw new GlobalException(ResponseCodeEnum.BUSINESS_FAILED, ex.toString());
        }

    }
}

有收获点个赞吧~欢迎大家关注我的公众号

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值