『壹』 java:關於base64編碼問題求解大俠
網上的 base64 編碼器是啥?
中文沒有標准做法,他不見得就是對的
『貳』 java的base64在安卓解碼不了,怎麼回事
估計是代碼的問題,示例如下:
Android端:編碼:
String oneBaseEncoder = Base64.encode(msg.getBytes());
解碼:
String oneBaseDecoder = new String(Base64.decode(msg));
JAVA WEB端:編碼:
String oneBaseEncoder = new BASE64Encoder().encode(jsonString.getBytes("utf-8"));
解碼:
String oneBaseDecoder = new String(new BASE64Decoder().decodeBuffer(jsonString));
『叄』 在Java中如何進行BASE64編碼和解碼
importsun.misc.BASE64Encoder;
importsun.misc.BASE64Decoder;
//將s進行BASE64編碼
publicstaticStringgetBASE64(Strings){
if(s==null)returnnull;
return(newsun.misc.BASE64Encoder()).encode(s.getBytes());
}
//將BASE64編碼的字元串s進行解碼
(Strings){
if(s==null)returnnull;
BASE64Decoderdecoder=newBASE64Decoder();
try{
byte[]b=decoder.decodeBuffer(s);
returnnewString(b);
}catch(Exceptione){
returnnull;
}
}
『肆』 base64編碼傳到後台,後端java怎麼接收
1、兩種方式取值(不同的值傳遞方式和位置取法不一)
HttpServletRequest.getParameter("屬性名");//--第一種
//---第二種
BufferedReader bufferedReader = request.getReader();
String bodyStr = IOUtils.read(bufferedReader);
2、自學java歡迎關注
『伍』 在 java 中如何進行base64 編碼和解碼
//將s進行BASE64編碼
publicstaticStringgetBASE64(Strings){
if(s==null)returnnull;
return(newsun.misc.BASE64Encoder()).encode(s.getBytes());
}
//將BASE64編碼的字元串s進行解碼
(Strings){
if(s==null)returnnull;
BASE64Decoderdecoder=newBASE64Decoder();
try{
byte[]b=decoder.decodeBuffer(s);
returnnewString(b);
}catch(Exceptione){
returnnull;
}
}
//將BASE64編碼的字元串InputStream進行解碼
publicstaticjava.nio.ByteBuffergetFromBASE64byte(Strings){
if(s==null)
returnnull;
BASE64Decoderdecoder=newBASE64Decoder();
try{
returndecoder.decodeBufferToByteBuffer(s);//decoder.decodeBuffer(s);
}catch(Exceptione){
returnnull;
}
}
//將BASE64編碼的文件進行解碼
ByteBuffervalue=Base64Utils.getFromBASE64byte(nl.item(i*2+1).getTextContent().trim()); FileOutputStreamfos=newFileOutputStream(filename); FileChannelfc=fos.getChannel();
fc.write(value);
fos.flush();
fc.close();
importsun.misc.BASE64Encoder;
importsun.misc.BASE64Decoder;
『陸』 在java Base64編碼數據解碼問題,怎麼解決
import sun.misc.BASE64Encoder; import sun.misc.BASE64Decoder; // 將 s 進行 BASE64 編碼 public static String getBASE64(String s) { if (s == null) return null; return (new sun.misc.BASE64Encoder()).encode( s.getBytes() ); } // 將 BASE64 編碼的字元串 s 進行解碼 public static String getFromBASE64(String s) { if (s == null) return null; BASE64Decoder decoder = new BASE64Decoder(); try { byte[] b = decoder.decodeBuffer(s); return new String(b); } catch (Exception e) { return null; } }
『柒』 Java Base64加碼解碼 Base64.encodeBase64 ( ) 和 new BASE64Enccoder( ).encode( )區別
Base64.encodeBase64 ( ) 可以處理換行符,
new BASE64Enccoder( ).encode( )需要單獨處理換行符。
在linux/windows下,推薦使用第一種,不用自己單獨處理換行。
『捌』 怎麼用JAVA對一個文件進行base64編碼
JAVA對一個文件進行base64編碼
importsun.misc.BASE64Encoder;
importsun.misc.BASE64Decoder;
//將s進行BASE64編碼
publicstaticStringgetBASE64(Strings){
if(s==null)returnnull;
return(newsun.misc.BASE64Encoder()).encode(s.getBytes());
}
//將BASE64編碼的字元串s進行解碼
(Strings){
if(s==null)returnnull;
BASE64Decoderdecoder=newBASE64Decoder();
try{
byte[]b=decoder.decodeBuffer(s);
returnnewString(b);
}catch(Exceptione){
returnnull;
}
}
『玖』 關於java中BASE64解碼演算法
讓我們再來看一個實際的例子,加深印象!
轉換前 10101101 10111010 01110110
轉換後 00101011 00011011 00101001 00110110
十進制 43 27 41 54
對應碼表中的值 r b p 2
將第一個字元右移2位得00101011, 得第一個目標字元00101011
將第一個字元左移4位得11010000,第二個字元右移4位的00001011相加得第二個目標字元11011011
將第二個字元左移2位得11101000,第三個字元右移6位的00000001相加的第三個目標字元11101001
第四個目標字元就是01110110
然後讓各個目標字元與0x3F進行and位操作,讓最高的兩位為零。
『拾』 java base64解碼 怎麼是亂碼呢
會亂碼的原因是你的編碼不一致導致的
php中的urlencode的編碼是和系統編碼一致的(比如windows默認gb2312,ubuntu默認utf-8)
所以首先需要確定你的系統編碼,之後根據得到的系統編碼在調用java的decode方法的時候,將這個編碼傳入(考慮到你的例子中有繁體字,所以,建議你使用utf-8編碼),以下是我使用utf-8編碼的例子(php環境是ubuntun下)