gpt4 book ai didi

java - 从 URL java 中提取图像名称

转载 作者:行者123 更新时间:2023-11-30 07:33:43 25 4
gpt4 key购买 nike

最佳答案

首先我们要解码 URL 并获取 url 参数。我们可以使用 BalusC's great code for parsing query Strings 来做到这一点.然后我们只需将该 URL 拆分为 / 并获取最后一部分。

String url ="http://m.people.com/img/ic?width=474&height=269&fsize=999000&format=jpg&url=http%3A%2F%2Fimg2.timeinc.net%2Fpeople%2Fi%2F2011%2Fnews%2F110425%2Fgwyneth-paltrow-300.jpg";
Map<String, List<String>> params = new HashMap<String, List<String>>();
String[] urlParts = url.split("\\?");
if (urlParts.length > 1) {
String query = urlParts[1];
for (String param : query.split("&")) {
String[] pair = param.split("=");
String key;
try
{
key = URLDecoder.decode(pair[0], "UTF-8");
} catch (UnsupportedEncodingException e)
{
key = URLDecoder.decode(pair[0]);
}
String value;
try
{
value = URLDecoder.decode(pair[1], "UTF-8");
} catch (UnsupportedEncodingException e)
{
value = URLDecoder.decode(pair[1]);
}
List<String> values = params.get(key);
if (values == null) {
values = new ArrayList<String>();
params.put(key, values);
}
values.add(value);
}
}
String img = params.get("url").get(0);
String[] parts = img.split("/");
String imgName = parts[parts.length - 1];
System.out.println(imgName);

关于java - 从 URL java 中提取图像名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5666634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com