Crawl pictures from the network, generate thumbnails and save them to Baidu cloud storage!

Here is the programming house jb51 CC collects and arranges code fragments through the network.

Programming house Xiaobian now shares it with you and gives you a reference.

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,8192)) != -1) {
   os.write(buffer,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,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;
 }
}

The above is all the code content collected by the programming home (jb51. CC). I hope this article can help you solve the program development problems you encounter.

If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>