根据传入的地址获取网络文件大小

来源:互联网 发布:who生长曲线软件 编辑:程序博客网 时间:2024/05/16 06:30
/** * 根据传入的地址获取文件大小 *  * @param filePath *            网络文件路径 * @return */public static long getFileSize(String filePath) {long filesize = 0;URL url = null;ByteArrayOutputStream bos = new ByteArrayOutputStream();try {url = new URL(filePath);HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 利用HttpURLConnection对象,我们可以从网络中获取网页数据.filesize = conn.getContentLengthLong();if (filesize > 0) {filesize /= 1024;}bos.close();return filesize;} catch (MalformedURLException e) {logger.error("url错误!", e);} catch (IOException e) {logger.error("文件读写错误!", e);}return 0;}

0 0
原创粉丝点击