haungxing
3 months ago
3 changed files with 95 additions and 4 deletions
@ -0,0 +1,50 @@
|
||||
package com.hnac.hzinfo.inspect.task.service.impl; |
||||
|
||||
import com.hnac.hzinfo.exception.HzServiceException; |
||||
import com.hnac.hzinfo.inspect.utils.FileUtil; |
||||
import lombok.AllArgsConstructor; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springblade.core.oss.model.BladeFile; |
||||
import org.springblade.core.tool.api.R; |
||||
import org.springblade.core.tool.api.ResultCode; |
||||
import org.springblade.resource.feign.IOssClient; |
||||
import org.springframework.mock.web.MockMultipartFile; |
||||
import org.springframework.stereotype.Service; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* @Author: huangxing |
||||
* @Date: 2024/08/13 19:44 |
||||
*/ |
||||
@Service |
||||
@AllArgsConstructor |
||||
@Slf4j |
||||
public class FileUploaderService { |
||||
|
||||
private final IOssClient ossClient; |
||||
|
||||
/** |
||||
* minio上传文件 |
||||
* @param url 待上传文件url |
||||
* @param fileName 上传图片名称 |
||||
* @return 上传结果 |
||||
*/ |
||||
public BladeFile upload(String url,String fileName) { |
||||
MockMultipartFile file = FileUtil.urlToMultipartFile(url, fileName); |
||||
return this.upload(file); |
||||
} |
||||
|
||||
/** |
||||
* minio上传文件 |
||||
* @param file 待上传文件 |
||||
* @return 上传结果 |
||||
*/ |
||||
public BladeFile upload(MockMultipartFile file) { |
||||
R<BladeFile> result = ossClient.putFile(file); |
||||
Assert.isTrue(result.isSuccess(), () -> { |
||||
throw new HzServiceException(ResultCode.FAILURE,"文件上传失败!"); |
||||
}); |
||||
return result.getData(); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,42 @@
|
||||
package com.hnac.hzinfo.inspect.utils; |
||||
|
||||
import cn.hutool.http.HttpRequest; |
||||
import cn.hutool.http.HttpResponse; |
||||
import com.hnac.hzinfo.exception.HzServiceException; |
||||
import org.springblade.core.oss.model.BladeFile; |
||||
import org.springblade.core.tool.api.IResultCode; |
||||
import org.springblade.core.tool.api.R; |
||||
import org.springblade.core.tool.api.ResultCode; |
||||
import org.springblade.core.tool.utils.SpringUtil; |
||||
import org.springblade.resource.feign.IOssClient; |
||||
import org.springframework.mock.web.MockMultipartFile; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.web.client.RestTemplate; |
||||
import org.springframework.web.multipart.MultipartFile; |
||||
|
||||
import java.io.ByteArrayInputStream; |
||||
import java.io.InputStream; |
||||
|
||||
/** |
||||
* @Author: huangxing |
||||
* @Date: 2024/08/13 11:32 |
||||
*/ |
||||
public class FileUtil { |
||||
|
||||
public static MockMultipartFile urlToMultipartFile(String url, String fileName) { |
||||
HttpResponse response = HttpRequest.get(url).execute(); |
||||
if (response.isOk()) { |
||||
// 读取响应体为字节数组
|
||||
byte[] fileBytes = response.bodyBytes(); |
||||
// 使用字节数组、文件名和MIME类型创建MockMultipartFile
|
||||
return new MockMultipartFile( |
||||
"file", // 表单字段名,这通常与你的Spring MVC控制器中的@RequestParam("file")匹配
|
||||
fileName, // 文件名
|
||||
"image/jpeg", // MIME类型
|
||||
fileBytes // 文件内容字节数组
|
||||
); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue