I'm trying to download a file using the below code. I need to determine the filename and mime type of the file being download. The URL that I'm downloading from does not contain the filename. I've looked at the methods pertaining to the buffered streams used below and don't see any methods to do that.
con = (HttpURLConnection) url.openConnection();
//authenticate this request
if (passwordAuthentication != null) {
String auth = passwordAuthentication.getUserName()+":"+passwordAuthentication.getPassword();
con.setRequestProperty("Authentication", Base64.encodeBase64(auth.getBytes()).toString());
}
BufferedInputStream bis = new BufferedInputStream(con.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName()));
while ((i = bis.read()) != -1) {
bos.write(i);
}
bos.flush();
bis.close();
I see that I can get it via String contentType = con.getHeaderField("Content-Type");
, but that returns text/html
not the ContentType of the actual file being downloaded.
Edit: This is NOT a duplicate question, the other method does not provide a solution, described below.
I've now tried saving the file without a file extension and using MagicMatch match = Magic.getMagicMatch(file, true);
but it returns text/plain
. I'm guessing that's because I saved it without a file extension.