Skip to content

Commit 6a3091f

Browse files
committed
fix image
1 parent e2ee6f9 commit 6a3091f

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.aliyun.oss;
21+
22+
/**
23+
* <p>
24+
* 表示OSS端的数据与SDK端的数据不一致。
25+
* </p>
26+
*
27+
*
28+
* <p>
29+
* 通常来讲,调用者需要处理{@link InconsistentException}。因为该异常表明请求被服务处理,
30+
* 上传操作已经成功,但是OSS端的数据与SDK端不一致。调用者需要上传上传的文件然后重新删除。
31+
* </p>
32+
*
33+
* <p>
34+
* 抛出该异常的操作包括putObject、appendObject、uploadPart、uploadFile等上传操作,
35+
* getObject下载操作的数据一致性,调用者需要在数据流读取结束后校验,即比较OSS端与SDK端的数据校验和。
36+
* </p>
37+
*
38+
*/
39+
public class InconsistentException extends RuntimeException {
40+
41+
private static final long serialVersionUID = 2140587868503948665L;
42+
43+
private Long clientChecksum;
44+
private Long serverChecksum;
45+
private String requestId;
46+
47+
public InconsistentException(Long clientChecksum, Long serverChecksum, String requestId) {
48+
super();
49+
this.clientChecksum = clientChecksum;
50+
this.serverChecksum = serverChecksum;
51+
this.requestId = requestId;
52+
}
53+
54+
public Long getClientChecksum() {
55+
return clientChecksum;
56+
}
57+
58+
public void setClientChecksum(Long clientChecksum) {
59+
this.clientChecksum = clientChecksum;
60+
}
61+
62+
public Long getServerChecksum() {
63+
return serverChecksum;
64+
}
65+
66+
public void setServerChecksum(Long serverChecksum) {
67+
this.serverChecksum = serverChecksum;
68+
}
69+
70+
public String getRequestId() {
71+
return requestId;
72+
}
73+
74+
public void setRequestId(String requestId) {
75+
this.requestId = requestId;
76+
}
77+
78+
@Override
79+
public String getMessage() {
80+
return "InconsistentException " + "\n[RequestId]: " + getRequestId()
81+
+ "\n[ClientChecksum]: " + getClientChecksum()
82+
+ "\n[ServerChecksum]: " + getServerChecksum();
83+
}
84+
85+
}

src/samples/CRCSample.java

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)