Java Base64Encoder 无法解析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6526883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Base64Encoder cannot be resolved
提问by arsenal
This is my Java code in a JSP file. I am getting
这是我在 JSP 文件中的 Java 代码。我正进入(状态
Base64Encoder cannot be resolved.
Base64Encoder 无法解析。
Why is it so? I have to add something related to Base64Encoder
. Any suggestions will be appreciated.
为什么会这样?我必须添加一些与Base64Encoder
. 任何建议将不胜感激。
<%@ page language="java" import="java.io.OutputStream,java.net.HttpURLConnection,java.net.URL,java.util.Collection,org.apache.commons.httpclient.Credentials,org.apache.commons.httpclient.auth.AuthenticationException,org.apache.commons.httpclient.auth.MalformedChallengeException,org.apache.commons.httpclient.params.DefaultHttpParams,org.apache.commons.httpclient.params.HttpParams,org.apache.commons.httpclient.auth.AuthScheme,org.apache.commons.httpclient.auth.AuthPolicy,org.apache.commons.httpclient.HttpClient,org.apache.commons.httpclient.UsernamePasswordCredentials,org.apache.commons.httpclient.auth.AuthScope,org.apache.commons.httpclient.methods.GetMethod,org.w3c.dom.*,javax.xml.parsers.DocumentBuilder,javax.xml.parsers.DocumentBuilderFactory,java.net.*,java.io.*" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%
String a_Url = request.getParameter( "url" ) ;
URL url = new URL (a_Url);
String encoding = Base64Encoder.encode ("test:test");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty ("Authorization", "Basic " + encoding);
InputStream content = (InputStream)connection.getInputStream();
BufferedReader in =
new BufferedReader (new InputStreamReader (content));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
%>
采纳答案by Sai
Looks like you are using a class that does not exist in a jar you have included in the web application. Can you try the following? Make adjustments if necessary, I am just looking at the documentation for commons and typing this out --
看起来您正在使用 Web 应用程序中包含的 jar 中不存在的类。您可以尝试以下方法吗?如有必要,请进行调整,我只是在查看公共文档并输入此内容-
- Go to http://commons.apache.org/codec/index.htmland read through the information there
- Now go to http://commons.apache.org/codec/download_codec.cgiand download the zip file
- Extract out the jar file and copy it to the lib directory of your web application
- Replace the line [String encoding = Base64Encoder.encode ("test:test");]
- 转到http://commons.apache.org/codec/index.html并阅读那里的信息
- 现在去http://commons.apache.org/codec/download_codec.cgi并下载 zip 文件
- 提取 jar 文件并将其复制到您的 Web 应用程序的 lib 目录中
- 替换行 [String encoding = Base64Encoder.encode("test:test");]
with
和
String encoding = new String(
org.apache.commons.codec.binary.Base64.encodeBase64
(org.apache.commons.codec.binary.StringUtils.getBytesUtf8("test:test"))
);
回答by Sai
You may need to do an import or specify the fully qualified class name for Base64Encoder
您可能需要导入或指定 Base64Encoder 的完全限定类名
回答by Kristofer Hoch
I don't see an inclusion of the namespace here for Base64Encoder. Try adding 'com.oreilly.servlet' to your import.
我没有看到此处包含 Base64Encoder 的命名空间。尝试将“com.oreilly.servlet”添加到您的导入中。
回答by Greg Chappell
I suspect you're not using a standalone JRE instead of the one included in the JDK.
我怀疑您没有使用独立的 JRE 而不是 JDK 中包含的 JRE。
- Right click your project and click "Build Path" -> "Configure Build Path"
- Under "Libraries", click on the existing JRE and then click "Remove"
- Click "Add Library" -> "JRE System Library" -> "Finish"
- 右键单击您的项目,然后单击“构建路径”->“配置构建路径”
- 在“库”下,单击现有的 JRE,然后单击“删除”
- 点击“添加库”->“JRE系统库”->“完成”
The class should now resolve.
该类现在应该解决。