无法从URI中找到正确的扩展名。Web服务器可以欺骗您下载。链接可能会说它在哪里。jpg(符号链接)。
针对这个问题,如果url解析不起作用,您可以建立连接并使用getContentType等获取服务器设置的信息。
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();
// always check HTTP response code first
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "";
String disposition = httpConn.getHeaderField("Content-Disposition");
String contentType = httpConn.getContentType();
if (disposition != null) {
// extracts file name from header field
int index = disposition.indexOf("filename=");
if (index > 0) {
fileName = disposition.substring(index + 10,
disposition.length() - 1);
}
}
}
注意:代码未经测试。希望它能给你一个提示。