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

    }
}

欢迎大家关注我的公众号

发布了30 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_40898368/article/details/91984279