Search code examples
javafile-uploadjerseyimage-uploadingfileinputstream

How to get file extension from google drive direct download link?


I have given a URI which is from google drive direct download link of a image, and I want to find the file extension of the file that is returned, what do I have to do in Java.

For example Link will be like: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 fileName-extrect from link-/uc?export=download&id=0BwtDpsO0CtJZbm9iNUNMTXNTX0k


Solution

  • For your example URL there is a redirect to a different URL, but a request to that URL returns a response with the header content-type. For your example it is image/jpeg.