| | |
| | | |
| | | import com.ycl.jxkg.base.BaseApiController; |
| | | import com.ycl.jxkg.base.Result; |
| | | import com.ycl.jxkg.config.RuoYiConfig; |
| | | import com.ycl.jxkg.service.FileUpload; |
| | | import com.ycl.jxkg.service.UserService; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.util.StringUtils; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.ResponseBody; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | |
| | | import org.springframework.web.multipart.MultipartHttpServletRequest; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.util.HashMap; |
| | | import java.util.UUID; |
| | | |
| | | |
| | | @RequiredArgsConstructor |
| | | @RequestMapping("/api/student/upload") |
| | | @RestController("StudentUploadController") |
| | | public class UploadController extends BaseApiController { |
| | | |
| | | private final FileUpload fileUpload; |
| | | private final UserService userService; |
| | | private final RuoYiConfig ruoYiConfig; |
| | | |
| | | @Autowired |
| | | public UploadController(FileUpload fileUpload, UserService userService) { |
| | | this.fileUpload = fileUpload; |
| | | this.userService = userService; |
| | | } |
| | | |
| | | |
| | | @RequestMapping("/image") |
| | | @ResponseBody |
| | | public Result questionUploadAndReadExcel(HttpServletRequest request) { |
| | | MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request; |
| | | MultipartFile multipartFile = multipartHttpServletRequest.getFile("file"); |
| | | long attachSize = multipartFile.getSize(); |
| | | String imgName = multipartFile.getOriginalFilename(); |
| | | try (InputStream inputStream = multipartFile.getInputStream()) { |
| | | String filePath = fileUpload.uploadFile(inputStream, attachSize, imgName); |
| | | userService.changePicture(getCurrentUser(), filePath); |
| | | return Result.ok(filePath); |
| | | } catch (IOException e) { |
| | | return Result.fail(2, e.getMessage()); |
| | | @PostMapping("/image") |
| | | public Result questionUploadAndReadExcel(MultipartFile file) { |
| | | // 检查文件是否为空 |
| | | if (file == null || file.isEmpty()) { |
| | | return Result.fail(500, "上传的文件为空"); |
| | | } |
| | | String randomName = null; |
| | | HashMap hashMap = new HashMap(2); |
| | | try { |
| | | // 获取文件名 |
| | | String originalFileName = StringUtils.cleanPath(file.getOriginalFilename()); |
| | | randomName = UUID.randomUUID().toString().replace("-", "") + originalFileName.substring(originalFileName.lastIndexOf(".")); |
| | | // 指定文件存储路径 |
| | | String uploadDir = ruoYiConfig.getUrl(); // 修改为您希望存储的目录 |
| | | // 如果目录不存在,则创建目录 |
| | | File dir = new File(uploadDir); |
| | | if (!dir.exists()) { |
| | | dir.mkdirs(); |
| | | } |
| | | // 构建目标文件的路径 |
| | | String filePath = uploadDir + "/" + randomName; |
| | | // 将文件保存到目标位置 |
| | | file.transferTo(new File(filePath)); |
| | | hashMap.put("name", originalFileName); |
| | | hashMap.put("url", randomName); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | // 返回失败响应 |
| | | return Result.fail(500, "文件上传失败"); |
| | | } |
| | | userService.changePicture(getCurrentUser(), randomName); |
| | | return Result.ok(hashMap); |
| | | } |
| | | |
| | | |
| | | } |