| | |
| | | import com.aliyun.oss.OSSClient; |
| | | 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.core.common.SnowflakeIdWorker; |
| | | import com.core.exception.CoolException; |
| | | import lombok.Data; |
| | |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.core.io.InputStreamResource; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.InputStream; |
| | | import java.io.*; |
| | | import java.net.URL; |
| | | import java.nio.file.Path; |
| | | import java.util.Date; |
| | |
| | | * 上传OSS文件 |
| | | * @return the url |
| | | */ |
| | | @Deprecated |
| | | public String upload(InputStream inputStream, String suffix){ |
| | | if(suffix.trim().equals("file")){ |
| | | suffix="jpg"; |
| | |
| | | } |
| | | throw new CoolException("500-上传失败"); |
| | | } |
| | | |
| | | @Deprecated |
| | | public String upload(InputStream inputStream, String suffix,String contentType){ |
| | | if(suffix.trim().equals("file")){ |
| | | suffix="jpg"; |
| | |
| | | throw new CoolException("500-上传失败"); |
| | | } |
| | | |
| | | @Deprecated |
| | | public String upload(InputStream inputStream) { |
| | | return upload(inputStream, ".jpg"); |
| | | } |
| | | |
| | | /** |
| | | * 文件上传 |
| | | * @param filePath OSS路径 |
| | | * @param file 文件File |
| | | */ |
| | | public boolean uploadFile(String filePath, File file) { |
| | | OSSClient ossClient = new OSSClient(OSS_ENDPOINT, accessKeyId, accessKeySecret); |
| | | // 创建PutObjectRequest对象。 |
| | | PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, filePath, file); |
| | | // 上传文件。 |
| | | PutObjectResult result = ossClient.putObject(putObjectRequest); |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 下载文件 |
| | | * @param filePath OSS路径 |
| | | */ |
| | | public ResponseEntity<InputStreamResource> downloadFile(String filePath) throws IOException { |
| | | OSSClient ossClient = new OSSClient(OSS_ENDPOINT, accessKeyId, accessKeySecret); |
| | | OSSObject ossObject = ossClient.getObject(bucket, filePath); |
| | | InputStream inputStream = ossObject.getObjectContent(); |
| | | |
| | | // 创建响应实体,将输出流作为文件内容返回 |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); |
| | | headers.setContentDispositionFormData("attachment", "output.docx"); |
| | | InputStreamResource resource = new InputStreamResource(convertToByteArrayInputStream(inputStream)); |
| | | return ResponseEntity.ok() |
| | | .headers(headers) |
| | | .body(resource); |
| | | } |
| | | |
| | | public static ByteArrayInputStream convertToByteArrayInputStream(InputStream inputStream) throws IOException { |
| | | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| | | byte[] buffer = new byte[4096]; |
| | | int bytesRead; |
| | | while ((bytesRead = inputStream.read(buffer)) != -1) { |
| | | outputStream.write(buffer, 0, bytesRead); |
| | | } |
| | | byte[] data = outputStream.toByteArray(); |
| | | return new ByteArrayInputStream(data); |
| | | } |
| | | |
| | | /** |
| | |
| | | // } |
| | | // } |
| | | |
| | | @Deprecated |
| | | public String download(String name) { |
| | | OSSClient ossClient = new OSSClient(OSS_ENDPOINT, accessKeyId, accessKeySecret); |
| | | try { |