You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java获取网页内容时遇‘cannot find symbol - variable IOUtils’问题求助

Hey there, let's fix those errors step by step!

解决 'cannot find symbol IOUtils' 和 'package org.apache.commons.io does not exist' 错误

First, let's break down the root cause: IOUtils is a utility class from the Apache Commons IO third-party library. Your project currently doesn't have this library as a dependency—so even if you try to import the package, the compiler can't find the actual class files, leading to both errors. Here's how to fix it based on your project setup:

1. For Maven projects

Add the Commons IO dependency to your pom.xml file:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.15.1</version> <!-- Use the latest stable version available -->
</dependency>

After adding this, let Maven sync your dependencies. It will automatically download the library, and the package import error should disappear.

2. For Gradle projects

If you're using Groovy-based build.gradle:

implementation 'commons-io:commons-io:2.15.1'

For Kotlin DSL build.gradle.kts:

implementation("commons-io:commons-io:2.15.1")

Sync your Gradle project, and the dependency will be added to your build.

3. Manual JAR addition (no build tool)

  • Download the latest commons-io.jar file from the official Apache Commons IO distribution
  • Create a lib folder in your project (if it doesn't exist) and place the JAR inside it
  • In your IDE, add the JAR as a project library:
    • IntelliJ: Right-click the JAR → Add as Library
    • Eclipse: Right-click your project → Build Path → Add External Archives

Fixed code example

Once the dependency is set up correctly, make sure to import IOUtils properly, and also specify the encoding in IOUtils.toString() to avoid potential garbled text:

import org.apache.commons.io.IOUtils;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class WebContentFetcher {
    public static void main(String[] args) throws Exception {
        URL url = new URL("http://www.example.com/");
        URLConnection con = url.openConnection();
        InputStream in = con.getInputStream();
        String encoding = con.getContentEncoding();
        encoding = encoding == null ? "UTF-8" : encoding;
        // Add the encoding parameter to ensure correct text parsing
        String body = IOUtils.toString(in, encoding);
        System.out.println(body);
    }
}

Alternative: Use Java native API (no third-party library)

If you don't want to add an external dependency, you can achieve the same result with Java's built-in classes like BufferedReader:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;

public class WebContentFetcher {
    public static void main(String[] args) throws Exception {
        URL url = new URL("http://www.example.com/");
        URLConnection con = url.openConnection();
        String encoding = con.getContentEncoding();
        if (encoding == null) {
            encoding = StandardCharsets.UTF_8.name();
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), encoding));
        StringBuilder body = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            body.append(line).append("\n");
        }
        reader.close();
        System.out.println(body.toString());
    }
}

内容的提问来源于stack exchange,提问作者Miro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:28:36