| | |
| | | import org.springblade.common.config.FtpConfig; |
| | | import org.springblade.common.utils.FileUtil; |
| | | import org.springblade.core.launch.constant.AppConstant; |
| | | import org.springblade.core.log.exception.ServiceException; |
| | | import org.springblade.core.oss.model.BladeFile; |
| | | import org.springblade.core.oss.model.OssFile; |
| | | import org.springblade.core.secure.annotation.PreAuth; |
| | |
| | | return R.data(bladeFile); |
| | | } |
| | | |
| | | /** |
| | | * 上传文件 |
| | | * |
| | | * @param file 文件 |
| | | * @return ObjectStat |
| | | */ |
| | | @SneakyThrows |
| | | @PostMapping("/put-file-user-avatar") |
| | | public R putFileUserAvatar(@RequestParam MultipartFile file) { |
| | | long sizeL = file.getSize(); |
| | | double size = sizeL/1024; |
| | | |
| | | if (size<=30){ |
| | | throw new ServiceException("图片大小不能低于30KB"); |
| | | } |
| | | //填写你文件上传的地址以及相应信息 |
| | | String url = "http://223.82.109.183:2081"; |
| | | String access = "zhbaadmin"; |
| | | String secret = "zhbapassword"; |
| | | String bucket = "zhba"; |
| | | MinioClient minioClient = |
| | | MinioClient.builder() |
| | | .endpoint(url) |
| | | .credentials(access, secret) |
| | | .build(); |
| | | // 检查存储桶是否已经存在 |
| | | boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build()); |
| | | if (!isExist) { |
| | | // 创建一个名为zip的存储桶,用于zip文件。 |
| | | minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build()); |
| | | minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucket).build()); |
| | | } |
| | | String fileName = file.getOriginalFilename(); |
| | | String newName = "upload/picture/" + UUID.randomUUID().toString().replaceAll("-", "") |
| | | + fileName.substring(fileName.lastIndexOf(".")); |
| | | InputStream in = file.getInputStream(); |
| | | String[] split = newName.split("/"); |
| | | //创建头部信息 |
| | | Map<String, String> headers = new HashMap<>(1 << 2); |
| | | //添加自定义内容类型 |
| | | headers.put("Content-Type", "application/octet-stream"); |
| | | //上传 |
| | | minioClient.putObject( |
| | | PutObjectArgs.builder().bucket(bucket).object(newName).stream( |
| | | in, in.available(), -1) |
| | | .headers(headers) |
| | | .build()); |
| | | InputStream inputStream = file.getInputStream(); |
| | | FtpUtil.uploadFile(FtpConfig.ftpHost, ftpPort, FtpConfig.ftpUserName, ftpPassword, ftpPath, "/", split[2], inputStream); |
| | | in.close(); |
| | | String urls = "http://223.82.109.183:2081/zhba/" + newName; |
| | | //数据封装 |
| | | Map<String, Object> map = new HashMap<>(2); |
| | | map.put("name", newName); |
| | | map.put("url", urls); |
| | | //返回 |
| | | return R.data(map); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * app 文件上传 |
| | |
| | | String fileType = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(Locale.US); |
| | | //判断文件是不是zip类型 |
| | | if(!fileType.equals("zip")){ |
| | | map.put("data","上传文件类型不符!"); |
| | | return R.data(map); |
| | | // map.put("data","上传文件类型不符!"); |
| | | // return R.data(map); |
| | | throw new ServiceException("上传文件类型不符!必须是 zip 压缩文件格式!"); |
| | | } |
| | | //FileConfig.localtion是配置文件和config类生产的,测试demo可以直接把FileConfig.localtion替换成D:/test |
| | | String uuid = UUID.randomUUID().toString(); |
| | |
| | | String fileType = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(Locale.US); |
| | | //判断文件是不是zip类型 |
| | | if(!fileType.equals("zip")){ |
| | | map.put("data","上传文件类型不符!"); |
| | | return R.data(map); |
| | | // map.put("data","上传文件类型不符!"); |
| | | // return R.data(map); |
| | | throw new ServiceException("上传文件类型不符!必须是 zip 压缩文件格式!"); |
| | | } |
| | | //FileConfig.localtion是配置文件和config类生产的,测试demo可以直接把FileConfig.localtion替换成D:/test |
| | | String uuid = UUID.randomUUID().toString(); |