package com.keao.edu.user.controller; import com.keao.edu.common.controller.BaseController; import com.keao.edu.common.entity.UploadReturnBean; import com.keao.edu.user.service.UploadFileService; import com.keao.edu.util.upload.UploadUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiParam; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; /** * 上传控制层 */ @RestController @Api(tags = "文件上传服务") public class UploadFileController extends BaseController { private final static Logger LOGGER = LoggerFactory.getLogger(UploadFileController.class); @Autowired private UploadFileService uploadFileService; @PostMapping(value = "uploadFile") public Object uploadFile(@ApiParam(value = "上传的文件", required = true) @RequestParam("file") MultipartFile file) { try { if (file != null && StringUtils.isNotBlank(file.getOriginalFilename())) { UploadReturnBean bean = uploadFileService.uploadFile(file.getInputStream(), UploadUtil.getExtension(file.getOriginalFilename())); if (bean.isStatus()) { return succeed(bean); } return failed(bean.getMessage()); } } catch (Exception e) { LOGGER.error("上传失败", e); } return failed("上传失败"); } }