/**
*
* @Title: getUrlFile
* @Description: 从服务器获取图片流文件
* @date 2018年6月26日下午2:15:32
*/
private InputStream getUrlFile(URL url){
InputStream inputStream = null;
HttpURLConnection httpURLConnection = null;
try {
httpURLConnection = (HttpURLConnection) url.openConnection();
// 设置网络连接超时时间
httpURLConnection.setConnectTimeout(3000);
// 设置应用程序要从网络连接读取数据
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET");
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == 200) {
// 从服务器返回一个输入流
inputStream = httpURLConnection.getInputStream();
}
} catch (MalformedURLException e) {
logger.error(e.getMessage(),e);
} catch (IOException e) {
logger.error(e.getMessage(),e);
}
return inputStream;
}
*
* @Title: getUrlFile
* @Description: 从服务器获取图片流文件
* @date 2018年6月26日下午2:15:32
*/
private InputStream getUrlFile(URL url){
InputStream inputStream = null;
HttpURLConnection httpURLConnection = null;
try {
httpURLConnection = (HttpURLConnection) url.openConnection();
// 设置网络连接超时时间
httpURLConnection.setConnectTimeout(3000);
// 设置应用程序要从网络连接读取数据
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET");
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == 200) {
// 从服务器返回一个输入流
inputStream = httpURLConnection.getInputStream();
}
} catch (MalformedURLException e) {
logger.error(e.getMessage(),e);
} catch (IOException e) {
logger.error(e.getMessage(),e);
}
return inputStream;
}
本文介绍了一种通过Java代码从服务器获取图片流的方法。具体实现包括使用HttpURLConnection打开连接、设置连接参数(如超时时间和请求方式)、检查响应码并获取输入流。
1710

被折叠的 条评论
为什么被折叠?



