编程语言
首页 > 编程语言> > java根据路径获取远程服务器文件大小

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,int,return,printStackTrace,fileSize,服务器,httpURLConnection
来源: https://blog.csdn.net/jungeCSND/article/details/120525684