为什么下面的代码会出现错误
线程“C:\temp>java”中的xxx异常:org/apache/ -cp /co dec/二进制/Base64 64 at Test.main(Test.java:22),原因是: java.lang.ClassNotFoundException: org.apache.commons.codec.binary.Bas e64 at Test.main at java.net.URLClassLoader$1.run(URLClassLoader )( java.security.AccessController.doPrivileged(Native方法)在java.net.URLClassLoader.findClass(URLClassLoader.java:354),在java.lang.ClassLoader.loadClass(ClassLoader.java:425),在sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308),在java.lang.ClassLoader.loadClass(ClassLoader.java:358) .
代码
import org.apache.commons.codec.binary.Base64;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class Test {
public static void main(String[] args) {
try {
String webPage = "xx";
String name = "xxx";
String password = "xx";
String authString = name + ":" + password;
System.out.println("auth string: " + authString);
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
System.out.println("Base64 encoded auth string: " + authStringEnc);
URL url = new URL(webPage);
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
InputStream is = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int numCharsRead;
char[] charArray = new char[1024];
StringBuffer sb = new StringBuffer();
while ((numCharsRead = isr.read(charArray)) > 0) {
sb.append(charArray, 0, numCharsRead);
}
String result = sb.toString();
System.out.println("*** BEGIN ***");
System.out.println(result);
System.out.println("*** END ***");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
发布于 2014-09-22 01:17:44
https://stackoverflow.com/questions/25970388
复制相似问题