/** * 获取真实文件名(xx.后缀),通过网络获取. * * @param connection 连接 * @return 文件名 */ public static String getRealFileName(HttpURLConnection connection) { String name = null; try { if (connection == null) { return name; } if (connection.getResponseCode() == 200) { for (int i = 0; ; i++) { String mime = connection.getHeaderField(i); if (mime == null) { break; } // "Content-Disposition","attachment; filename=1.txt" // Content-Length if ("content-disposition".equals(connection.getHeaderFieldKey(i).toLowerCase())) { Matcher m = Pattern.compile(".*filename=(.*)").matcher(mime.toLowerCase()); if (m.find()) { return m.group(1).replace("\"", ""); } } } } } catch (Exception e) { e.printStackTrace(); AbLogUtil.e(AbFileUtil.class, "网络上获取文件名失败"); } return name; }
/** * 描述:获取网络文件的大小. * * @param Url 图片的网络路径 * @return int 网络文件的大小 */ public static int getContentLengthFromUrl(String Url) { int mContentLength = 0; try { URL url = new URL(Url); HttpURLConnection mHttpURLConnection = (HttpURLConnection) url.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", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"); mHttpURLConnection.setRequestProperty("Connection", "Keep-Alive"); mHttpURLConnection.connect(); if (mHttpURLConnection.getResponseCode() == 200) { // 根据响应获取文件大小 mContentLength = mHttpURLConnection.getContentLength(); } } catch (Exception e) { e.printStackTrace(); AbLogUtil.d(AbFileUtil.class, "获取长度异常:" + e.getMessage()); } return mContentLength; }
/** * 描述:获取src中的图片资源. * * @param src 图片的src路径,如(“image/arrow.png”) * @return Bitmap 图片 */ public static Bitmap getBitmapFromSrc(String src) { Bitmap bit = null; try { bit = BitmapFactory.decodeStream(AbFileUtil.class.getResourceAsStream(src)); } catch (Exception e) { AbLogUtil.d(AbFileUtil.class, "获取图片异常:" + e.getMessage()); } return bit; }
/** * 描述:根据URL从互连网获取图片. * * @param url 要下载文件的网络地址 * @param type 图片的处理类型(剪切或者缩放到指定大小,参考AbConstant类) * @param desiredWidth 新图片的宽 * @param desiredHeight 新图片的高 * @return Bitmap 新图片 */ public static Bitmap getBitmapFromURL(String url, int type, int desiredWidth, int desiredHeight) { Bitmap bit = null; try { bit = AbImageUtil.getBitmap(url, type, desiredWidth, desiredHeight); } catch (Exception e) { AbLogUtil.d(AbFileUtil.class, "下载图片异常:" + e.getMessage()); } return bit; }
/** * 描述:获取Asset中的图片资源. * * @param context the context * @param fileName the file name * @return Drawable 图片 */ public static Drawable getDrawableFromAsset(Context context, String fileName) { Drawable drawable = null; try { AssetManager assetManager = context.getAssets(); InputStream is = assetManager.open(fileName); drawable = Drawable.createFromStream(is, null); } catch (Exception e) { AbLogUtil.d(AbFileUtil.class, "获取图片异常:" + e.getMessage()); } return drawable; }
/** * 描述:获取Asset中的图片资源. * * @param context the context * @param fileName the file name * @return Bitmap 图片 */ public static Bitmap getBitmapFromAsset(Context context, String fileName) { Bitmap bit = null; try { AssetManager assetManager = context.getAssets(); InputStream is = assetManager.open(fileName); bit = BitmapFactory.decodeStream(is); } catch (Exception e) { AbLogUtil.d(AbFileUtil.class, "获取图片异常:" + e.getMessage()); } return bit; }
/** * 获取真实文件名(xx.后缀),通过网络获取. * * @param response the response * @return 文件名 */ public static String getRealFileName(HttpResponse response) { String name = null; try { if (response == null) { return name; } // 获取文件名 Header[] headers = response.getHeaders("content-disposition"); for (int i = 0; i < headers.length; i++) { Matcher m = Pattern.compile(".*filename=(.*)").matcher(headers[i].getValue()); if (m.find()) { name = m.group(1).replace("\"", ""); } } } catch (Exception e) { e.printStackTrace(); AbLogUtil.e(AbFileUtil.class, "网络上获取文件名失败"); } 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; }
/** * 下载网络文件到SD卡中.如果SD中存在同名文件将不再下载 * * @param url 要下载文件的网络地址 * @param dirPath the dir path * @return 下载好的本地文件地址 */ public static String downloadFile(String url, String dirPath) { InputStream in = null; FileOutputStream fileOutputStream = null; HttpURLConnection connection = null; String downFilePath = null; File file = null; try { if (!isCanUseSD()) { return null; } // 先判断SD卡中有没有这个文件,不比较后缀部分比较 String fileNameNoMIME = getCacheFileNameFromUrl(url); File parentFile = new File(imageDownloadDir); File[] files = parentFile.listFiles(); for (int i = 0; i < files.length; ++i) { String fileName = files[i].getName(); String name = fileName.substring(0, fileName.lastIndexOf(".")); if (name.equals(fileNameNoMIME)) { // 文件已存在 return files[i].getPath(); } } URL mUrl = new URL(url); connection = (HttpURLConnection) mUrl.openConnection(); connection.connect(); // 获取文件名,下载文件 String fileName = getCacheFileNameFromUrl(url, connection); file = new File(imageDownloadDir, fileName); downFilePath = file.getPath(); if (!file.exists()) { file.createNewFile(); } else { // 文件已存在 return file.getPath(); } in = connection.getInputStream(); fileOutputStream = new FileOutputStream(file); byte[] b = new byte[1024]; int temp = 0; while ((temp = in.read(b)) != -1) { fileOutputStream.write(b, 0, temp); } } catch (Exception e) { e.printStackTrace(); AbLogUtil.e(AbFileUtil.class, "有文件下载出错了,已删除"); // 检查文件大小,如果文件为0B说明网络不好没有下载成功,要将建立的空文件删除 if (file != null) { file.delete(); } file = null; downFilePath = null; } finally { try { if (in != null) { in.close(); } } catch (Exception e) { e.printStackTrace(); } try { if (fileOutputStream != null) { fileOutputStream.close(); } } catch (Exception e) { e.printStackTrace(); } try { if (connection != null) { connection.disconnect(); } } catch (Exception e) { e.printStackTrace(); } } return downFilePath; }