阿里云oss文件存储
package com.cqcfs.storage.oss.service; import java.io.InputStream; /** * 文件上传接口 * * @author kongweichun * @date 2017-12-14-上午10:45 */ public interface FileStorage{ /** * 上传文件 * * @param fileBytes 文件的字节数组 * @param key 文件名 * @return 文件标识的唯一id * @author LIQIU * @Date 2017/12/14 上午11:28 */ void store(byte[] fileBytes, String key); /** * 存储输入流 * * @param input * @param key */ void store(InputStream input, String key); /** * 下载文件 * * @param key 文件名(带后缀名的) * @author LIQIU * @Date 2017/12/14 上午11:28 */ byte[] getBytes(String key); /** * 通过KEY删除文件 * * @param key */ void remove(String key); /** * 获取输入流 * * @param key * @return */ InputStream getInputStream(String key); /** * 获取下载链接 * * @param key * @return */ String getDownloadUrl(String key); }
package com.cqcfs.storage.oss.service.imp; import com.aliyun.oss.OSSClient; import com.aliyun.oss.model.Bucket; import com.aliyun.oss.model.BucketInfo; import com.aliyun.oss.model.OSSObject; import com.cqcfs.storage.oss.service.FileStorage; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import javax.annotation.PreDestroy; import java.io.*; /** * 阿里云文件上传 * * @author kongweichun * @date 2017-12-14-上午10:53 */ @Service public class AliyunFileStorage implements FileStorage { @Value("${aliyun.file.storage.accesssKey}") private String accessKey; @Value("${aliyun.file.storage.accessKeySecret}") private String accessKeySecret; @Value("${aliyun.file.storage.endpoint}") private String endpoint; /** * oss的bucket名称 */ private String bucketName = "cqcfs-iot"; private OSSClient getOssClient(){ return new OSSClient(endpoint, accessKey, accessKeySecret); } /** * 文件上传 * @param fileBytes 文件的字节数组 * @param key 文件名 */ @Override public void store(byte[] fileBytes, String key) { getOssClient().putObject(bucketName, key, new ByteArrayInputStream(fileBytes)); } /** * 图片上传 * @param input * @param key */ @Override public void store(InputStream input, String key) { getOssClient().putObject(bucketName, key, input); } /** * 文件下载 * @param key 文件名(带后缀名的) * @return */ @Override public byte[] getBytes(String key) { OSSObject ossObject = getOssClient().getObject(bucketName, key); BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent())); byte[] buf = new byte[1024]; InputStream in = ossObject.getObjectContent(); for (int n = 0; n != -1; ) { try { n = in.read(buf, 0, buf.length); } catch (IOException e) { throw new RuntimeException("download file error!"); } } try { in.close(); reader.close(); } catch (IOException e) { throw new RuntimeException("download file error!"); } return buf; } /** * 删除文件 * @param key 文件名 */ @Override public void remove(String key) { getOssClient().deleteObject(bucketName, key); } /** * 图片下载 * @param key 文件名 * @return */ @Override public InputStream getInputStream(String key) { return getOssClient().getObject(bucketName, key).getObjectContent(); } /** * 获取文件下载地址 * @param key 文件名 * @return */ @Override public String getDownloadUrl(String key) { BucketInfo bucketInfo = getOssClient().getBucketInfo(this.getBucketName()); Bucket bucket = bucketInfo.getBucket(); return "http://" + bucket.getName() + "." + bucket.getExtranetEndpoint() + "/" + key; } public String getBucketName() { return bucketName; } public void setBucketName(String bucketName) { this.bucketName = bucketName; } @PreDestroy public void destroy() { getOssClient().shutdown(); } }
package com.cqcfs.storage.oss.server; import com.cqcfs.framework.common.protocol.Result; import com.cqcfs.storage.oss.model.MeterialUploadForm; import com.cqcfs.storage.oss.service.FileStorage; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.io.InputStream; /** * @author kongweichun * @since 2019/3/25 */ @RestController @RequestMapping("/file") @Api(value = "/file" , tags = "文件管理") public class AliyunFileStorageResource { @Autowired FileStorage fileStorage; @PostMapping("v1/upload") @ApiOperation(value = "文件上传") @ApiImplicitParams({ @ApiImplicitParam(paramType = "form", dataType="file", name = "file", value = "文件", required = true), @ApiImplicitParam(paramType = "query",dataType = "String",name = "key",value = "文件名称",required = true) }) public Result upload(MeterialUploadForm form, @RequestParam("key") String key)throws Exception{ if (form.getFile() != null){ byte[] bytes = form.getFile().getBytes(); fileStorage.store(bytes,key); return Result.buildSuccess("OK"); } return Result.buildFailure(70001,"文件为空"); } @PostMapping("v1/upload/img") @ApiOperation(value = "图片上传") @ApiImplicitParams({ @ApiImplicitParam(paramType = "form", dataType="file", name = "file", value = "文件", required = true), @ApiImplicitParam(paramType = "query",dataType = "String",name = "key",value = "文件名称",required = true) }) public Result uploadImg(MeterialUploadForm form, @RequestParam("key") String key)throws Exception{ if (form.getFile() != null){ InputStream input = form.getFile().getInputStream(); fileStorage.store(input,key); return Result.buildSuccess("OK"); } return Result.buildFailure(70001,"文件为空"); } @PostMapping("v1/get") @ApiOperation(value = "文件下载") @ApiImplicitParams({ @ApiImplicitParam(paramType = "query",dataType = "String",name = "key",value = "文件名称",required = true) }) public Result get(@RequestParam("key")String key){ byte[] bytes = fileStorage.getBytes(key); return Result.buildSuccess(bytes); } @PostMapping("v1/remove") @ApiOperation(value = "文件删除") @ApiImplicitParams({ @ApiImplicitParam(paramType = "query",dataType = "String",name = "key",value = "文件名称",required = true) }) public Result remove(@RequestParam("key")String key){ fileStorage.remove(key); return Result.buildSuccess("OK"); } @PostMapping("v1/get/inp") @ApiOperation(value = "获取输入流") @ApiImplicitParams({ @ApiImplicitParam(paramType = "query",dataType = "String",name = "key",value = "文件名称",required = true) }) public Result getInp(@RequestParam("key")String key){ InputStream inputStream = fileStorage.getInputStream(key); return Result.buildSuccess(inputStream); } @PostMapping("v1/get/url") @ApiOperation(value = "获取下载URL") @ApiImplicitParams({ @ApiImplicitParam(paramType = "query",dataType = "String",name = "key",value = "文件名称",required = true) }) public Result getUrl(String key){ String url = fileStorage.getDownloadUrl(key); return Result.buildSuccess(url); } }
package com.cqcfs.storage.oss.model; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import org.springframework.web.multipart.MultipartFile; /** * @author kongweichun * @since 2019/3/26 */ @Data @ApiModel(description = "材料上传form") public class MeterialUploadForm { @ApiModelProperty("材料文件") private MultipartFile file; }