自己写的工具类,分享一下。
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- public class DownloadImageUtil {
- public static byte[] getPicBytes(String goodsPic) {
- String picUrl = "D:\\Tomcat6.0\\apache-tomcat-6.0.39\\webapps\\UsedMallMinaServer\\images\\goodsPhotos\\"
- + goodsPic;
- // 创建文件
- File picFile = new File(picUrl);
- // 利用文件流读取图片文件
- InputStream is;
- byte[] picBytes = null;
- try {
- is = new FileInputStream(picFile);
- int len = (int) picFile.length();
- // 创建一个字节数组
- picBytes = new byte[len];
- int offset = 0;
- int numRead = 0;
- // 将文件流转换成字节数组
- while (offset < picBytes.length
- && ((numRead = is.read(picBytes, offset, len)) >= 0)) {
- offset += numRead;
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return picBytes;
- }
- }
复制代码
|
|