代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
从网络爬取图片,生成缩略图,保存到百度云存储
作者:
CodeKingKong
/ 发布于
2015/1/12
/
779
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; } }
试试其它关键字
网络爬取图片
缩略图
百度云存储
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
CodeKingKong
贡献的其它代码
(
2
)
.
从网络爬取图片,生成缩略图,保存到百度云存储
.
HashMap的put方法
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3