android圖片base64解碼
❶ Android base64string轉bitmap
"data:image/jpeg;base64,/9j/4........H/k/wD/2Q==" 是後台傳的圖片資源,安卓的
/**
* base64轉為bitmap
*
* @param base64Data
* @return
*/
public static Bitmap base64ToBitmap(String base64Data) {
byte[] bytes = android.util.Base64.decode(base64Data, android.util.Base64.DEFAULT);
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
方法轉圖片的時候需要去掉頭部的「data:image/jpeg;base64,」再調用這個方法,網上的在線base64轉圖片就可以不去掉這個頭部也能成功。
head_iv.setImageBitmap(Tools.base64ToBitmap(img.split(",")[1]));
❷ Android 將圖片轉成base64,小圖片可以正常轉換,當遇到大圖片是拿到的字元串是null
因為太大了,一般超過2M就不要用這種方式直接存儲處理圖片,建議使用縮略圖。
❸ 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));
❹ android,Base64.encodeToString(bitmapBytes, Base64.DEFAULT);內存溢出
如果圖片過大的話,分幾次讀取圖片,每次讀取到數組後轉碼成String,轉碼完成後再拼接String,建議轉換完成的String存儲到文件中,畢竟轉碼後會比源文件到了不少,也會內存溢出吧;如果要上傳文件,每次轉碼完成發送一次,只要服務端處理好就沒有問題,文件不會出錯的。望採納。
❺ android Base64.decode 報錯
android 解碼 Base64 圖片數據時報錯
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAkACQAAD/AxNDQ0Hyc5PTgyPC4zNDL/IyMjIyMjIyMjIyMjIyMjL/wAARCAK2AyADASIAAhEBAxEB//8
Base64.decode(data, Base64.DEFAULT)
經過測試把開頭的 data:image/jpeg;base64, 去掉就好了
❻ 如何在Android用FFmpeg解碼圖像
CC libavcodec/log2_tab.o
CC libavutil/log2_tab.o
CC libswresample/log2_tab.o
AR libavcodec/libavcodec.a
LD libavutil/libavutil.so.52
AR libavutil/libavutil.a
AR libswresample/libswresample.a
LD libavcodec/libavcodec.so.55
LD libswresample/libswresample.so.0
LD libswscale/libswscale.so.2
LD libavformat/libavformat.so.55
INSTALL libavformat/libavformat.a
INSTALL libavformat/libavformat.so
STRIP install-libavformat-shared
INSTALL libavcodec/libavcodec.a
INSTALL libavcodec/libavcodec.so
STRIP install-libavcodec-shared
INSTALL libswresample/libswresample.a
INSTALL libswresample/libswresample.so
STRIP install-libswresample-shared
INSTALL libswscale/libswscale.a
INSTALL libswscale/libswscale.so
STRIP install-libswscale-shared
INSTALL libavutil/libavutil.a
INSTALL libavutil/libavutil.so
STRIP install-libavutil-shared
INSTALL libavformat/avformat.h
INSTALL libavformat/avio.h
INSTALL libavformat/version.h
INSTALL libavformat/libavformat.pc
INSTALL libavcodec/avcodec.h
INSTALL libavcodec/avfft.h
INSTALL libavcodec/dxva2.h
INSTALL libavcodec/old_codec_ids.h
INSTALL libavcodec/vaapi.h
INSTALL libavcodec/vda.h
INSTALL libavcodec/vdpau.h
INSTALL libavcodec/version.h
INSTALL libavcodec/xvmc.h
INSTALL libavcodec/libavcodec.pc
INSTALL libswresample/swresample.h
INSTALL libswresample/version.h
INSTALL libswresample/libswresample.pc
INSTALL libswscale/swscale.h
INSTALL libswscale/version.h
INSTALL libswscale/libswscale.pc
INSTALL libavutil/adler32.h
INSTALL libavutil/aes.h
INSTALL libavutil/attributes.h
INSTALL libavutil/audio_fifo.h
INSTALL libavutil/audioconvert.h
INSTALL libavutil/avassert.h
INSTALL libavutil/avstring.h
INSTALL libavutil/avutil.h
INSTALL libavutil/base64.h
INSTALL libavutil/blowfish.h
INSTALL libavutil/bprint.h
INSTALL libavutil/bswap.h
INSTALL libavutil/buffer.h
INSTALL libavutil/channel_layout.h
INSTALL libavutil/common.h
INSTALL libavutil/cpu.h
INSTALL libavutil/crc.h
INSTALL libavutil/error.h
INSTALL libavutil/eval.h
INSTALL libavutil/fifo.h
INSTALL libavutil/file.h
INSTALL libavutil/frame.h
INSTALL libavutil/hmac.h
INSTALL libavutil/imgutils.h
INSTALL libavutil/intfloat.h
INSTALL libavutil/intfloat_readwrite.h
INSTALL libavutil/intreadwrite.h
INSTALL libavutil/lfg.h
INSTALL libavutil/log.h
INSTALL libavutil/mathematics.h
INSTALL libavutil/md5.h
INSTALL libavutil/mem.h
INSTALL libavutil/murmur3.h
INSTALL libavutil/dict.h
INSTALL libavutil/old_pix_fmts.h
INSTALL libavutil/opt.h
INSTALL libavutil/parseutils.h
INSTALL libavutil/pixdesc.h
INSTALL libavutil/pixfmt.h
INSTALL libavutil/random_seed.h
INSTALL libavutil/rational.h
INSTALL libavutil/ripemd.h
INSTALL libavutil/samplefmt.h
INSTALL libavutil/sha.h
INSTALL libavutil/sha512.h
INSTALL libavutil/time.h
INSTALL libavutil/timecode.h
INSTALL libavutil/timestamp.h
INSTALL libavutil/version.h
INSTALL libavutil/xtea.h
INSTALL libavutil/lzo.h
INSTALL libavutil/avconfig.h
INSTALL libavutil/libavutil.pc
link ffmpeg.
二、新建一個Android工程,在工程目錄下新建一個jni文件夾,在文件夾下新建一個ffmpeg文件夾,用來放ffmpeg相關的頭文件。在ffmpeg文件夾下新建Android.mk文件用來預先載入ffmpeg動態庫。Android.mk文件內容如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ffmpeg
LOCAL_SRC_FILES := /path/to/build/output/libffmpeg.so
include $(PREBUILT_SHARED_LIBRARY)
三、在jni下新建Android.mk文件和Application.mk兩個文件用來指定編譯的順序和編譯的平台以及對應的cpu指令集。
Application.mk
APP_ABI := armeabi
APP_PLATFORM := android-9
Android.mk
include $(call all-subdir-makefiles)
四、編寫JNI文件,用來綁定java文件與.c文件的交互,文件內容如下:
/*
* ffmpeg_jni.c
*
* Created on: Sep 1, 2014
* Author: clarck
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <jni.h>
#include "../include/ffmpeg_logger.h"
#include "../include/ffmpeg.h"
// 指定要注冊的類,對應完整的java類名
#define JNIREG_CLASS "com/clarck/android/ffmpeg/MainActivity"
JNIEXPORT void JNICALL native_setDataSource(JNIEnv *env, jclass classzz, jstring path) {
char *filepath = ffmpeg_jstringTostr(env, path);
ffmpeg_setDataSource(filepath);
}
//Java和JNI函數的綁定
static JNINativeMethod method_table[] = {
{ "setDataSource", "(Ljava/lang/String;)V", native_setDataSource }
};
//注冊native方法到java中
static int registerNativeMethods(JNIEnv *env, const char *className,
JNINativeMethod *gMethods, int numMethods) {
jclass clazz;
clazz = (*env)->FindClass(env, className);
if (clazz == NULL) {
return JNI_FALSE;
}
if ((*env)->RegisterNatives(env, clazz, gMethods, numMethods) < 0) {
return JNI_FALSE;
}
return JNI_TRUE;
}
//調用注冊方法
int register_ndk_load(JNIEnv *env) {
return registerNativeMethods(env, JNIREG_CLASS, method_table,
(int) (sizeof(method_table) / sizeof(method_table[0])));
}
JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
JNIEnv *env = NULL;
jint result = -1;
if ((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6) != JNI_OK) {
return result;
}
register_ndk_load(env);
//返回JNI的版本
return JNI_VERSION_1_6;
}
五、編寫ffmpeg調用函數,內容如下:
/*
* ffmpeg.c
*
* Created on: Sep 1, 2014
* Author: clarck
*/
#include <jni.h>
#include <android/native_window_jni.h>
#include "../include/ffmpeg.h"
#include "../include/ffmpeg_logger.h"
#include "../ffmpeg/include/libavcodec/avcodec.h"
#include "../ffmpeg/include/libavformat/avformat.h"
#include "../ffmpeg/include/libavutil/pixfmt.h"
#include "../ffmpeg/include/libswscale/swscale.h"
char* ffmpeg_jstringTostr(JNIEnv* env, jstring jstr) {
char* pStr = NULL;
jclass jstrObj = (*env)->FindClass(env, "java/lang/String");
jstring encode = (*env)->NewStringUTF(env, "utf-8");
jmethodID methodId = (*env)->GetMethodID(env, jstrObj, "getBytes",
"(Ljava/lang/String;)[B");
jbyteArray byteArray = (jbyteArray) (*env)->CallObjectMethod(env, jstr,
methodId, encode);
jsize strLen = (*env)->GetArrayLength(env, byteArray);
jbyte *jBuf = (*env)->GetByteArrayElements(env, byteArray, JNI_FALSE);
if (jBuf > 0) {
pStr = (char*) malloc(strLen + 1);
if (!pStr) {
return NULL ;
}
memcpy(pStr, jBuf, strLen);
pStr[strLen] = 0;
}
(*env)->ReleaseByteArrayElements(env, byteArray, jBuf, 0);
return pStr;
}
void ffmpeg_setDataSource(char *file_path) {
LOGI("ffmpeg_setDataSource:%s", file_path);
AVFormatContext *pFormatCtx;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame, *pFrameYUV;
AVPacket *packet;
uint8_t *out_buffer;
static struct SwsContext *img_convert_ctx;
int videoStream, i, numBytes;
int ret, got_picture;
av_register_all();
pFormatCtx = avformat_alloc_context();
if (avformat_open_input(&pFormatCtx, file_path, NULL, NULL) != 0) {
LOGE("can『t open the file. \n");
return;
}
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
LOGE("Could『t find stream infomation.\n");
return;
}
videoStream = 1;
for (i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStream = i;
}
}
if (videoStream == -1) {
LOGE("Didn『t find a video stream.\n");
return;
}
pCodecCtx = pFormatCtx->streams[videoStream]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec == NULL) {
LOGE("Codec not found.\n");
return;
}
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
LOGE("Could not open codec.\n");
return;
}
pFrame = av_frame_alloc();
pFrameYUV = av_frame_alloc();
numBytes = avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width,
pCodecCtx->height);
out_buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
avpicture_fill((AVPicture *) pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P,
pCodecCtx->width, pCodecCtx->height);
int y_size = pCodecCtx->width * pCodecCtx->height;
packet = (AVPacket *) malloc(sizeof(AVPacket));
av_new_packet(packet, y_size);
av_mp_format(pFormatCtx, 0, file_path, 0);
while (av_read_frame(pFormatCtx, packet) >= 0) {
if (packet->stream_index == videoStream) {
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture,
packet);
LOGI("avcodec_decode_video2 ret:%d", ret);
if (ret < 0) {
LOGE("decode error.\n");
return;
}
if (got_picture) {
//TODO 此處可以將解碼出來的圖片保存起來。
}
}
av_free_packet(packet);
}
av_free(out_buffer);
av_free(pFrameYUV);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
}
六、編寫Android.mk用來編譯相關的.c文件,內容如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
FFMPEG_PATH := ../ffmpeg
LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(FFMPEG_PATH)/include
LOCAL_MODULE := ffmpeg_player
LOCAL_SRC_FILES += ffmpeg_jni.c
LOCAL_SRC_FILES += ffmpeg.c
LOCAL_SHARED_LIBRARIES := ffmpeg
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
七、編寫java文件中相關執行調用方法
package com.clarck.android.ffmpeg;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setDataSource("/sdcard/a.mp4");
}
public native void setDataSource(String path);
static {
System.loadLibrary("ffmpeg");
System.loadLibrary("ffmpeg_player");
}
}
❼ 手機端用android.util.Base64編碼,服務端我該導入哪個包解碼
Base 64是通用演算法 所有語言的演算法都是一樣的, 服務端可以用這個包, jar包已經上傳直接導入即可使用. javabase64-1.3.1.jar大小:4.26K所需財富值:5 已經過網路安全檢測,放心下載 點擊下載下載量:19
❽ Android 根據文件路徑base64字元串和文件互相轉換
android 本地存文件,通過base64轉碼文件存儲到本地
2.Base64字元串轉文件
先通過BASE64Decoder 將base64字元串解碼轉為位元組數組,在通過位元組流將位元組數組寫入文件中,通過bytes.length 屬性可查看base64字元串轉位元組是否有缺失,比對文件大小查看是否一樣
3.將文件轉為Base64字元串
這個沒什麼可說的,跟上面的流程相反,通過位元組流讀文件,然後將讀出的位元組數組通過BASE64Encoder 編碼
❾ android 圖片轉BASE64上傳提示java.lang.OutOfMemoryError
我最近也碰到了這個問題,但是網上沒有找到相關有效的直接解決方法。
後來看到了一篇解釋base64編碼原理的文章,研究了一番後解決了。
一般碰到這個問題的,都涉及到"大文件上傳"的問題,"大文件上傳"過程中除了base64編碼時可能OOM,其實還有其他問題,雖然提問中沒有提出,可能是因為這個問題還沒有解決,所以還沒有遇到其它問題,我就圍繞著"大文件上傳"來解決這個問題吧。
(提問時間在下看的清楚)
————————————————————
做項目的過程中碰到一個需求:
在java客戶端,使用http通信,把客戶端的本地文件通過http發送上傳到伺服器;
請求格式是xml(不管是json還是xml都是字元串,所以這個無所謂),中間包含[文件流字元串];
之前的做法是,把文件流通過base64編碼轉換為base64Byte,然後和其它字元串信息放到一起,post的時候通過HttpURLConnection的write方法寫入到伺服器中去,這個上傳的過程就完成了。
——————————
但是碰到一個問題,當文件體積較大時,從文件流轉換成base64Byte後,體積會很大,可能會導致OOM;
(以二進制流的方式保存,體積最小;以byte數組的方式保存,體積會相對變大一些;以String形式保存,體積最大;)
出錯原因是:
FileInputStream fis = new FileInputStream(file); //這一步打開了一個對准file准備進行讀取的文件指針,但是還沒有開始讀寫,file的相關數據沒有從本地載入到內存中來;所以即使file的體積有10G那麼大,這一步也是不會OOM的
//把文件流轉換為位元組數組
byte[] fileBytes;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] byteBuf = new byte[1024];
int count;
while((count=fis.read(buf))!=-1)
{
baos.write(buf,0,count); //實際上,如果文件體積比較大的話,不用轉碼,在這一步就可能OOM了
}
fileBytes= baos.toByteArray();
byte[] base64Bytes = Base64.encodeBase64(fileBytes); //在這一步也可能OOM
(文件轉換為byte[]時,是有可能OOM的;而轉換為base64Bytes後,體積會增大1/3,所以有可能前一步沒有OOM,卻在這一步出現OOM;
為什麼轉碼後體積會增大1/3,後面我會解釋)
——————————
解決方法
既然file在本地沒有載入到內存來的時候不會出現內存溢出的情況,我就想到了一個解決的方法:分段上傳
(加大內存並不能從根本上解決內存溢出的問題,問題的根本原因不是內存不夠大,而是代碼有問題)
在本地的file通過HttpURLConnection的getOutputStream()進行write時,不是一次性全部寫入,而是循環配合flush進行寫入:
FileInputStream fis = new FileInputStream(file);
byte[] buf = new byte[1024];
int count;
while((count = fis.read(buf)) != -1)
{
os.write(Base64.encodeBase64(buf), 0, count);
os.flush();
}
(我從本地讀1024位元組,然後馬上上傳到伺服器,清空本地緩存,然後再從本地讀1024位元組,這樣循環讀取,即使文件有20G,理論上也不有OOM問題出現,因為我從本地文件中讀到的數據不會在內存中駐留)
——————————
解決問題的思路對了,但是出現了其他的細節問題
os.write(Base64.encodeBase64(buf), 0, count); //這一行代碼報錯了,出現了OOM
我搜集了一下資料,發現原因是:
HttpURLConnection的getOutputStream的實際對象是sun.net.<a href="http://www.http.PosterOutputStream" target="_blank">www.http.PosterOutputStream</a>,這個對象的flush方法代碼是空的,write配合flush,並沒有達到即時上傳數據的效果。PosterOutputStream其實是自己在本地維護了一個緩沖區,你通過write寫入的數據其實還是在這個本地的緩沖區里,只有當你getInputStream後,HttpURLConnection才會把這段緩沖區中的數據上傳到伺服器上。而flush達不到上傳數據,清空本地緩存的效果。
——————————
(我是不能通過getInputStream來刷新緩沖流的,因為那就不是分段上傳而是"分次"上傳了)
那這就不是我的思路的問題了。再去搜索解決方法後,得知:
在創建HttpURLConnection對象的時候,要調用一個方法
hurlc.setChunkedStreamingMode(1024); //設置分塊流模式 也就是分塊上傳 1024是getOutputStream維護的本地緩沖區的大小
調用該方法後,只要本地緩存區滿了,HttpURLConnection就會自動把緩沖區里的數據發送到伺服器,同時清空本地緩存(ps:HttpURLConnection的getOutputStream似乎是個抽象的工廠方法,在調用setChunkedStreamingMode方法後,我發現getOutputStream獲取到的實例對象從sun.net.<a href="http://www.http.PosterOutputStream" target="_blank">www.http.PosterOutputStream</a>變成了sun.net.<a href="http://www.protocol.http.HttpURLConnection$StreamingOutputStream" target="_blank">www.protocol.http.HttpURLConnection$StreamingOutputStream</a>)
——————————
果然,調用setChunkedStreamingMode方法後,os.write(Base64.encodeBase64(buf), 0, count);沒有再出現OOM異常了
但是,又出現了一個新的問題
我發現
FileInputStream fis = new FileInputStream(file);
byte[] buf = new byte[1024];
int count;
while((count = fis.read(buf)) != -1)
{
os.write(Base64.encodeBase64(buf), 0, count);
os.flush();
}
這段分段編碼寫入的代碼,其編碼所得結果,與非分段編碼所得結果是不一樣的
通過分段編碼上傳的圖片內容出現了錯誤
我通過下面代碼測試:
//分段編碼
ByteArrayOutputStream os1 = new ByteArrayOutputStream();
InputStream file1 = new FileInputStream(path);
byte[] buf1 = new byte[1024];
int count1;
while((count1 = file1.read(buf1)) != -1)
{
os1.write(Base64.encodeBase64(buf1), 0, count1);
os1.flush();
}
file1.close();
System.out.println(os1.toString());
//非分段編碼
ByteArrayOutputStream os2 = new ByteArrayOutputStream();
InputStream file2 = new FileInputStream(path);
byte[] buf2 = new byte[1024];
int count2;
while((count2 = file2.read(buf2)) != -1)
{
os2.write(buf2, 0, count2);
os2.flush();
}
file2.close();
System.out.println(new String(Base64.encodeBase64(os2.toByteArray())));
兩者的結果:
/9j/4AAQSkZJR...wDtUAVs7eF...
/9j/4AAQSkZJR...wDt89ymnxJ...
前面一段還是相同的,轉到後面,就開始南轅北轍了
——————————
原因我去網上找了一下但是沒有找到直接答案,但是看到一篇解釋base64編碼原理的文章
原文鏈接:<a href="http://www.cnblogs.com/luguo3000/p/3940197.html" target="_blank">http://www.cnblogs.com/luguo3000/p/3940197.html</a>
假設有文件A(txt文件,包含文本內容"ABCDEFG"),轉換為InputStream->byte[]後
它們的ASIIC碼分別對應65、66、67、68、69、70、71
二進製表現形式為:
1000001 1000010 1000011 1000100 1000101 1000110 1000111
對高位補零後:
01000001 01000010 01000011 01000100 01000101 01000110 01000111
在內存中的實際表現:
而base64編碼,使用的字元包括(A-Z、a-z、0-9、+、/、=)這些常規可讀字元,使用base64編碼的原因,用途,在於把一些亂碼字元、不可讀字元轉換為常規可讀字元;
(因為java底層的通信協議、或者說其它的通信協議,很多地方用到遠程通信這一塊的,對一些亂碼字元不支持傳輸,所以需要把亂碼字元轉換成常規可讀字元才能進行傳輸)
比如對於'矙'這個字元,部分傳輸協議的編碼集就不認識它,所以無法直接傳輸,必須base64轉碼
'矙'的UTF-8編碼值為30681,二進製表現形式為111011111011001->(0)111011111011001
需要兩個位元組來存儲01110111 11011001
base64編碼只有(A-Z、a-z、0-9、+、/、=)這些字元來表示。需要強調的是,在base64編碼規范中,字元'A'不等於65、'B'也不是66...。base64字元與數值(二進制值)的對應關系如下:
也就是說,常規字元'A'=65=01000001;而base64字元'A'=0=00000000;
base64字元代表的二進制值是無法直接表示'矙'這個字元的,因為base64字元的值范圍在0~63之間(二進制值在(00)000000~(00)111111之間)。
那如何通過(00)000000~(00)111111之間的數值來表示01110111 11011001呢?
這就是base64的編碼演算法了
一個base64字元的二進制值在(00)000000~(00)111111之間,也就是說它可以表示000000~111111之間的二進制數,一個base64字元的有效位為後6位。如何通過以6bit為單位的base64字元表示以8bit為單位的常規位元組?
6和8的最小公倍數為24,即 每4個base64字元可以表示3個常規位元組;
回到剛才的文件A,編碼過程:
(初始文件A)->"ABCDEFG"
(轉UTF-8碼 int)->65 66 67 68 69 70 71
("ABCDEFG"的二進製表示;7位元組)->1000001 1000010 1000011 1000100 1000101 1000110 1000111
(高位補零)->01000001 01000010 01000011 01000100 01000101 01000110 01000111
(連寫)->
(按6bit為單位對所有bit進行分割;得到10位元組)->010000 010100 001001 000011 010001 000100 010101 000110 010001 11
(按6bit*4=8bit*3的對應關系再分割;得到3組6*4位元組)->(010000 010100 001001 000011) (010001 000100 010101 000110) (010001 11)
(高位補2個零;末尾的低位也補零)->(00010000 00010100 00001001 00000011) (00010001 00000100 00010101 00000110) (00010001 00110000)
(二進制值換算成十進制)->(16 20 9 3) (17 4 21 6) (17 48)
(按base64編碼的值-字元對應表,得出上面的十進制值對應的base64字元)->(Q U J D) (R E V G) (R w)
(每組base64字元都要求是4個,空白的位置補'='字元)->(Q U J D) (R E V G) (R w = =)
(文件A的最終轉碼結果)->QUJDREVGRw==
這里以文本文件作為演示,因為文本文件機器可讀人也可讀;實際情況中,很多時候轉碼的目標文件並不是文本文件,那就不能以可讀字元串形式表示了,會直接以二進制格式表示
體積增大的原因,是因為3位元組=24bit=分割成4個6bit-,對4個6bit高位補零後,就得到4個位元組
也就是說3個常規位元組經base64編碼後會生成4個base64位元組,這就是文件經base64轉碼後體積會增加1/3的原因
——————————
base64編碼原理解釋了,再看剛才的分段編碼
ByteArrayOutputStream os1 = new ByteArrayOutputStream();
InputStream file1 = new FileInputStream(path);
byte[] buf1 = new byte[1024];
int count1;
while((count1 = file1.read(buf1)) != -1)
{
os1.write(Base64.encodeBase64(buf1), 0, count1); //可以發現一個問題:Base64.encodeBase64(buf1)編碼後,體積會增加1/3,所以這里的Base64.encodeBase64(buf1)編碼轉換後的實際長度和count1並不相等,所以實際寫入到os1中的base64字元數只有Base64.encodeBase64(buf1)編碼產生的字元數的3/4
os1.flush();
}
file1.close();
System.out.println(os1.toString());
修改後:
ByteArrayOutputStream os1 = new ByteArrayOutputStream();
InputStream file1 = new FileInputStream(path);
byte[] byteBuf = new byte[1024];
byte[] base64ByteBuf;
while(file1.read(byteBuf) != -1)
{
base64ByteBuf = Base64.encodeBase64(byteBuf);
os1.write(base64ByteBuf, 0, base64ByteBuf.length);
os1.flush();
}
file1.close();
System.out.println(os1.toString());
——————————
修改後,發現分段編碼的結果發生了變化,跟之前不一樣了
但仍然不是正確的結果
原因在於,base64字元的基礎單位是(6bit*4=4位元組),而3個常規位元組(8bit*3)才能剛好產生4個base64位元組
根本原因在於,如果進行編碼的常規位元組數不是3的倍數,最後就會餘下1或2個位元組,而這1、2個位元組編碼的結果就會產生'='字元;
使用1024作為分段編碼緩沖時,編碼的結果是3+3+3+...+1
也就是每次都會餘1位元組
而沒有使用分段編碼時,當編碼到第1024個位元組時,"餘下"的1位元組會跟後面的位元組形成連續,就不會產生'='字元
(對一段byte字元進行base64編碼時,中間是絕不會產生'='字元的,因為只有在結尾才可能餘下1或2個位元組,所以對一段byte字元進行編碼時,只有結尾才可能產生1或2個'='補全字元)
——————————
解決方法是,使用3的公倍數作為緩沖區大小
修改後:
ByteArrayOutputStream os1 = new ByteArrayOutputStream();
InputStream file1 = new FileInputStream(path);
byte[] byteBuf = new byte[3*1000];
byte[] base64ByteBuf;
while(file1.read(byteBuf) != -1)
{
base64ByteBuf = Base64.encodeBase64(byteBuf);
os1.write(base64ByteBuf, 0, base64ByteBuf.length);
os1.flush();
}
file1.close();
System.out.println(os1.toString());
測試結果再次發生了改變
中間不再有'='字元了,因為中間每次都是3位元組3位元組的編碼,沒有餘下多餘的位元組
對比之後發現,中間段的結果已經正常了
——————————
但是,發現,結尾處兩個轉碼的結果有些許不同
原因在於,假設文件A的長度為3001個位元組;
在第二次循環讀取時,只讀到1個有效位元組,而byteBuf的剩餘2999個位元組都是無效位元組,而此時編碼時,卻把多餘的2999個無效位元組也編碼了進去
(如果是非分段轉碼,就不會出現這種情況)
解決方法:
ByteArrayOutputStream os1 = new ByteArrayOutputStream();
InputStream file1 = new FileInputStream(path);
byte[] byteBuf = new byte[3*1000];
byte[] base64ByteBuf;
int count1; //每次從文件中讀取到的有效位元組數
while((count1=file1.read(byteBuf)) != -1)
{
if(count1!=byteBuf.length) //如果有效位元組數不為3*1000,則說明文件已經讀到尾了,不夠填充滿byteBuf了
{
byte[] = Arrays.Of(byteBuf, count1); //從byteBuf中截取包含有效位元組數的位元組段
base64ByteBuf = Base64.encodeBase64(); //對有效位元組段進行編碼
}
else
{
base64ByteBuf = Base64.encodeBase64(byteBuf);
}
os1.write(base64ByteBuf, 0, base64ByteBuf.length);
os1.flush();
}
file1.close();
System.out.println(os1.toString());
至此,base64分段編碼才算大功告成。大文件上傳核心代碼才算大功告成。
其實代碼改起來非常簡單,但是不知道原因不知道原理的話,是無法無中生有的
對我本人來說原本只是想隨便答一下,但沒想到答的過程中發現自己有很多坑沒有發現。答的過程中把自己不懂的地方沒有發現的坑也完善了。不說碰到一個知識點就要追根究底,但實際開發中,每一個自己能親身碰到的實際問題都是鍛煉自己的絕佳機會,這種近距離觸碰問題、解決問題的機會是難得的。雖然開發中還有很多其它問題也很重要,但你沒有親手碰到過,是無法共鳴的。所以自己在開發中碰到了問題,還是建議大概弄清原因。
弄清原理後,即使以後出現這個問題的其它"變種",也能找到原因並自己解決,但僅僅粘貼復制無法做到這一點。
❿ android,java 通用的加密解密方式有幾種
移動端越來越火了,我們在開發過程中,總會碰到要和移動端打交道的場景,比如.NET和android或者iOS的打交道。為了讓數據交互更安全,我們需要對數據進行加密傳輸。今天研究了一下,把幾種語言的加密都實踐了一遍,實現了.NET,java(android),iOS都同一套的加密演算法,下面就分享給大家。
AES加密有多種演算法模式,下面提供兩套模式的可用源碼。
加密方式:
先將文本AES加密
返回Base64轉碼
解密方式:
將數據進行Base64解碼
進行AES解密
一、CBC(Cipher Block Chaining,加密塊鏈)模式
是一種循環模式,前一個分組的密文和當前分組的明文異或操作後再加密,這樣做的目的是增強破解難度.
密鑰
密鑰偏移量
java/adroid加密AESOperator類:
package com.bci.wx.base.util;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* AES 是一種可逆加密演算法,對用戶的敏感信息加密處理 對原始數據進行AES加密後,在進行Base64編碼轉化;
*/
public class AESOperator {
/*
* 加密用的Key 可以用26個字母和數字組成 此處使用AES-128-CBC加密模式,key需要為16位。
*/
private String sKey = "smkldospdosldaaa";//key,可自行修改
private String ivParameter = "0392039203920300";//偏移量,可自行修改
private static AESOperator instance = null;
private AESOperator() {
}
public static AESOperator getInstance() {
if (instance == null)
instance = new AESOperator();
return instance;
}
public static String Encrypt(String encData ,String secretKey,String vector) throws Exception {
if(secretKey == null) {
return null;
}
if(secretKey.length() != 16) {
return null;
}
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] raw = secretKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(vector.getBytes());// 使用CBC模式,需要一個向量iv,可增加加密演算法的強度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(encData.getBytes("utf-8"));
return new BASE64Encoder().encode(encrypted);// 此處使用BASE64做轉碼。
}
// 加密
public String encrypt(String sSrc) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());// 使用CBC模式,需要一個向量iv,可增加加密演算法的強度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
return new BASE64Encoder().encode(encrypted);// 此處使用BASE64做轉碼。
}
// 解密
public String decrypt(String sSrc) throws Exception {
try {
byte[] raw = sKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, "utf-8");
return originalString;
} catch (Exception ex) {
return null;
}
}
public String decrypt(String sSrc,String key,String ivs) throws Exception {
try {
byte[] raw = key.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivs.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, "utf-8");
return originalString;
} catch (Exception ex) {
return null;
}
}
public static String encodeBytes(byte[] bytes) {
StringBuffer strBuf = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
strBuf.append((char) (((bytes[i] >> 4) & 0xF) + ((int) 'a')));
strBuf.append((char) (((bytes[i]) & 0xF) + ((int) 'a')));
}
return strBuf.toString();
}