Preparatory work:
1. Apply for OSS you will get Endpoint,accesskeyid,bucketname,accesskeysecret
2. Introduction of OSS maven dependency:com.aliyun.oss:aliyun-sdk-oss:jar:2.5.0
3. Create Ossclient
@Bean
Public Ossclient createossclient () {
Ossclient client = new Ossclient (endpoint, Accesskeyid, Accesskeysecret);
return client;
}
4. Upload file:
InputStream is=new bytearrayinputstream (bytes);
Logger.info ("Key is:" + key+ "corresponds to Bucket:" +bucketname);
Putobjectresult result = Ossclient.putobject (Bucketname, Key, is);
Logger.info (json.tojsonstring (result));
5. Download the file:
Download pictures
Ossobject ossobject = Ossclient.getobject (Bucketname, Fileid);
Response.setcharacterencoding ("Utf-8");
Sets the file encoding returned
Response.setcontenttype ("Image/png;image/gif;image/jpeg;image/bmp");
try {
Response.getoutputstream (). Write (Ioutils.tobytearray (Ossobject.getobjectcontent ()));
catch (IOException e) {
Logger.error ("File download Error", E);
throw new Campusguardexception ("File download Error");
}