79 lines
2.6 KiB
Java
79 lines
2.6 KiB
Java
package com.imooc.controller;
|
|
|
|
|
|
import com.imooc.config.MinIOConfig;
|
|
import com.imooc.grace.result.GraceJSONResult;
|
|
import com.imooc.utils.MinIOUtils;
|
|
import com.imooc.utils.SMSUtils;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import com.imooc.utils.QcCloud;
|
|
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.InputStream;
|
|
import java.io.OutputStream;
|
|
|
|
|
|
@Slf4j
|
|
@Api(tags = "FileController 文件上传测试的接口")
|
|
@RestController
|
|
public class FileController {
|
|
|
|
@Autowired
|
|
private MinIOConfig minIOConfig;
|
|
|
|
@PostMapping("upload")
|
|
public GraceJSONResult upload(MultipartFile file) throws Exception {
|
|
// 获取 vlogdata 目录路径
|
|
// String storagePath = new File(System.getProperty("user.dir"), "vlogdata").getAbsolutePath();
|
|
String storagePath = "/data/vlogdata"; // 生产
|
|
// String storagePath = "/Users/wuzhongjie/Desktop/vlog-1.0.0/vlogdata"; // 本地
|
|
|
|
log.info(storagePath);
|
|
File storageDir = new File(storagePath);
|
|
if (!storageDir.exists()) {
|
|
storageDir.mkdirs(); // 确保目录存在
|
|
}
|
|
|
|
// 组装完整的存储路径
|
|
File destFile = new File(storageDir, file.getOriginalFilename());
|
|
log.info(destFile.getAbsolutePath());
|
|
String imgUrl = destFile.getAbsolutePath();
|
|
// 使用流手动存储文件
|
|
try (InputStream inputStream = file.getInputStream();
|
|
OutputStream outputStream = new FileOutputStream(destFile)) {
|
|
byte[] buffer = new byte[1024];
|
|
int bytesRead;
|
|
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
outputStream.write(buffer, 0, bytesRead);
|
|
}
|
|
}
|
|
|
|
log.info("本地文件存储成功: " + destFile.getAbsolutePath());
|
|
|
|
// String fileName = file.getOriginalFilename();
|
|
//
|
|
//
|
|
//
|
|
// MinIOUtils.uploadFile(minIOConfig.getBucketName(),
|
|
// fileName,
|
|
// file.getInputStream());
|
|
//
|
|
// String imgUrl = minIOConfig.getFileHost()
|
|
// + "/"
|
|
// + minIOConfig.getBucketName()
|
|
// + "/"
|
|
// + fileName;
|
|
|
|
return GraceJSONResult.ok(imgUrl);
|
|
}
|
|
}
|