urlconnection.getinputstream()
时间: 2023-05-01 14:03:44 浏览: 59
b'urlconnection.getinputstream()'的意思是获取URLConnection对象的输入流。该方法返回一个InputStream对象,可以通过它来读取URLConnection返回的数据。
相关问题
urlconnection.getinputstream() 报错,java.io.FileNotFoundException: 怎么解决
这个错误通常是由于指定的 URL 无效或文件在指定路径不存在引起的。你可以检查 URL 是否正确,以及确认文件是否存在,然后尝试在代码中处理这个异常,例如:
```java
try {
URL url = new URL("http://example.com/file.txt");
URLConnection urlConn = url.openConnection();
InputStream inputStream = urlConn.getInputStream();
// ...
} catch (FileNotFoundException e) {
// 处理文件不存在的情况
} catch (IOException e) {
// 处理其他 IO 异常
}
```
public List<SystemPort> getSystem(Integer pageNo, Integer length) { try { //编译 String encode = URLDecoder.decode(String.format(systemPort,pageNo,length), "UTF-8"); //发送 URL url = new URL(encode); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); //响应结果 缓存和加强性能 转为字节流 客户端:返回值 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); StringBuffer stringBuffer = new StringBuffer(); String inputLine; while ((inputLine = bufferedReader.readLine()) !=null){ stringBuffer.append(inputLine); } bufferedReader.close(); //转为集合对象 RegionResult regionResult = JSON.parseObject(stringBuffer.toString(), RegionResult.class); List<SystemPort> content = JSONArray.parseArray(JSON.toJSONString(regionResult.getData().get("content")), SystemPort.class); String totalElements = JSON.toJSONString(regionResult.getData().get("totalElements")); if(Integer.parseInt(totalElements)>content.size()){ getSystem(pageNo,Integer.parseInt(totalElements)); } return content; } catch (IOException e) { e.printStackTrace(); } return null;}
这段代码是一个Java方法,用于获取系统端口信息。该方法接收两个整数类型的参数pageNo和length,用于指定获取数据的起始页数和每页数据的长度。该方法通过HTTP协议向指定的URL发送GET请求,并解析响应结果。最终将解析结果转换为系统端口集合对象并返回。如果总元素数大于当前页的元素数,则会递归调用该方法获取所有数据。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)