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 uploadPic(MultipartFile[] multipartFiles,String secFile){
        List urlList = new ArrayList();
        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 resultMap = new HashMap<>();
        try{
            List 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());
        }

    }
}

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

 

springboot上传单张或者多张图片,MultipartFile_第1张图片

你可能感兴趣的:(后台)