/** * 获取文件后缀,本地和网络. * * @param url 文件地址 * @param response the response * @return 文件后缀 */ public static String getMIMEFromUrl(String url, HttpResponse response) { if (AbStrUtil.isEmpty(url)) { return null; } String mime = null; try { // 获取后缀 if (url.lastIndexOf(".") != -1) { mime = url.substring(url.lastIndexOf(".")); if (mime.indexOf("/") != -1 || mime.indexOf("?") != -1 || mime.indexOf("&") != -1) { mime = null; } } if (AbStrUtil.isEmpty(mime)) { // 获取文件名 这个效率不高 String fileName = getRealFileName(response); if (fileName != null && fileName.lastIndexOf(".") != -1) { mime = fileName.substring(fileName.lastIndexOf(".")); } } } catch (Exception e) { e.printStackTrace(); } return mime; }
/** * 获取文件后缀,本地. * * @param url 文件地址 * @param connection the connection * @return 文件后缀 */ public static String getMIMEFromUrl(String url, HttpURLConnection connection) { if (AbStrUtil.isEmpty(url)) { return null; } String suffix = null; try { // 获取后缀 if (url.lastIndexOf(".") != -1) { suffix = url.substring(url.lastIndexOf(".")); if (suffix.indexOf("/") != -1 || suffix.indexOf("?") != -1 || suffix.indexOf("&") != -1) { suffix = null; } } if (AbStrUtil.isEmpty(suffix)) { // 获取文件名 这个效率不高 String fileName = getRealFileName(connection); if (fileName != null && fileName.lastIndexOf(".") != -1) { suffix = fileName.substring(fileName.lastIndexOf(".")); } } } catch (Exception e) { e.printStackTrace(); } return suffix; }
/** * 描述:通过文件的网络地址从SD卡中读取图片,如果SD中没有则自动下载并保存. * * @param url 文件的网络地址 * @param type 图片的处理类型(剪切或者缩放到指定大小,参考AbImageUtil类) 如果设置为原图,则后边参数无效,得到原图 * @param desiredWidth 新图片的宽 * @param desiredHeight 新图片的高 * @return Bitmap 新图片 */ public static Bitmap getBitmapFromSD(String url, int type, int desiredWidth, int desiredHeight) { Bitmap bitmap = null; try { if (AbStrUtil.isEmpty(url)) { return null; } // SD卡不存在 或者剩余空间不足了就不缓存到SD卡了 if (!isCanUseSD() || freeSdSpaceNeededToCache < freeSpaceOnSD()) { bitmap = getBitmapFromURL(url, type, desiredWidth, desiredHeight); return bitmap; } // 下载文件,如果不存在就下载,存在直接返回地址 String downFilePath = downloadFile(url, imageDownloadDir); if (downFilePath != null) { // 获取图片 return getBitmapFromSD(new File(downFilePath), type, desiredWidth, desiredHeight); } else { return null; } } catch (Exception e) { e.printStackTrace(); } return bitmap; }
/** * 获取文件名(.后缀),外链模式和通过网络获取. * * @param url 文件地址 * @param connection the connection * @return 文件名 */ public static String getCacheFileNameFromUrl(String url, HttpURLConnection connection) { if (AbStrUtil.isEmpty(url)) { return null; } String name = null; try { // 获取后缀 String suffix = getMIMEFromUrl(url, connection); if (AbStrUtil.isEmpty(suffix)) { suffix = ".ab"; } name = AbMd5.MD5(url) + suffix; } catch (Exception e) { e.printStackTrace(); } return name; }
/** * 获取文件名(不含后缀). * * @param url 文件地址 * @return 文件名 */ public static String getCacheFileNameFromUrl(String url) { if (AbStrUtil.isEmpty(url)) { return null; } String name = null; try { name = AbMd5.MD5(url); } catch (Exception e) { e.printStackTrace(); } return name; }
/** * 获取文件名,通过网络获取. * * @param url 文件地址 * @return 文件名 */ public static String getRealFileNameFromUrl(String url) { String name = null; try { if (AbStrUtil.isEmpty(url)) { return name; } URL mUrl = new URL(url); HttpURLConnection mHttpURLConnection = (HttpURLConnection) mUrl.openConnection(); mHttpURLConnection.setConnectTimeout(5 * 1000); mHttpURLConnection.setRequestMethod("GET"); mHttpURLConnection.setRequestProperty( "Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); mHttpURLConnection.setRequestProperty("Accept-Language", "zh-CN"); mHttpURLConnection.setRequestProperty("Referer", url); mHttpURLConnection.setRequestProperty("Charset", "UTF-8"); mHttpURLConnection.setRequestProperty("User-Agent", ""); mHttpURLConnection.setRequestProperty("Connection", "Keep-Alive"); mHttpURLConnection.connect(); if (mHttpURLConnection.getResponseCode() == 200) { for (int i = 0; ; i++) { String mine = mHttpURLConnection.getHeaderField(i); if (mine == null) { break; } if ("content-disposition".equals(mHttpURLConnection.getHeaderFieldKey(i).toLowerCase())) { Matcher m = Pattern.compile(".*filename=(.*)").matcher(mine.toLowerCase()); if (m.find()) return m.group(1).replace("\"", ""); } } } } catch (Exception e) { e.printStackTrace(); AbLogUtil.e(AbFileUtil.class, "网络上获取文件名失败"); } return name; }