本文目錄一覽:
- 1、在java Base64編碼數據解碼問題,怎麼解決
- 2、java base64解碼 怎麼是亂碼呢
- 3、在 java 中如何進行base64 編碼和解碼
- 4、在Java中如何進行BASE64編碼和解碼
在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解碼 怎麼是亂碼呢
會亂碼的原因是你的編碼不一致導致的
php中的urlencode的編碼是和系統編碼一致的(比如windows默認gb2312,ubuntu默認utf-8)
所以首先需要確定你的系統編碼,之後根據得到的系統編碼在調用java的decode方法的時候,將這個編碼傳入(考慮到你的例子中有繁體字,所以,建議你使用utf-8編碼),以下是我使用utf-8編碼的例子(php環境是ubuntun下)
在 java 中如何進行base64 編碼和解碼
// 將 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;
}
}
//將 BASE64 編碼的字符串 InputStream 進行解碼
public static java.nio.ByteBuffer getFromBASE64byte(String s) {
if (s == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
try {
return decoder.decodeBufferToByteBuffer(s);//decoder.decodeBuffer(s);
} catch (Exception e) {
return null;
}
}
//將 BASE64 編碼的文件進行解碼
ByteBuffer value = Base64Utils.getFromBASE64byte(nl.item(i*2+1).getTextContent().trim()); FileOutputStream fos = new FileOutputStream(filename); FileChannel fc = fos.getChannel();
fc.write(value);
fos.flush();
fc.close();
import sun.misc.BASE64Encoder;
import sun.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;
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/271513.html