1、測試類
package test;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class test {
/**
* 測試
* @param args
*/
public static void main(String[] args) {
// 隨便一個網絡圖片地址
String urlpath = "https://teny-img.oss-cn-hongkong.aliyuncs.com/images/goods/5d42961629c65468.jpg";
// 得到數據
byte[] imageFromURL =getImageFromURL(urlpath);
// 轉換
String byte2kb = bytes2kb(imageFromURL.length);
//輸出
System.out.println("圖片的字節數:"+imageFromURL.length+"圖片的大小:"+byte2kb);
}
/**
* 根據圖片地址獲取圖片信息
* @param urlPath 網絡圖片地址
* @return
*/
public static byte[] getImageFromURL(String urlPath) {
// 字節數組
byte[] data = null;
// 輸入流
InputStream is = null;
// Http連接對象
HttpURLConnection conn = null;
try {
// Url對象
URL url = new URL(urlPath);
// 打開連接
conn = (HttpURLConnection) url.openConnection();
// 打開讀取 寫入是setDoOutput
// conn.setDoOutput(true);
conn.setDoInput(true);
// 設置請求方式
conn.setRequestMethod("GET");
// 設置超時時間
conn.setConnectTimeout(6000);
// 得到訪問的數據流
is = conn.getInputStream();
// 驗證訪問狀態是否是200 正常
if (conn.getResponseCode() == 200) {
data = readInputStream(is);
} else{
data=null;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(is != null){
// 關閉流
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
// 關閉連接
conn.disconnect();
}
return data;
}
/**
* 將流轉換爲字節
* @param is
* @return
*/
public static byte[] readInputStream(InputStream is) {
/**
* 捕獲內存緩衝區的數據,轉換成字節數組
* ByteArrayOutputStream類是在創建它的實例時,程序內部創建一個byte型別數組的緩衝區,然後利用ByteArrayOutputStream和ByteArrayInputStream的實例向數組中寫入或讀出byte型數據。
* 在網絡傳輸中我們往往要傳輸很多變量,我們可以利用ByteArrayOutputStream把所有的變量收集到一起,然後一次性把數據發送出去。
*/
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 創建字節數組 1024 = 1M
byte[] buffer = new byte[1024];
// 防止無限循環
int length = -1;
try {
// 循環寫入數據到字節數組
while ((length = is.read(buffer)) != -1) {
baos.write(buffer, 0, length);
}
// 強制刷新,掃尾工作,主要是爲了,讓數據流在管道的傳輸工程中全部傳輸過去,防止丟失數據
baos.flush();
} catch (IOException e) {
e.printStackTrace();
}
// 字節流轉換字節數組
byte[] data = baos.toByteArray();
try {
// 關閉讀取流
is.close();
// 關閉寫入流
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
/**
* 獲取本地圖片的字節數
* @param imgPath
* @return
*/
public static String pathSize(String imgPath) {
File file = new File(imgPath);
FileInputStream fis;
int fileLen = 0;
try {
fis = new FileInputStream(file);
fileLen = fis.available();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return bytes2kb(fileLen);
}
/**
* 將獲取到的字節數轉換爲KB,MB模式
* @param bytes
* @return
*/
public static String bytes2kb(long bytes){
BigDecimal filesize = new BigDecimal(bytes);
BigDecimal megabyte = new BigDecimal(1024 * 1024);
float returnValue = filesize.divide(megabyte, 2, BigDecimal.ROUND_UP).floatValue();
if(returnValue > 1)
return (returnValue + "MB");
BigDecimal kilobyte = new BigDecimal(1024);
returnValue = filesize.divide(kilobyte, 2, BigDecimal.ROUND_UP).floatValue();
return (returnValue + "KB");
}
}
2、結果
3、遇到困難可以評論(有信必回)小軒微信17382121839。