| | |
| | | import com.qcloud.cos.ClientConfig; |
| | | import com.qcloud.cos.auth.BasicCOSCredentials; |
| | | import com.qcloud.cos.auth.COSCredentials; |
| | | import com.qcloud.cos.model.ObjectMetadata; |
| | | import com.qcloud.cos.model.PutObjectRequest; |
| | | import com.qcloud.cos.model.PutObjectResult; |
| | | import com.qcloud.cos.region.Region; |
| | |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | public static String uploadImg(String key,File localFile,String secretId, String secretKey,String regionName,String bucketName) throws IOException { |
| | | public static String uploadImg(String key,MultipartFile localFile,String secretId, String secretKey,String regionName,String bucketName) throws IOException { |
| | | |
| | | // 1 初始化用户身份信息(secretId, secretKey)。 |
| | | COSCredentials cred = new BasicCOSCredentials(secretId, secretKey); |
| | |
| | | COSClient cosClient = new COSClient(cred, clientConfig); |
| | | |
| | | //开始上传 |
| | | PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile); |
| | | ObjectMetadata objectMetadata = new ObjectMetadata(); |
| | | // 设置输入流长度为500 |
| | | objectMetadata.setContentLength(500); |
| | | // 设置 Content type, 默认是 application/octet-stream,对于本地文件上传,默认根据本地文件的后缀进行映射 |
| | | // ,例如 jpg 文件映射 为image/jpeg对于流式上传 默认是 application/octet-stream |
| | | //objectMetadata.setContentType("application/pdf"); |
| | | PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key,localFile.getInputStream(),objectMetadata); |
| | | PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest); |
| | | |
| | | //拼接路径 |