| | |
| | | package com.jilongda.common.component; |
| | | |
| | | |
| | | import com.aliyun.oss.OSS; |
| | | import com.aliyun.oss.OSSClientBuilder; |
| | | import com.aliyun.oss.common.utils.BinaryUtil; |
| | | import com.aliyun.oss.model.MatchMode; |
| | | import com.aliyun.oss.model.OSSObject; |
| | | import com.aliyun.oss.model.PolicyConditions; |
| | | import com.aliyun.oss.model.PutObjectResult; |
| | | import com.jilongda.common.basic.ApiResult; |
| | | import com.jilongda.common.utils.WebUtils; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.Map; |
| | | |
| | | |
| | | /** |
| | | * @author feiyunchuan |
| | | */ |
| | | @Slf4j |
| | | @Api(tags = "Ali-OSS文件上传接口") |
| | | @RestController |
| | | @RequestMapping("/ali-oss/") |
| | | public class AliOssFileUploadController { |
| | | |
| | | @Value("${file.cdn:https://nncdn.pharmacylinked.com}") |
| | | private String FILE_CDN; |
| | | |
| | | |
| | | @ApiOperation(value = "单文件上传", notes = "单文件上传,rename 默认不重命名") |
| | | @PostMapping(value = "strUpload", headers = "content-type=multipart/form-data") |
| | | public String strUploads(@RequestParam(value = "file") MultipartFile mf) throws IOException { |
| | | if (mf.isEmpty()) { |
| | | return "请传入文件!"; |
| | | } |
| | | String fileName = mf.getOriginalFilename(); |
| | | String TimeDir =new SimpleDateFormat("yyyy-MM-dd").format(new Date()); |
| | | // String realPath = "D:\\" + TimeDir; |
| | | String realPath = "/usr/local/nginx/html/images/" + TimeDir; |
| | | File file = new File(realPath); |
| | | // 没有目录就创建 |
| | | if (!file.exists()) { |
| | | file.mkdirs(); |
| | | } |
| | | File targetFile = new File(realPath, fileName);//目标文件 |
| | | //开始从源文件拷贝到目标文件 |
| | | //传图片一步到位 |
| | | mf.transferTo(targetFile); |
| | | //拼接数据 |
| | | return "http://eyes.chelota.com/images/" + TimeDir +"/"+ fileName; |
| | | } |
| | | @ApiOperation(value = "服务端上传", notes = "服务端上传") |
| | | @PostMapping(value = "upload") |
| | | public ApiResult<String> fileUpload(@RequestParam(value = "file") MultipartFile file) throws IOException { |
| | | InputStream inputStream = file.getInputStream(); |
| | | String filename = System.currentTimeMillis() + file.getOriginalFilename(); |
| | | // Endpoint以杭州为例,其它Region请按实际情况填写。 |
| | | final String endpoint = AliOss.endpoint; |
| | | // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。 |
| | | final String accessKeyId = AliOss.accessKeyId; |
| | | final String accessKeySecret = AliOss.accessKeySecret; |
| | | final String bucketName = AliOss.bucketName; |
| | | // <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。 |
| | | String objectName = AliOss.dir + filename; |
| | | // 创建OSSClient实例。 |
| | | OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
| | | // 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。 |
| | | // InputStream inputStream = new FileInputStream("D:\\localpath\\examplefile.txt"); |
| | | // 依次填写Bucket名称(例如examplebucket)和Object完整路径(例如exampledir/exampleobject.txt)。Object完整路径中不能包含Bucket名称。 |
| | | PutObjectResult putObjectResult = ossClient.putObject(bucketName, objectName, inputStream); |
| | | OSSObject ossObject = ossClient.getObject(bucketName, objectName); |
| | | String uri = ossObject.getResponse().getUri(); |
| | | // 关闭OSSClient。 |
| | | ossClient.shutdown(); |
| | | // uri = uri.replace("http://nn-bucket.oss-cn-shanghai.aliyuncs.com",FILE_CDN); |
| | | return ApiResult.okmsg(uri); |
| | | } |
| | | |
| | | |
| | | @ApiOperation(value = "服务端签名后直传", notes = "服务端签名后直传") |
| | | @PostMapping("signature") |
| | | public ApiResult<Map<String, String>> signature() { |
| | | OSS builder = new OSSClientBuilder().build(AliOss.endpoint, AliOss.accessKeyId, AliOss.accessKeySecret); |
| | | long expireTime = 30; |
| | | long expireEndTime = System.currentTimeMillis() + expireTime * 1000; |
| | | Date expiration = new Date(expireEndTime); |
| | | PolicyConditions policyConds = new PolicyConditions(); |
| | | policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000); |
| | | policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, AliOss.dir); |
| | | String postPolicy = builder.generatePostPolicy(expiration, policyConds); |
| | | byte[] binaryData = postPolicy.getBytes(StandardCharsets.UTF_8); |
| | | String encodedPolicy = BinaryUtil.toBase64String(binaryData); |
| | | String postSignature = builder.calculatePostSignature(postPolicy); |
| | | Map<String, String> respMap = new LinkedHashMap<>(6); |
| | | respMap.put("accessid", AliOss.accessKeyId); |
| | | respMap.put("policy", encodedPolicy); |
| | | respMap.put("signature", postSignature); |
| | | // 示例为dir |
| | | respMap.put("dir", AliOss.dir); |
| | | // 前端为key |
| | | // respMap.put("key", AliOss.dir); |
| | | respMap.put("host", AliOss.host); |
| | | // respMap.put("cdn", AliOss.cdn); |
| | | respMap.put("expire", String.valueOf(expireEndTime / 1000)); |
| | | // // 回调 |
| | | // if (StringUtils.hasLength(AliOss.callbackUrl)) { |
| | | // JSONObject jasonCallback = new JSONObject(); |
| | | // jasonCallback.put("callbackUrl", AliOss.callbackUrl); |
| | | // String callbackBody = "{\"filename\":${object},\"size\":${size},\"mimeType\":${mimeType}}"; |
| | | // jasonCallback.put("callbackBody", callbackBody); |
| | | // jasonCallback.put("callbackBodyType", "application/json"); |
| | | // String base64CallbackBody = BinaryUtil.toBase64String(jasonCallback.toString().getBytes()); |
| | | // respMap.put("callback", base64CallbackBody); |
| | | // } |
| | | |
| | | HttpServletResponse response = WebUtils.response(); |
| | | assert response != null; |
| | | response.setHeader("Access-Control-Allow-Origin", "*"); |
| | | response.setHeader("Access-Control-Allow-Methods", "GET, POST"); |
| | | |
| | | // 关闭OSSClient。 |
| | | builder.shutdown(); |
| | | return ApiResult.success(respMap); |
| | | } |
| | | |
| | | @ApiOperation(value = "服务端上传(二进制字符串)", notes = "服务端上传") |
| | | @PostMapping(value = "str/upload") |
| | | public String strUpload(InputStream inputStream, String filename) throws IOException { |
| | | //String filename = System.currentTimeMillis() + CodeGenerateUtils.generateVolumeSn()+".jpg"; |
| | | // Endpoint以杭州为例,其它Region请按实际情况填写。 |
| | | final String endpoint = AliOss.endpoint; |
| | | // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。 |
| | | final String accessKeyId = AliOss.accessKeyId; |
| | | final String accessKeySecret = AliOss.accessKeySecret; |
| | | final String bucketName = AliOss.bucketName; |
| | | // <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。 |
| | | String objectName = AliOss.dir + filename; |
| | | // 创建OSSClient实例。 |
| | | OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
| | | // 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。 |
| | | // InputStream inputStream = new FileInputStream("D:\\localpath\\examplefile.txt"); |
| | | // 依次填写Bucket名称(例如examplebucket)和Object完整路径(例如exampledir/exampleobject.txt)。Object完整路径中不能包含Bucket名称。 |
| | | PutObjectResult putObjectResult = ossClient.putObject(bucketName, objectName, inputStream); |
| | | OSSObject ossObject = ossClient.getObject(bucketName, objectName); |
| | | String uri = ossObject.getResponse().getUri(); |
| | | // 关闭OSSClient。 |
| | | ossClient.shutdown(); |
| | | return uri; |
| | | } |
| | | |
| | | /** |
| | | * oss上传成功回调 |
| | | * |
| | | * @param callback |
| | | * @return |
| | | */ |
| | | @ApiOperation(value = "oss回调", notes = "oss回调") |
| | | @PostMapping(value = "/callback") |
| | | public ApiResult callback(@RequestBody Map<String, Object> callback) { |
| | | String filename = "http://".concat(AliOss.bucketName).concat(".").concat(AliOss.endpoint).concat("/").concat(callback.get("filename").toString()); |
| | | return ApiResult.okmsg(filename); |
| | | } |
| | | |
| | | } |
| | | //package com.jilongda.common.component; |
| | | // |
| | | // |
| | | //import com.aliyun.oss.OSS; |
| | | //import com.aliyun.oss.OSSClientBuilder; |
| | | //import com.aliyun.oss.common.utils.BinaryUtil; |
| | | //import com.aliyun.oss.model.MatchMode; |
| | | //import com.aliyun.oss.model.OSSObject; |
| | | //import com.aliyun.oss.model.PolicyConditions; |
| | | //import com.aliyun.oss.model.PutObjectResult; |
| | | //import com.jilongda.common.basic.ApiResult; |
| | | //import com.jilongda.common.utils.WebUtils; |
| | | //import io.swagger.annotations.Api; |
| | | //import io.swagger.annotations.ApiOperation; |
| | | //import lombok.extern.slf4j.Slf4j; |
| | | //import org.springframework.beans.factory.annotation.Value; |
| | | //import org.springframework.web.bind.annotation.*; |
| | | //import org.springframework.web.multipart.MultipartFile; |
| | | // |
| | | //import javax.servlet.http.HttpServletResponse; |
| | | //import java.io.File; |
| | | //import java.io.IOException; |
| | | //import java.io.InputStream; |
| | | //import java.nio.charset.StandardCharsets; |
| | | //import java.text.SimpleDateFormat; |
| | | //import java.util.Date; |
| | | //import java.util.LinkedHashMap; |
| | | //import java.util.Map; |
| | | // |
| | | // |
| | | ///** |
| | | // * @author feiyunchuan |
| | | // */ |
| | | //@Slf4j |
| | | //@Api(tags = "Ali-OSS文件上传接口") |
| | | //@RestController |
| | | //@RequestMapping("/ali-oss/") |
| | | //public class AliOssFileUploadController { |
| | | // |
| | | // @Value("${file.cdn:https://nncdn.pharmacylinked.com}") |
| | | // private String FILE_CDN; |
| | | // |
| | | // |
| | | //// @ApiOperation(value = "单文件上传", notes = "单文件上传,rename 默认不重命名") |
| | | //// @PostMapping(value = "strUpload", headers = "content-type=multipart/form-data") |
| | | //// public String strUploads(@RequestParam(value = "file") MultipartFile mf) throws IOException { |
| | | //// if (mf.isEmpty()) { |
| | | //// return "请传入文件!"; |
| | | //// } |
| | | //// String fileName = mf.getOriginalFilename(); |
| | | //// String TimeDir =new SimpleDateFormat("yyyy-MM-dd").format(new Date()); |
| | | ////// String realPath = "D:\\" + TimeDir; |
| | | //// String realPath = "/usr/local/nginx/html/images/" + TimeDir; |
| | | //// File file = new File(realPath); |
| | | //// // 没有目录就创建 |
| | | //// if (!file.exists()) { |
| | | //// file.mkdirs(); |
| | | //// } |
| | | //// File targetFile = new File(realPath, fileName);//目标文件 |
| | | //// //开始从源文件拷贝到目标文件 |
| | | //// //传图片一步到位 |
| | | //// mf.transferTo(targetFile); |
| | | //// //拼接数据 |
| | | //// return "http://eyes.chelota.com/images/" + TimeDir +"/"+ fileName; |
| | | //// } |
| | | // @ApiOperation(value = "服务端上传", notes = "服务端上传") |
| | | // @PostMapping(value = "upload") |
| | | // public ApiResult<String> fileUpload(@RequestParam(value = "file") MultipartFile file) throws IOException { |
| | | // InputStream inputStream = file.getInputStream(); |
| | | // String filename = System.currentTimeMillis() + file.getOriginalFilename(); |
| | | // // Endpoint以杭州为例,其它Region请按实际情况填写。 |
| | | // final String endpoint = AliOss.endpoint; |
| | | // // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。 |
| | | // final String accessKeyId = AliOss.accessKeyId; |
| | | // final String accessKeySecret = AliOss.accessKeySecret; |
| | | // final String bucketName = AliOss.bucketName; |
| | | // // <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。 |
| | | // String objectName = AliOss.dir + filename; |
| | | // // 创建OSSClient实例。 |
| | | // OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
| | | // // 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。 |
| | | // // InputStream inputStream = new FileInputStream("D:\\localpath\\examplefile.txt"); |
| | | // // 依次填写Bucket名称(例如examplebucket)和Object完整路径(例如exampledir/exampleobject.txt)。Object完整路径中不能包含Bucket名称。 |
| | | // PutObjectResult putObjectResult = ossClient.putObject(bucketName, objectName, inputStream); |
| | | // OSSObject ossObject = ossClient.getObject(bucketName, objectName); |
| | | // String uri = ossObject.getResponse().getUri(); |
| | | // // 关闭OSSClient。 |
| | | // ossClient.shutdown(); |
| | | //// uri = uri.replace("http://nn-bucket.oss-cn-shanghai.aliyuncs.com",FILE_CDN); |
| | | // return ApiResult.okmsg(uri); |
| | | // } |
| | | // |
| | | // |
| | | // @ApiOperation(value = "服务端签名后直传", notes = "服务端签名后直传") |
| | | // @PostMapping("signature") |
| | | // public ApiResult<Map<String, String>> signature() { |
| | | // OSS builder = new OSSClientBuilder().build(AliOss.endpoint, AliOss.accessKeyId, AliOss.accessKeySecret); |
| | | // long expireTime = 30; |
| | | // long expireEndTime = System.currentTimeMillis() + expireTime * 1000; |
| | | // Date expiration = new Date(expireEndTime); |
| | | // PolicyConditions policyConds = new PolicyConditions(); |
| | | // policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000); |
| | | // policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, AliOss.dir); |
| | | // String postPolicy = builder.generatePostPolicy(expiration, policyConds); |
| | | // byte[] binaryData = postPolicy.getBytes(StandardCharsets.UTF_8); |
| | | // String encodedPolicy = BinaryUtil.toBase64String(binaryData); |
| | | // String postSignature = builder.calculatePostSignature(postPolicy); |
| | | // Map<String, String> respMap = new LinkedHashMap<>(6); |
| | | // respMap.put("accessid", AliOss.accessKeyId); |
| | | // respMap.put("policy", encodedPolicy); |
| | | // respMap.put("signature", postSignature); |
| | | // // 示例为dir |
| | | // respMap.put("dir", AliOss.dir); |
| | | // // 前端为key |
| | | // // respMap.put("key", AliOss.dir); |
| | | // respMap.put("host", AliOss.host); |
| | | //// respMap.put("cdn", AliOss.cdn); |
| | | // respMap.put("expire", String.valueOf(expireEndTime / 1000)); |
| | | //// // 回调 |
| | | //// if (StringUtils.hasLength(AliOss.callbackUrl)) { |
| | | //// JSONObject jasonCallback = new JSONObject(); |
| | | //// jasonCallback.put("callbackUrl", AliOss.callbackUrl); |
| | | //// String callbackBody = "{\"filename\":${object},\"size\":${size},\"mimeType\":${mimeType}}"; |
| | | //// jasonCallback.put("callbackBody", callbackBody); |
| | | //// jasonCallback.put("callbackBodyType", "application/json"); |
| | | //// String base64CallbackBody = BinaryUtil.toBase64String(jasonCallback.toString().getBytes()); |
| | | //// respMap.put("callback", base64CallbackBody); |
| | | //// } |
| | | // |
| | | // HttpServletResponse response = WebUtils.response(); |
| | | // assert response != null; |
| | | // response.setHeader("Access-Control-Allow-Origin", "*"); |
| | | // response.setHeader("Access-Control-Allow-Methods", "GET, POST"); |
| | | // |
| | | // // 关闭OSSClient。 |
| | | // builder.shutdown(); |
| | | // return ApiResult.success(respMap); |
| | | // } |
| | | // |
| | | // @ApiOperation(value = "服务端上传(二进制字符串)", notes = "服务端上传") |
| | | // @PostMapping(value = "str/upload") |
| | | // public String strUpload(InputStream inputStream, String filename) throws IOException { |
| | | // //String filename = System.currentTimeMillis() + CodeGenerateUtils.generateVolumeSn()+".jpg"; |
| | | // // Endpoint以杭州为例,其它Region请按实际情况填写。 |
| | | // final String endpoint = AliOss.endpoint; |
| | | // // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。 |
| | | // final String accessKeyId = AliOss.accessKeyId; |
| | | // final String accessKeySecret = AliOss.accessKeySecret; |
| | | // final String bucketName = AliOss.bucketName; |
| | | // // <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。 |
| | | // String objectName = AliOss.dir + filename; |
| | | // // 创建OSSClient实例。 |
| | | // OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
| | | // // 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。 |
| | | // // InputStream inputStream = new FileInputStream("D:\\localpath\\examplefile.txt"); |
| | | // // 依次填写Bucket名称(例如examplebucket)和Object完整路径(例如exampledir/exampleobject.txt)。Object完整路径中不能包含Bucket名称。 |
| | | // PutObjectResult putObjectResult = ossClient.putObject(bucketName, objectName, inputStream); |
| | | // OSSObject ossObject = ossClient.getObject(bucketName, objectName); |
| | | // String uri = ossObject.getResponse().getUri(); |
| | | // // 关闭OSSClient。 |
| | | // ossClient.shutdown(); |
| | | // return uri; |
| | | // } |
| | | // |
| | | // /** |
| | | // * oss上传成功回调 |
| | | // * |
| | | // * @param callback |
| | | // * @return |
| | | // */ |
| | | // @ApiOperation(value = "oss回调", notes = "oss回调") |
| | | // @PostMapping(value = "/callback") |
| | | // public ApiResult callback(@RequestBody Map<String, Object> callback) { |
| | | // String filename = "http://".concat(AliOss.bucketName).concat(".").concat(AliOss.endpoint).concat("/").concat(callback.get("filename").toString()); |
| | | // return ApiResult.okmsg(filename); |
| | | // } |
| | | // |
| | | //} |