-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from yanyanho/main
add cos file storage
- Loading branch information
Showing
13 changed files
with
305 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 53 additions & 65 deletions
118
src/main/java/com/dl/officialsite/file/FileController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,53 @@ | ||
//package com.dl.officialsite.file; | ||
// | ||
//import cn.hutool.core.io.IoUtil; | ||
//import com.dl.officialsite.common.base.BaseResponse; | ||
//import com.dl.officialsite.common.enums.CodeEnums; | ||
//import com.dl.officialsite.common.exception.BizException; | ||
//import com.dl.officialsite.ipfs.IPFSService; | ||
//import lombok.Data; | ||
//import lombok.extern.slf4j.Slf4j; | ||
//import org.springframework.web.bind.annotation.*; | ||
//import org.springframework.web.multipart.MultipartFile; | ||
// | ||
//import javax.servlet.http.HttpServletResponse; | ||
//import java.io.IOException; | ||
//import java.io.InputStream; | ||
// | ||
///** | ||
// * @ClassName FileController | ||
// * @Author jackchen | ||
// * @Date 2023/10/16 17:07 | ||
// * @Description 文件上传 | ||
// **/ | ||
//@RestController | ||
//@RequestMapping("/file") | ||
//@Data | ||
//@Slf4j | ||
//public class FileController { | ||
// | ||
// private final IPFSService ipfsService; | ||
// | ||
// /** | ||
// * 文件上传 | ||
// */ | ||
// @PostMapping("/upload") | ||
// public BaseResponse upload(@RequestParam("file") MultipartFile file, | ||
// @RequestParam String address) { | ||
// try { | ||
// String hash = ipfsService.upload(file.getBytes()); | ||
// return BaseResponse.successWithData(hash); | ||
// } catch (IOException e) { | ||
// log.error("文件上传失败{}", file.getName()); | ||
// throw new BizException(CodeEnums.FAIL_UPLOAD_FAIL.getCode(), | ||
// CodeEnums.FAIL_UPLOAD_FAIL.getMsg()); | ||
// } | ||
// } | ||
// | ||
// /** | ||
// * 文件下载 | ||
// */ | ||
// @GetMapping("/download") | ||
// public void download(@RequestParam String fileHash, | ||
// @RequestParam String address, HttpServletResponse response) | ||
// throws IOException { | ||
// InputStream inputStream = null; | ||
// try { | ||
// inputStream = ipfsService.downloadStream(fileHash); | ||
// } catch (IOException e) { | ||
// log.error("文件下载失败{}", fileHash); | ||
// throw new BizException(CodeEnums.FAIL_DOWNLOAD_FAIL.getCode(), | ||
// CodeEnums.FAIL_DOWNLOAD_FAIL.getMsg()); | ||
// } | ||
// response.setContentType("application/octet-stream"); | ||
// IoUtil.copy(inputStream, response.getOutputStream()); | ||
// } | ||
//} | ||
package com.dl.officialsite.file;//package com.dl.officialsite.file; | ||
|
||
import cn.hutool.core.io.IoUtil; | ||
import com.dl.officialsite.common.base.BaseResponse; | ||
import com.dl.officialsite.file.cos.FileService; | ||
import lombok.Data; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* @ClassName FileController | ||
* @Author jackchen | ||
* @Date 2023/10/16 17:07 | ||
* @Description 文件上传 | ||
**/ | ||
@RestController | ||
@RequestMapping("/file") | ||
@Data | ||
@Slf4j | ||
public class FileController { | ||
|
||
private final FileService fileService; | ||
|
||
/** | ||
* 文件上传 | ||
*/ | ||
@PostMapping("/upload") | ||
public BaseResponse upload(@RequestParam(required = false) MultipartFile file, | ||
@RequestParam String address) { | ||
String hash = fileService.upload(file); | ||
return BaseResponse.successWithData(hash); | ||
} | ||
|
||
/** | ||
* todo | ||
*/ | ||
@GetMapping("/download") | ||
public void download(@RequestParam String fileHash, | ||
@RequestParam String address, HttpServletResponse response) | ||
throws IOException { | ||
InputStream inputStream = null; | ||
|
||
inputStream = fileService.download(fileHash); | ||
|
||
response.setContentType("application/octet-stream"); | ||
IoUtil.copy(inputStream, response.getOutputStream()); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/com/dl/officialsite/file/FileControllerWithipfs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//package com.dl.officialsite.file; | ||
// | ||
//import cn.hutool.core.io.IoUtil; | ||
//import com.dl.officialsite.common.base.BaseResponse; | ||
//import com.dl.officialsite.common.enums.CodeEnums; | ||
//import com.dl.officialsite.common.exception.BizException; | ||
//import com.dl.officialsite.ipfs.IPFSService; | ||
//import lombok.Data; | ||
//import lombok.extern.slf4j.Slf4j; | ||
//import org.springframework.web.bind.annotation.*; | ||
//import org.springframework.web.multipart.MultipartFile; | ||
// | ||
//import javax.servlet.http.HttpServletResponse; | ||
//import java.io.IOException; | ||
//import java.io.InputStream; | ||
// | ||
///** | ||
// * @ClassName FileController | ||
// * @Author jackchen | ||
// * @Date 2023/10/16 17:07 | ||
// * @Description 文件上传 | ||
// **/ | ||
//@RestController | ||
//@RequestMapping("/file") | ||
//@Data | ||
//@Slf4j | ||
//public class FileController { | ||
// | ||
// private final IPFSService ipfsService; | ||
// | ||
// /** | ||
// * 文件上传 | ||
// */ | ||
// @PostMapping("/upload") | ||
// public BaseResponse upload(@RequestParam("file") MultipartFile file, | ||
// @RequestParam String address) { | ||
// try { | ||
// String hash = ipfsService.upload(file.getBytes()); | ||
// return BaseResponse.successWithData(hash); | ||
// } catch (IOException e) { | ||
// log.error("文件上传失败{}", file.getName()); | ||
// throw new BizException(CodeEnums.FAIL_UPLOAD_FAIL.getCode(), | ||
// CodeEnums.FAIL_UPLOAD_FAIL.getMsg()); | ||
// } | ||
// } | ||
// | ||
// /** | ||
// * 文件下载 | ||
// */ | ||
// @GetMapping("/download") | ||
// public void download(@RequestParam String fileHash, | ||
// @RequestParam String address, HttpServletResponse response) | ||
// throws IOException { | ||
// InputStream inputStream = null; | ||
// try { | ||
// inputStream = ipfsService.downloadStream(fileHash); | ||
// } catch (IOException e) { | ||
// log.error("文件下载失败{}", fileHash); | ||
// throw new BizException(CodeEnums.FAIL_DOWNLOAD_FAIL.getCode(), | ||
// CodeEnums.FAIL_DOWNLOAD_FAIL.getMsg()); | ||
// } | ||
// response.setContentType("application/octet-stream"); | ||
// IoUtil.copy(inputStream, response.getOutputStream()); | ||
// } | ||
//} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/dl/officialsite/file/cos/COSClientConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.dl.officialsite.file.cos; | ||
|
||
|
||
import com.qcloud.cos.COSClient; | ||
import com.qcloud.cos.ClientConfig; | ||
import com.qcloud.cos.auth.BasicCOSCredentials; | ||
import com.qcloud.cos.auth.COSCredentials; | ||
import com.qcloud.cos.region.Region; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@EnableConfigurationProperties(COSProperties.class) | ||
public class COSClientConfig { | ||
|
||
|
||
@Autowired | ||
private COSProperties cosProperties; | ||
|
||
@Bean | ||
public COSClient cosClient(){ | ||
COSCredentials cred = new BasicCOSCredentials(cosProperties.getSecretId(),cosProperties.getSecretKey()); | ||
Region region = new Region(cosProperties.getRegionName()); | ||
ClientConfig clientConfig = new ClientConfig(region); | ||
// 3 生成 cos 客户端。 | ||
COSClient cosClient = new COSClient(cred, clientConfig); | ||
return cosClient; | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/dl/officialsite/file/cos/COSProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.dl.officialsite.file.cos; | ||
|
||
import lombok.Data; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@Data | ||
@ConfigurationProperties(prefix = "qcloud") | ||
public class COSProperties { | ||
private String secretId; | ||
private String secretKey; | ||
private String bucketName; | ||
private String regionName; | ||
|
||
|
||
} |
102 changes: 102 additions & 0 deletions
102
src/main/java/com/dl/officialsite/file/cos/FileService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.dl.officialsite.file.cos; | ||
|
||
|
||
import com.qcloud.cos.COSClient; | ||
import com.qcloud.cos.model.ObjectMetadata; | ||
import com.qcloud.cos.model.PutObjectRequest; | ||
import com.qcloud.cos.model.PutObjectResult; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import javax.imageio.ImageIO; | ||
import java.awt.image.BufferedImage; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@Service | ||
@Slf4j | ||
public class FileService { | ||
|
||
@Autowired | ||
private COSClient cosClient; | ||
@Autowired | ||
private COSProperties cosProperties; | ||
|
||
// 支持的文件类型 | ||
private static final List<String> suffixes = Arrays.asList("image/png", "image/jpeg"); | ||
|
||
|
||
public String upload(MultipartFile file) { | ||
|
||
// 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20 M 以下的文件使用该接口 | ||
// 大文件上传请参照 API 文档高级 API 上传 | ||
File localFile = null; | ||
|
||
String oldFileName = file.getOriginalFilename(); | ||
String eName = oldFileName.substring(oldFileName.lastIndexOf(".")); | ||
String newFileName = UUID.randomUUID()+eName; | ||
String key = null; | ||
try { | ||
localFile = File.createTempFile("temp",null); | ||
file.transferTo(localFile); | ||
key = UUID.randomUUID().toString().replace("-",""); | ||
PutObjectRequest putObjectRequest = new PutObjectRequest(cosProperties.getBucketName(), key, localFile); | ||
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest); | ||
} catch (IOException e) { | ||
} | ||
//拼接返回路径 | ||
cosClient.shutdown(); | ||
String imagePath = "https://" + cosProperties.getBucketName() + ".cos." + cosProperties.getRegionName() + ".myqcloud.com/" + key; | ||
return imagePath; | ||
} | ||
|
||
public String uploadImage(MultipartFile file) { | ||
String key = null; | ||
try { | ||
// 1、图片信息校验 | ||
// 1)校验文件类型 | ||
String type = file.getContentType(); //获取文件格式 | ||
if (!suffixes.contains(type)) { | ||
// logger.info("上传失败,文件类型不匹配:{}", type); | ||
return null; | ||
} | ||
// 2)校验图片内容 | ||
BufferedImage image = ImageIO.read(file.getInputStream()); | ||
if (image == null) { | ||
// logger.info("上传失败,文件内容不符合要求"); | ||
return null; | ||
} | ||
|
||
ObjectMetadata objectMetadata = new ObjectMetadata(); | ||
objectMetadata.setContentType(type); | ||
UUID uuid = UUID.randomUUID(); | ||
// 指定要上传到 COS 上对象键 此key是文件唯一标识 | ||
key = uuid.toString().replace("-","")+".jpg"; | ||
PutObjectRequest putObjectRequest = new PutObjectRequest(cosProperties.getBucketName(), key, file.getInputStream(),objectMetadata); | ||
|
||
//使用cosClient调用第三方接口 | ||
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest); | ||
log.info(putObjectRequest+""); | ||
//返回路径 | ||
|
||
}catch (Exception e){ | ||
e.printStackTrace(); | ||
} | ||
cosClient.shutdown(); | ||
//拼接返回路径 | ||
String imagePath = "https://" + cosProperties.getBucketName() + ".cos." + cosProperties.getRegionName() + ".myqcloud.com/" + key; | ||
return imagePath; | ||
} | ||
|
||
|
||
//todo | ||
public InputStream download(String fileHash) { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ public class Hiring { | |
|
||
private String invoice; | ||
|
||
|
||
private String yearlySalary; | ||
|
||
private String benefits; | ||
|
Oops, something went wrong.