import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class TestSuit {
public static void main(String[] args) {
String mp3 = "ftp://static-bot.nearme.com.cn/1573713806068-postman.jpg";
try{
Map<String, Object> urlFileInfo = getUrlFileInfo(mp3);
System.out.println(urlFileInfo.get("size"));
System.out.println(urlFileInfo.get("imageWidth"));
System.out.println(urlFileInfo.get("imageHeight"));
System.out.println(urlFileInfo.get("fileSuffix"));
System.out.println(urlFileInfo.get("fileName"));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static Map<String,Object> getUrlFileInfo(String urlPath) throws Exception{
if(urlPath == null || urlPath.trim().length() == 0){
return null;
}
Map<String,Object> info = new HashMap<String,Object>(16);
try {
// first Exception
URL url = new URL(urlPath);
// second Exception --IOException
BufferedImage imageRead = ImageIO.read(url);
info.put("imageWidth",imageRead.getWidth());
info.put("imageHeight",imageRead.getHeight());
byte[] array = new byte[1024];
// third Exception --IOException
InputStream inputStream = url.openStream();
int size = 0;
int length = 0;
// fourth Exception --IOException
while ((length = inputStream.read(array)) != -1) {
size += length;
}
info.put("size",size);
int lastIndex =urlPath.lastIndexOf(".");
if(lastIndex < urlPath.length() - 1){
info.put("fileSuffix",urlPath.substring(lastIndex+1));
}
String fileString = url.getFile();
if(fileString.length()>0){
info.put("fileName",fileString.substring(0,fileString.lastIndexOf(".")));
}
return info;
} catch (MalformedURLException e) {
throw new Exception("url缺少通信协议,请检查url是否正确");
} catch (IOException e) {
throw new Exception("网络位置的文件不存在,请检查url是否正确");
}
}
}