获取远程服务器文件的两种方法

方式1

import java.net.HttpURLConnection;
import java.net.URL;
//创建url;
URL url = new URL(filepath);
//创建url连接;
HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
//链接远程服务器;
urlconn.connect();
BufferedInputStream  bis = null;
try {
	bis = new BufferedInputStream(urlconn.getInputStream() );
} catch (FileNotFoundException e) {
	e.printStackTrace();
} catch (IOException e) {
	e.printStackTrace();
} finally {
	urlconn.disconnect();//记得关闭链接
	try {
		bis.close();
	} catch (IOException e) {
		e.printStackTrace(); 
	}
}

方式2

//创建url;
URL url = new URL(fileUrl);
InputStream inputStream=null;
try {
	inputStream=url.openStream();
} catch (FileNotFoundException e) {
	e.printStackTrace();
} catch (IOException e) {
	e.printStackTrace();
} finally {
	try {
		inputStream.close();
	} catch (IOException e) {
		e.printStackTrace(); 
	}
}

参考链接:
https://blog.csdn.net/i_huli/article/details/80256228
https://blog.csdn.net/qinjingkai/article/details/83336109

你可能感兴趣的:(获取远程服务器文件的两种方法)