我正在从 Facebook 检索帖子,我想将数据保存在数据库中,但对于图像和照片,我需要下载它们,但我需要确定图像的扩展名,但由于链接不同,因此使用拆分并非始终有用。有没有办法确定扩展名?
我的代码:
public static String DownLoadFile(String netUrl, String savePath, String name, String ext) {
try {
URL url = new URL(netUrl);
File file = new File(savePath, name + "." + ext);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
long startTime = System.currentTimeMillis();
Log.d("DownloadManager", "download begining");
Log.d("DownloadManager", "downloaded file name:" + name);
ByteArrayBuffer baf = new ByteArrayBuffer(5000);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
return file.getAbsolutePath();
} catch (Exception exx) {
if (exx.getMessage() != null) {
Log.w(Error_Tag, "Err3 = " + exx.getMessage());
} else {
Log.w(Error_Tag, "Err3 = " + exx.getMessage());
}
}
return null;
}