提交修改
This commit is contained in:
parent
2060b0cffe
commit
ec9876f471
@ -1,37 +1,10 @@
|
|||||||
package com.heyu.api.data.utils;
|
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 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,16 +1,19 @@
|
|||||||
package com.heyu.api.controller.mm;
|
package com.heyu.api.controller.mm;
|
||||||
|
|
||||||
|
|
||||||
import com.heyu.api.data.utils.FileUtils;
|
|
||||||
import com.heyu.api.data.utils.R;
|
import com.heyu.api.data.utils.R;
|
||||||
import com.heyu.api.oss.OssFileUploadService;
|
import com.heyu.api.oss.OssFileUploadService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.ObjectUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -26,19 +29,20 @@ public class UploadOssController {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param files
|
*post man 配置
|
||||||
* @return
|
* https://blog.csdn.net/maowendi/article/details/80537304
|
||||||
|
*
|
||||||
|
*
|
||||||
* http://localhost:8888/mm/upload/file
|
* http://localhost:8888/mm/upload/file
|
||||||
*/
|
*/
|
||||||
@PostMapping("/file")
|
@PostMapping("/file")
|
||||||
public R uploadFile(MultipartFile[] files) {
|
public R uploadFile(HttpServletRequest request, HttpServletResponse response ,@RequestParam("file") MultipartFile file) {
|
||||||
|
|
||||||
|
String userName = ObjectUtils.toString(request.getParameter("userPhone"), "");
|
||||||
|
|
||||||
files = new MultipartFile[1];
|
String type = ObjectUtils.toString(request.getParameter("type"), "");
|
||||||
files[1] = FileUtils.getFile("/Users/quyixiao/Desktop/seal.jpeg");
|
|
||||||
|
|
||||||
|
List<Map<String, Object>> data = ossFileUploadService.uploadImages(new MultipartFile[]{file});
|
||||||
List<Map<String, Object>> data = ossFileUploadService.uploadImages(files);
|
|
||||||
return R.ok().setData(data);
|
return R.ok().setData(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user