提交修改

This commit is contained in:
quyixiao 2025-08-29 13:32:47 +08:00
parent 3298341644
commit 2060b0cffe
3 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,60 @@
package com.heyu.api.data.utils;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public class CustomMultipartFile implements MultipartFile {
private final String name;
private final String originalFilename;
private final String contentType;
private final byte[] content;
public CustomMultipartFile(String name, String originalFilename, String contentType, byte[] content) {
this.name = name;
this.originalFilename = originalFilename;
this.contentType = contentType;
this.content = content;
}
@Override
public String getName() {
return this.name;
}
@Override
public String getOriginalFilename() {
return this.originalFilename;
}
@Override
public String getContentType() {
return this.contentType;
}
@Override
public boolean isEmpty() {
return this.content.length == 0;
}
@Override
public long getSize() {
return this.content.length;
}
@Override
public byte[] getBytes() throws IOException {
return this.content;
}
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(this.content);
}
@Override
public void transferTo(java.io.File dest) throws IOException, IllegalStateException {
throw new UnsupportedOperationException("Not implemented");
}
}

View File

@ -0,0 +1,37 @@
package com.heyu.api.data.utils;
import org.springframework.web.multipart.MultipartFile;
import java.io.FileInputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FileUtils {
public static MultipartFile getFile(String filePath) {
// 获取路径对象
Path path = Paths.get(filePath);
// 获取文件名
String fileName = path.getFileName().toString();
String contentType;
byte[] content;
try (FileInputStream fis = new FileInputStream(filePath)) {
// 获取文件的内容类型
contentType = Files.probeContentType(path);
// 读取文件内容到字节数组
content = fis.readAllBytes();
// 创建并返回一个 CustomMultipartFile 对象
return new CustomMultipartFile(fileName, fileName, contentType, content);
} catch (Exception e) {
// 打印错误信息便于调试
e.printStackTrace();
// 返回 null 表示读取文件失败
return null;
}
}
}

View File

@ -1,6 +1,7 @@
package com.heyu.api.controller.mm;
import com.heyu.api.data.utils.FileUtils;
import com.heyu.api.data.utils.R;
import com.heyu.api.oss.OssFileUploadService;
import lombok.extern.slf4j.Slf4j;
@ -21,15 +22,28 @@ public class UploadOssController {
@Autowired
private OssFileUploadService ossFileUploadService;
/**
* @param files
* @return
* http://localhost:8888/mm/upload/file
*/
@PostMapping("/file")
public R uploadFile(MultipartFile[] files) {
files = new MultipartFile[1];
files[1] = FileUtils.getFile("/Users/quyixiao/Desktop/seal.jpeg");
List<Map<String, Object>> data = ossFileUploadService.uploadImages(files);
return R.ok().setData(data);
}
}