存储成功:
一、引入pom
<dependency> <groupId>com.qcloud</groupId> <artifactId>cos_api</artifactId> <version>4.4</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency>
二、腾讯配置
三、代码
1、controller /** * 上传文件 */ @PostMapping("/upload") @ResponseBody public R upload(@RequestParam("file") MultipartFile file) throws Exception { //上传文件 String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); String url = CloudFactory.build().uploadSuffix(file.getBytes(), suffix); System.out.println("url=" + url); 、、 这里需要将地址保存到数据库 return R.ok().put("url", url); }
工厂类
public class CloudFactory { public static CloudStorageService build(){ //获取云存储配置信息 CloudStorageConfig config = new CloudStorageConfig(); config.setQcloudAppId(1256); config.setQcloudBucketName("ly"); config.setQcloudDomain("https://ly8.cossh.myqcloud.com"); config.setQcloudPrefix(""); config.setQcloudRegion("sh"); config.setQcloudSecretId("6RdXDerpa7PSFm3RpKZ"); config.setQcloudSecretKey("7zd191cx2rUXLtCFG3bu2va1DAs"); return new QcloudCloudStorageService(config); }
构建配置
抽象类
/** * 云存储(支持七牛、阿里云、腾讯云、又拍云) * @author lyon * @date 2018年3月27日 */ public abstract class CloudStorageService { /** 云存储配置信息 */ CloudStorageConfig config; /** * 文件路径 * @param prefix 前缀 * @param suffix 后缀 * @return 返回上传路径 */ public String getPath(String prefix, String suffix) { //生成uuid String uuid = UUID.randomUUID().toString().replaceAll("-", ""); //文件路径 String path = DateUtils.format(new Date(), "yyyyMMdd") + "/" + uuid; if(StringUtils.isNotBlank(prefix)){ path = prefix + "/" + path; } return path + suffix; } /** * 文件上传 * @param data 文件字节数组 * @param path 文件路径,包含文件名 * @return 返回http地址 */ public abstract String upload(byte[] data, String path); /** * 文件上传 * @param data 文件字节数组 * @param suffix 后缀 * @return 返回http地址 */ public abstract String uploadSuffix(byte[] data, String suffix); /** * 文件上传 * @param inputStream 字节流 * @param path 文件路径,包含文件名 * @return 返回http地址 */ public abstract String upload(InputStream inputStream, String path); /** * 文件上传 * @param inputStream 字节流 * @param suffix 后缀 * @return 返回http地址 */ public abstract String uploadSuffix(InputStream inputStream, String suffix); }
配置类bean
public class CloudStorageConfig { //腾讯云绑定的域名 private String qcloudDomain; //腾讯云路径前缀 private String qcloudPrefix; //腾讯云AppId private Integer qcloudAppId; //腾讯云SecretId private String qcloudSecretId; //腾讯云SecretKey private String qcloudSecretKey; //腾讯云BucketName private String qcloudBucketName; //腾讯云COS所属地区 private String qcloudRegion;
实际实现类
public class QcloudCloudStorageService extends CloudStorageService { private COSClient client; public QcloudCloudStorageService(CloudStorageConfig config){ this.config = config; //初始化 init(); } private void init(){ Credentials credentials = new Credentials(config.getQcloudAppId(), config.getQcloudSecretId(), config.getQcloudSecretKey()); //初始化客户端配置 ClientConfig clientConfig = new ClientConfig(); //设置bucket所在的区域,华南:gz 华北:tj 华东:sh clientConfig.setRegion(config.getQcloudRegion()); client = new COSClient(clientConfig, credentials); } @Override public String upload(byte[] data, String path) { //腾讯云必需要以"/"开头 if(!path.startsWith("/")) { path = "/" + path; } //上传到腾讯云 UploadFileRequest request = new UploadFileRequest(config.getQcloudBucketName(), path, data); String response = client.uploadFile(request); JSONObject jsonObject = JSONObject.fromObject(response); if(jsonObject.getInt("code") != 0) { try { throw new Exception("文件上传失败," + jsonObject.getString("message")); } catch (Exception e) { e.printStackTrace(); } } return config.getQcloudDomain() + path; } @Override public String upload(InputStream inputStream, String path) { try { byte[] data = IOUtils.toByteArray(inputStream); return this.upload(data, path); } catch (IOException e) { try { throw new Exception("上传文件失败", e); } catch (Exception e1) { e1.printStackTrace(); return null; } } } @Override public String uploadSuffix(byte[] data, String suffix) { return upload(data, getPath(config.getQcloudPrefix(), suffix)); } @Override public String uploadSuffix(InputStream inputStream, String suffix) { return upload(inputStream, getPath(config.getQcloudPrefix(), suffix)); } }