如何在我的代码中使用Base64.java文件?
作者:互联网
我正在尝试这个
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpBasicAuth {
public static void downloadFileWithAuth(String urlStr, String user, String pass, String outFilePath) {
try {
// URL url = new URL ("http://ip:port/download_url");
URL url = new URL(urlStr);
String authStr = user + ":" + pass;
String authEncoded = Base64.encodeBytes(authStr.getBytes());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty("Authorization", "Basic " + authEncoded);
File file = new File(outFilePath);
InputStream in = (InputStream) connection.getInputStream();
OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
for (int b; (b = in.read()) != -1;) {
out.write(b);
}
out.close();
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
>正常,但显示错误“找不到符号错误Base64Encoder”
>下载了Base64.java文件
现在,我不知道如何在我的项目中使用此文件来消除错误.
您能告诉我如何使用Base64.java文件删除错误吗?
谢谢您的期待.
解决方法:
需要将Base64导入到您的代码中.导入取决于您的源文件.
Apache Commons Codec具有Base64的可靠实现.
例:
import org.apache.commons.codec.binary.Base64;
标签:base64,java,java-ee,http-authentication 来源: https://codeday.me/bug/20191201/2079267.html