1
我想從這個專有使用編碼阿帕奇百科全書
private String getSecWebSocketAccept(String secKey)
throws UnsupportedEncodingException, NoSuchAlgorithmException {
String guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
secKey += guid;
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(secKey.getBytes("ISO-8859-1"), 0, secKey.length());
byte[] shalHash = md.digest();
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(shalHash);
}
編碼改爲Apache的版本
org.apache.commons.codec.binary.Base64
但它只是不會工作,當我嘗試
private String getSecWebSocketAccept(String secKey)
throws UnsupportedEncodingException, NoSuchAlgorithmException {
String guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
secKey += guid;
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(secKey.getBytes("ISO-8859-1"), 0, secKey.length());
byte[] shalHash = md.digest();
org.apache.commons.codec.binary.Base64.encodeBase64 encoder = new org.apache.commons.codec.binary.Base64.encodeBase64();
return encoder.encode(shalHash);
}
這是錯誤我得到
C:\xampp\htdocs\html>javac SimpleServer.java
SimpleServer.java:87: error: cannot find symbol
org.apache.commons.codec.binary.Base64.encodeBase64 encoder = ne
w org.apache.commons.codec.binary.Base64.encodeBase64();
^
symbol: class encodeBase64
location: class Base64
SimpleServer.java:87: error: cannot find symbol
org.apache.commons.codec.binary.Base64.encodeBase64 encoder = ne
w org.apache.commons.codec.binary.Base64.encodeBase64();
^
symbol: class encodeBase64
location: class Base64
2 errors
嘗試改變進口和罐子。 – user1929959
這是我的導入導入org.apache.commons.codec.binary.Base64; 和jar在類路徑中。 –
太「新」了。請注意,標準Java(EE)中有幾個公共的Base64實現:'byte [] bytes = DatatypeConverter.parseBase64Binary(data);'from'java.xml.bind'。 –