import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.common.utils.StringUtils;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
@RestController
public class FileUploadController {
@PostMapping("/upload")
public Map<String, String> uploadFile(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
throw new RuntimeException("文件不能为空");
}
// 文件名
String fileName = file.getOriginalFilename();
// 上传到本地文件系统
String localFilePath = "/path/to/local/directory/" + fileName;
file.transferTo(new java.io.File(localFilePath));
// 上传到OSS
String endpoint = "您的EndPoint";
String accessKeyId = "您的AccessKeyId";
String accessKeySecret = "您的AccessKeySecret";
String bucketName = "您的Bucket名称";
String key = "文件路径和名称";
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
ossClient.putObject(bucketName, key, file.getInputStream());
} catch (IOException e) {
throw new RuntimeException("上传OSS失败", e);
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
Map<String, String> result = new LinkedHashMap<>();
result.put("localFilePath", localFilePath);
result.put("OSSFilePath", "https://" + bucketName + "." + endpoint + "/" + key);
return result;
}
@GetMapping("/policy")
public Map<String, String> getPolicy() {
String endpoint = "您的EndPoint";
String accessKeyId = "您的AccessKeyId";
String accessKeySecret = "您的AccessKeySecret";
String bucketName = "您的Bucket名称";
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
long expireTime = 30;
long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
Date expiration = new Date(expireEndTime);
PolicyConditions policyConds = new P
评论已关闭