java根据路径获取远程服务器文件大小

2021/9/28 11:41:05

本文主要是介绍java根据路径获取远程服务器文件大小,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

/**
	 * 
	 * @description: 从服务器获得文件大小
	 * @author: Hj
	 * @return
	 */
	public static int  getFileSize(String urlPath) {
		int  fileSize = 0;
		HttpURLConnection httpURLConnection = null;
		try {
			URL url = new URL(urlPath);
			httpURLConnection = (HttpURLConnection) url.openConnection();
			httpURLConnection.setConnectTimeout(3000);
			httpURLConnection.setDoInput(true);
			httpURLConnection.setRequestMethod("GET");
			int responseCode = httpURLConnection.getResponseCode();
			if (responseCode == 200) {
				fileSize = httpURLConnection.getContentLength();
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return fileSize;
	}



这篇关于java根据路径获取远程服务器文件大小的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程