我给了一个URI,它是从Google驱动器直接下载图像的链接,我想找到返回的文件的文件扩展名,我必须用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 = 0BwtDpsO0CtJZbm9iNUNMTXNTX0k 来自link- / uc?export = download&id = 0BwtDpsO0CtJZbm9iNUNMTXNTX0k的fileName-extrect
答案 0 :(得分:0)
对于您的示例URL,重定向到另一个URL,但是对该URL的请求返回带有标头content-type
的响应。对于您的示例,它是image/jpeg
。