示例代码
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class ImageSample {
private static String objectName = "your_object";
private static String ak = "your_ak";
private static String sk = "your_sk";
private static final int CONN_TIMEOUT = 10000;
private static final int READ_TIMEOUT = 30000;
private static final String DATE_STR = "EEE, d MMM yyyy HH:mm:ss 'GMT'";
private static String bucket = "your_bucket";
private static String host = "oos-hz.ctyunapi.cn";
private static final SimpleDateFormat DATE_FMT = newSimpleDateFormat(DATE_STR, Locale.ENGLISH);
static {
TimeZonegmt = TimeZone.getTimeZone("GMT");
DATE_FMT.setTimeZone(gmt);
}
public static void main(String[] args) throws Exception {
ImageSample is = new ImageSample();
// 获取单边固定宽度的缩略图片,宽度为100,高度按比例处理
is.getObject(bucket, objectName, "@oosImage|100h", new File("single_side_width.jpg"));
// 获取单边固定宽度的缩略图片,强制缩略成宽度为100,高度为100
is.getObject(bucket, objectName, "@oosImage|100h_100w_2e", new File("force_width_height.jpg"));
// 按比例缩放图片,将图按比例缩小1/2
is.getObject(bucket, objectName, "@oosImage|50p", new File("reduce.jpg"));
//格式转换,将jpg保存成高度为100, 宽度为100的png格式
is.getObject(bucket, objectName, "@oosImage|100h_100w.png", new File("format_conversion.png"));
// 获取图片的基本信息和exif信息
is.getObject(bucket, objectName, "@oosImage|infoexif", new File("info.txt"));
// 获取文字水印图片,字体是文泉驿正黑,字体大小是20, 颜色是白色(#FFFFFF), 文字阴影是50, 文字水印内容是:Hello 图片服务!,水印位置是:右中,水平边距是:10,中线垂直偏移是:20
is.getObject(bucket, objectName, "@oosImage|watermark=2&type=d3F5LXplbmhlaQ&size=20&text=SGVsbG8g5Zu-54mH5pyN5YqhIQ&color=I0ZGRkZGRg&s=50&t=90&p=6&x=10&voffset=20", new File("wartermark.jpg"));
}
private String authorize(String httpVerb, String date, String bucket, String objectName) throws Exception {
String stringToSign = httpVerb + "\n\n\n" + date + "\n/" + bucket + "/" + objectName;
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(newSecretKeySpec(sk.getBytes("UTF-8"), "HmacSHA1"));
byte[] macResult = mac.doFinal(stringToSign.getBytes("UTF-8"));
String signature = newString(Base64.encodeBase64(macResult), "UTF-8");
String authorization = "AWS " + ak + ":" + signature;
return authorization;
}
public static void downloadInputStream(InputStreaminput, File file) throws IOException {
int c;
FileOutputStream fos = newFileOutputStream(file);
byte[] buff = newbyte[1024 * 4];
try {
while ((c = input.read(buff)) != -1) {
fos.write(buff, 0, c);
}
input.close();
fos.flush();
} finally {
fos.close();
}
}
public void getObject(String bucketName, String objectName, String imageParams, File destFile) throws Exception {
String date = DATE_FMT.format(new Date());
String authorization = authorize("GET", date, bucket, objectName + imageParams);
URL url = newURL("http", host, 80, "/" + bucket + "/" + objectName + imageParams);
HttpURLConnectionconn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setRequestProperty("Date", date);
conn.setRequestProperty("Authorization", authorization);
conn.setConnectTimeout(CONN_TIMEOUT);
conn.setReadTimeout(READ_TIMEOUT);
conn.setDoInput(true);
conn.setRequestMethod("GET");
conn.connect();
downloadInputStream(conn.getInputStream(), destFile);
}
}