Java获取网页内容时遇‘cannot find symbol - variable IOUtils’问题求助
Hey there, let's fix those errors step by step!
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.jarfile from the official Apache Commons IO distribution - Create a
libfolder 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
- IntelliJ: Right-click the JAR →
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

