直接上代码!
public class Sample {
private static final Log log = LogFactory.getLog(Sample.class);
// ----------------------------------------
static String host = "bcs.duapp.com";
static String accessKey = "XXXXXXXXXXXXXXXXXX"; //注册百度的时候,分配的accessKey
static String secretKey = "XXXXXXXXXXXXXXXXXXXXXXXX"; //注册百度的时候,分配的secretKey
static String bucket = "XXXbucket"; //在百度平台建立的bucket
public static void main(String[] args) throws URISyntaxException,
IOException {
// TODO Auto-generated method stub BCSCredentials
BCSCredentials credentials = new BCSCredentials(accessKey, secretKey);
BaiduBCS baiduBCS = new BaiduBCS(credentials, host); // baiduBCS.setDefaultEncoding("GBK");
baiduBCS.setDefaultEncoding("UTF-8"); // Default UTF-8
try {
String object = "/net_File"; //上传到百度云存储上面的文件名称
putObjectByFile(baiduBCS, object);
} catch (BCSServiceException e) {
log.warn("Bcs return:" + e.getBcsErrorCode() + ", "
+ e.getBcsErrorMessage() + ", RequestId="
+ e.getRequestId());
} catch (BCSClientException e) {
e.printStackTrace();
}
}
public static void inputstreamtofile(InputStream ins, File file)
throws IOException {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
}
public static void putObjectByFile(BaiduBCS baiduBCS, String object)
throws IOException {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("image/jpeg");
InputStream in = null;
URL url = new URL(http://p2.qhimg.com/t014aca5713dd7b7474.jpg); //从网上解析图片
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream();
Thumbnails.of(inStream).size(100, 100).toFile("upload/test.jpg"); 生成缩略图片
File file = new File("upload/test.jpg"); //暂时保存本地
byte[] buffer_data = getBytes("upload/test.jpg"); //加载本地缩略图片
in = ByteToInputStream(buffer_data,in);
metadata.setContentLength(buffer_data.length);
metadata.setContentLength(buffer_data.length);
PutObjectRequest request = new PutObjectRequest(bucket, object, in,
metadata);
request.setMetadata(metadata);
BaiduBCSResponse<ObjectMetadata> response = baiduBCS.putObject(request);
ObjectMetadata objectMetadata = response.getResult();
log.info("x-bs-request-id: " + response.getRequestId());
log.info(objectMetadata);
file.delete(); //删除本地图片
}
public static byte[] getBytes(String filePath) {
byte[] buffer = null;
try {
File file = new File(filePath); // 得到文件的byte流....
System.out.println(file.length());
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder();
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
public static InputStream ByteToInputStream(byte buffer[], InputStream in) {
in = new ByteArrayInputStream(buffer);
return in;
}
}