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;
	}

上一篇:VUE前端实现PDF预览时出现org.apache.catalina.connector.ClientAbortException:java.io.IOException: 您的主机中的软件中止了一个


下一篇:skills_java