|
| 1 | +package samples; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | + |
| 6 | +import com.aliyun.oss.ClientException; |
| 7 | +import com.aliyun.oss.OSSClient; |
| 8 | +import com.aliyun.oss.OSSException; |
| 9 | +import com.aliyun.oss.model.GetObjectRequest; |
| 10 | + |
| 11 | +/** |
| 12 | + * 断点续传下载用法示例 |
| 13 | + * |
| 14 | + */ |
| 15 | +public class ImageSample { |
| 16 | + |
| 17 | + private static String endpoint = "<endpoint, http://oss-cn-hangzhou.aliyuncs.com>"; |
| 18 | + private static String accessKeyId = "<accessKeyId>"; |
| 19 | + private static String accessKeySecret = "<accessKeySecret>"; |
| 20 | + private static String bucketName = "<bucketName>"; |
| 21 | + private static String key = "example.jpg"; |
| 22 | + |
| 23 | + |
| 24 | + public static void main(String[] args) throws IOException { |
| 25 | + |
| 26 | + OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret); |
| 27 | + |
| 28 | + try { |
| 29 | + // 缩放 |
| 30 | + String style = "image/resize,m_fixed,w_100,h_100"; |
| 31 | + GetObjectRequest request = new GetObjectRequest(bucketName, key); |
| 32 | + request.setProcess(style); |
| 33 | + |
| 34 | + ossClient.getObject(request, new File("example-resize.jpg")); |
| 35 | + |
| 36 | + // 裁剪 |
| 37 | + style = "image/crop,w_100,h_100,x_100,y_100,r_1"; |
| 38 | + request = new GetObjectRequest(bucketName, key); |
| 39 | + request.setProcess(style); |
| 40 | + |
| 41 | + ossClient.getObject(request, new File("example-crop.jpg")); |
| 42 | + |
| 43 | + // 旋转 |
| 44 | + style = "image/rotate,90"; |
| 45 | + request = new GetObjectRequest(bucketName, key); |
| 46 | + request.setProcess(style); |
| 47 | + |
| 48 | + ossClient.getObject(request, new File("example-rotate.jpg")); |
| 49 | + |
| 50 | + // 锐化 |
| 51 | + style = "image/sharpen,100"; |
| 52 | + request = new GetObjectRequest(bucketName, key); |
| 53 | + request.setProcess(style); |
| 54 | + |
| 55 | + ossClient.getObject(request, new File("example-sharpen.jpg")); |
| 56 | + |
| 57 | + // 水印 |
| 58 | + style = "image/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ"; |
| 59 | + request = new GetObjectRequest(bucketName, key); |
| 60 | + request.setProcess(style); |
| 61 | + |
| 62 | + ossClient.getObject(request, new File("example-watermark.jpg")); |
| 63 | + |
| 64 | + // 格式转换 |
| 65 | + style = "image/format,png"; |
| 66 | + request = new GetObjectRequest(bucketName, key); |
| 67 | + request.setProcess(style); |
| 68 | + |
| 69 | + ossClient.getObject(request, new File("example-format.png")); |
| 70 | + |
| 71 | + // 图片信息 |
| 72 | + style = "image/info"; |
| 73 | + request = new GetObjectRequest(bucketName, key); |
| 74 | + request.setProcess(style); |
| 75 | + |
| 76 | + ossClient.getObject(request, new File("example-info.txt")); |
| 77 | + |
| 78 | + } catch (OSSException oe) { |
| 79 | + System.out.println("Caught an OSSException, which means your request made it to OSS, " |
| 80 | + + "but was rejected with an error response for some reason."); |
| 81 | + System.out.println("Error Message: " + oe.getErrorCode()); |
| 82 | + System.out.println("Error Code: " + oe.getErrorCode()); |
| 83 | + System.out.println("Request ID: " + oe.getRequestId()); |
| 84 | + System.out.println("Host ID: " + oe.getHostId()); |
| 85 | + } catch (ClientException ce) { |
| 86 | + System.out.println("Caught an ClientException, which means the client encountered " |
| 87 | + + "a serious internal problem while trying to communicate with OSS, " |
| 88 | + + "such as not being able to access the network."); |
| 89 | + System.out.println("Error Message: " + ce.getMessage()); |
| 90 | + } catch (Throwable e) { |
| 91 | + e.printStackTrace(); |
| 92 | + } finally { |
| 93 | + ossClient.shutdown(); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments