我给了一个来自谷歌驱动器直接下载链接的图片的URI,我想找到返回的文件的文件扩展名,我在Java中需要做什么。
例如,链接将像:https://drive.google.com/uc?export=download&id=0BwtDpsO0CtJZbm9iNUNMTXNTX0k
`
public static String saveImage(String imageUrl, String playlist) throws
IOException {
URL url = new URL(imageUrl);
String fileName = url.getFile();
System.out.println(fileName);
String destName
="./figures"+fileName.substring(fileName.lastIndexOf("/"));
String destName = "../imgUpload/"+playlist;System.out.println(destName);
InputStream is = url.openStream();System.out.println("is-
1"+is);OutputStream os = new FileOutputStream(destName);
System.out.println("is"+is);
byte[] b = new byte[2048];int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();
return destName;
}
`
O / P: -
fileName - / uc?export = download&id = 0BwtDpsO0CtJZbm9i UNMTXNTX 0k fileName-extract from link- / uc?export = download&id = 0BwtDpsO0CtJZbm9iNUNMTXNTX0k
对于您的示例网址,有一个重定向到另一个网址,但是对该网址的请求会返回一个带有标头content-type
的响应。对于你的例子,它是image/jpeg
。