| | |
| | | package com.stylefeng.guns.modular.api; |
| | | |
| | | import com.aliyun.oss.OSS; |
| | | import com.aliyun.oss.OSSClient; |
| | | import com.aliyun.oss.OSSClientBuilder; |
| | | import com.aliyun.oss.model.OSSObject; |
| | | import com.aliyun.oss.model.ObjectMetadata; |
| | | import com.aliyun.oss.model.PutObjectRequest; |
| | | import com.aliyun.oss.model.PutObjectResult; |
| | | import com.stylefeng.guns.modular.file.OSSService; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.util.StreamUtils; |
| | | 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; |
| | | |
| | | @RestController |
| | | @RequestMapping("") |
| | | public class FileController { |
| | | private static final String endpoint = "oss-cn-hongkong.aliyuncs.com"; |
| | | private static final String accessKeyId = "LTAI5tQZzqsZYX5gw8yRNchQ"; |
| | | private static final String accessKeySecret = "5yJVdXwRzwPZwKKXp07lRAc7tkTxQp"; |
| | | private static final String bucketName = "bizuphk"; |
| | | @PostMapping("/base/files/upload") |
| | | @ApiOperation(value = "文件上传", tags = {"文件上传"}) |
| | | public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException { |
| | | // 获取原始文件名 |
| | | String originalFilename = file.getOriginalFilename(); |
| | | // 提取文件后缀 |
| | | if (originalFilename != null && originalFilename.contains(".")) { |
| | | System.err.println(originalFilename.substring(originalFilename.lastIndexOf("."))); |
| | | } |
| | | |
| | | // 创建 OSSClient 实例 |
| | | OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret); |
| | | long l = System.currentTimeMillis(); |
| | | // 创建 PutObjectRequest 对象 |
| | | PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, String.valueOf(l)+"."+originalFilename.substring(originalFilename.lastIndexOf(".") + 1), file.getInputStream()); |
| | | ObjectMetadata metadata = new ObjectMetadata(); |
| | | // 取消文件缓存,文件每次都会从OSS服务器获取 |
| | | metadata.setHeader("Cache-Control", "no-cache"); |
| | | metadata.setHeader("Expires", "0"); |
| | | // 上传文件 |
| | | PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest); |
| | | // 关闭 OSSClient 实例 |
| | | ossClient.shutdown(); |
| | | String fileUrl = "https://" + bucketName + "." + |
| | | endpoint + "/" |
| | | + String.valueOf(l) |
| | | +"."+originalFilename.substring(originalFilename.lastIndexOf(".")+ 1); |
| | | return fileUrl; |
| | | } |
| | | |
| | | // @ApiOperation(value = "服务端上传", notes = "服务端上传") |
| | | // @PostMapping(value = "upload") |
| | | // public String fileUpload(@RequestParam(value = "file") MultipartFile file) throws IOException { |
| | | // InputStream inputStream = file.getInputStream(); |
| | | // String filename = System.currentTimeMillis() + file.getOriginalFilename(); |
| | | // // Endpoint以杭州为例,其它Region请按实际情况填写。 |
| | | // final String endpoint = "oss-cn-hongkong.aliyuncs.com"; |
| | | // // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。 |
| | | // final String accessKeyId = "LTAI5tQZzqsZYX5gw8yRNchQ"; |
| | | // final String accessKeySecret = "5yJVdXwRzwPZwKKXp07lRAc7tkTxQp"; |
| | | // final String bucketName = "bizuphk"; |
| | | // // <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 uri; |
| | | // } |
| | | // @GetMapping("/base/files/download/{fileName}") |
| | | // @ApiOperation(value = "文件下载", tags = {"文件下载"}) |
| | | // public void downloadFile(@PathVariable("fileName") String fileName, HttpServletResponse response) { |
| | | // InputStream inputStream = ossService.downloadFile(fileName); |
| | | // if (inputStream != null) { |
| | | // try { |
| | | // StreamUtils.copy(inputStream, response.getOutputStream()); |
| | | // response.setContentType("application/octet-stream"); |
| | | // response.flushBuffer(); |
| | | // } catch (IOException e) { |
| | | // // 处理下载失败的逻辑 |
| | | // } |
| | | // } else { |
| | | // // 处理文件不存在的逻辑 |
| | | // } |
| | | // } |
| | | } |