base64转码以及网络图片下载
2016-07-25 13:29
471 查看
import sun.misc.BASE64Encoder;
import java.io.File;
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.net.URLConnection;
/**
* @Title: imgMakeBase64 * @Description: 图片转base64码 * @param filePath 网络路径全名 * @param fName 文件名
* @return String 返回类型 * @author: Konanconan * Create at: 2015年11月13日 下午4:54:29 */ private String imgMakeBase64(String filePath,String fName){ Properties props = null; try { props = PropertiesLoaderUtils.loadAllProperties("configs.properties"); } catch (IOException e) { LoggerUtils.hr11LogInfo("配置文件configs.properties读取失败", e); } String path = (String) props.get("filepath"); String filePath1=""; try { filePath1 = download(filePath,fName,path+fName.substring(fName.lastIndexOf("/")+1,fName.length())); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } File imgFile = new File(filePath1);// 待处理的图片 InputStream in = null; byte[] data = null; // 读取图片字节数组 try { in = new FileInputStream(imgFile); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } File f = new File(filePath1); f.delete(); // 对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data);// 返回Base64编码过的字节数组字符串 } public String download(String urlString, String filename,String savePath) throws Exception { //根据String形式创建一个URL对象, URL url = new URL(urlString); //实列一个URLconnection对象,用来读取和写入此 URL 引用的资源 URLConnection con = url.openConnection(); //获取一个输入流 InputStream is = con.getInputStream(); //实列一个输出对象 FileOutputStream fos = new FileOutputStream(savePath); //一个byte[]数组,一次读取多个字节 byte[] bt = new byte[200]; //用来接收每次读取的字节个数 int b = 0; //循环判断,如果读取的个数b为空了,则is.read()方法返回-1,具体请参考InputStream的read(); while ((b = is.read(bt)) != -1) { //将对象写入到对应的文件中 fos.write(bt, 0, b); } //刷新流 fos.flush(); //关闭流 fos.close(); is.close(); return savePath; }
import java.io.File;
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.net.URLConnection;
/**
* @Title: imgMakeBase64 * @Description: 图片转base64码 * @param filePath 网络路径全名 * @param fName 文件名
* @return String 返回类型 * @author: Konanconan * Create at: 2015年11月13日 下午4:54:29 */ private String imgMakeBase64(String filePath,String fName){ Properties props = null; try { props = PropertiesLoaderUtils.loadAllProperties("configs.properties"); } catch (IOException e) { LoggerUtils.hr11LogInfo("配置文件configs.properties读取失败", e); } String path = (String) props.get("filepath"); String filePath1=""; try { filePath1 = download(filePath,fName,path+fName.substring(fName.lastIndexOf("/")+1,fName.length())); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } File imgFile = new File(filePath1);// 待处理的图片 InputStream in = null; byte[] data = null; // 读取图片字节数组 try { in = new FileInputStream(imgFile); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } File f = new File(filePath1); f.delete(); // 对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data);// 返回Base64编码过的字节数组字符串 } public String download(String urlString, String filename,String savePath) throws Exception { //根据String形式创建一个URL对象, URL url = new URL(urlString); //实列一个URLconnection对象,用来读取和写入此 URL 引用的资源 URLConnection con = url.openConnection(); //获取一个输入流 InputStream is = con.getInputStream(); //实列一个输出对象 FileOutputStream fos = new FileOutputStream(savePath); //一个byte[]数组,一次读取多个字节 byte[] bt = new byte[200]; //用来接收每次读取的字节个数 int b = 0; //循环判断,如果读取的个数b为空了,则is.read()方法返回-1,具体请参考InputStream的read(); while ((b = is.read(bt)) != -1) { //将对象写入到对应的文件中 fos.write(bt, 0, b); } //刷新流 fos.flush(); //关闭流 fos.close(); is.close(); return savePath; }
相关文章推荐
- 关于learntorank http://qiita.com/rockhopper/items/bb3d46f01df5f6499123
- IP地址、子网掩码、网络号、主机号、网络地址、主机地址以及ip段
- file_get_contents()获取https出现这个错误Unable to find the wrapper “https” – did
- Android网络技术
- tcpdump抓包规则常用命令
- 会话 cookie 中缺少HttpOnly 属性 的问题
- Torch7学习(三)——学习神经网络包的用法(1)
- okhttp 请求封装
- SFGantt 展示project网络图
- word2vec
- Linux 网络命令详解
- R语言与机器学习学习笔记(分类算法)(5)神经网络
- 《linux学习》之网络相关
- 99%的人都理解错了HTTP中GET与POST的区别
- 图论(网络流):[SCOI2015]小凸玩矩阵
- HTTP缓存机制
- TCP发送接口(如send(),write()等)的返回值与成功发送到接收端的数据量无直接关系 .
- 利用流计算实时检测网络威胁
- Retrofit 2.0,RxJava(Android), OkHttp3.3
- TCP/IP四层模型