當前位置:首頁 » 編程語言 » javabase64文件

javabase64文件

發布時間: 2023-02-05 13:12:05

1. java中用Base64編程的文件批量加密解密工具程序代碼

/** * BASE64解密 * * @param key * @return * @throws Exception */
public static byte[] decryptBASE64(String key) throws Exception { return (new BASE64Decoder()).decodeBuffer(key); } /** * BASE64加密 * * @param key * @return * @throws Exception */ public static String encryptBASE64(byte[] key) throws Exception { return (new BASE64Encoder()).encodeBuffer(key); }

2. Java 保存圖片到資料庫時,為什麼要對圖片進行base64編碼

首先這是一種SB做法,圖片保存到資料庫這個很浪費資料庫資源, 通常情況下圖片等文件都是用ftp伺服器來存儲文件的. 為什麼要用base64進行編碼是因為, base64會把文件這個文件轉換成字元串, base64編碼後得到的是一組字元串, 為什麼要用blob類型, 因為這個類型可以存儲4GB數據, 資料庫中普通的 varchar varchar2 text等類型都有長度的限制

3. Base64演算法原理及實現

Base64演算法最開始是被用於解決電子郵件數據傳輸問題。在早期,由於歷史原因問題,電子郵件只允許使用ASCII字元,如果在郵件中出現了非ASCII字元,在通過某些網關進行數據轉發的時候,網關會對這些非ASCII字元做出調整,例如,把ASCII碼8位二進制碼的最高位置為0。此時接收方在收到郵件時就會出現亂碼。基於這個原因,產生了Base64演算法。

Base64編碼的思路說白了,就是把傳輸數據的每個位元組映射成ASCII碼表中的某些字元,這樣在傳輸的過程中,就不會出現亂碼的問題了。Base64演算法定義了一個映射表,如下所示。

由上表可以看出,之所以稱為Base64編碼,實際上是把原數據映射成了ASCII碼表中的64個字元。但是,64個字元最多能映射的位數是6bit。但是每個數據是8bit的,那怎麼轉換呢?Base64編碼的基本思想: 將原數據每3個位元組(24bit)分為一組,然後將這24bit數據按照每6bit一組,重新劃分為4組,分組完成之後,再將每每6bit數據為單元進行映射。
Base64編碼的基本流程如下:

例如,將字元串"ABC"進行Base64編碼流程如下。

所以,字元串"ABC"經過Base64編碼後的數據是"QUJD"。

從Base64編碼的原理可以看到,Base64實際上就是把原來數據中的每3個位元組一組進行Base64編碼轉換,編碼之後變成4個Base64字元。但是如果原文數據長度不是3的整數倍的時候該怎麼辦呢?Base64演算法規定,如果待加密數據不是3的整數倍,就在原文數據後面補0,直到長度湊夠3的整數倍為止,然後再進行Base64編碼轉換。待編碼轉換完成之後,在結果末尾補充相同個數的"="。
例如,將字元串"ABCD"進行Base64編碼流程如下。

所以,字元串"ABC"經過Base64編碼後的字元串是"QUJDRA=="。

其實這里有個規律,當原文的數據長度除以3餘數為0時,編碼之後後面沒有"=";當余數為1時,後面有兩個"=",當余數是2時,後面有一個"=","="的個數也就是補充的位元組數。

通過Base64的原理可以看到,Base64編碼實際上是把原數據的3個位元組映射成了4個位元組,所以相比於原數據長度,編碼後的長度會增加1/3。這也會降低傳輸效率。

Get方式和Post方式是Http請求常用的兩種方式,某些情況下會要求使用Get方式來傳遞二進制數據。這時,可以先通過Base64編碼來將二進制數據轉換成字元串數據。由於符號"+"和符號"/"是不允許出現在Url中的,所以,產生了Url安全的Base64演算法,所謂的Url安全的Base64演算法,其實主要包含兩個方面。

目前,在Java中,我們可以通過以下方式來是使用Base64演算法。

在java8之前,JDK官方庫中都沒有內置Base64演算法,其實Base64實現很簡單,這個不知道為什麼。但是Java8內置了Base64編碼器和解碼器。
在Java8中,Base64工具類提供了三種BASE64編解碼器:
1.基本Base64編碼
也就是完全按照標准Base64的映射規則來編解碼,不添加任何行標。

2.Url Base64編碼
JDK標准類庫中的Url Base64編碼是用"-"和"_"取代了"+"和"/"

3.MIME Base64編碼
Java類庫中還提供了一種格式更友好的Base64編碼,這種編碼輸出每行不超過76字元,並且使用' '並跟隨' '作為分割。

4.去除填充符的Base64
在Java標准類庫中,還提供了一種方式來去除編碼末尾的"=",就是在構建Encoder 對象後調用withoutPadding()方法,例如:

Commons Codec是Apache為Java開發者提供的一個開源軟體類庫,該類庫中主要是一些常用的編碼工具類包,例如DES、SHA1、MD5、Base64,URL等。在使用該類庫之前需要首先在Eclipse中添加依賴。Commons Codec提供了以下Base64編碼方式。
1.基本Base64編碼
Commons Codec和Java標准類庫提供給的Base64編碼方式是一樣的。

2.Url Base64編碼
Url Base64編碼和Java類庫也是一樣的,把"+"和"/"替換成了"-"和"_",有一個不同的地方是Commons Codec中的Url Base64默認去掉了後面的"=",相當於Java類庫中調用了withouPadding方法,例如:

3.類MIME格式輸出
Commons Codec中也提供了類似於Java類庫中的MIME的格式化輸出,在Commons Codec中有一個方法:

這里的isChunked置為true,就表示是按照MIME格式輸出編碼結果。

h

4. 從文件中讀取數據並編碼成base64 java

代碼如下:

importjava.io.FileInputStream;
importjava.io.IOException;
importjava.util.Arrays;
importjava.util.Base64;
importjava.util.Base64.Encoder;

publicclassApp{

publicstaticvoidmain(String[]args)throwsIOException{

Encoderencoder=Base64.getEncoder();

byte[]buffer=newbyte[1024];

intlen=0;

StringBuilderbuilder=newStringBuilder();

try(FileInputStreaminputStream=newFileInputStream("d:\temp\abc.txt")){

while((len=inputStream.read(buffer))!=-1){

byte[]src=Arrays.OfRange(buffer,0,len);

builder.append(encoder.encodeToString(src));
}

}

System.out.println(builder.toString());
}
}

5. java中如何用base64解碼圖片,並返回圖片,不保存。

給你發個我以前的工具類吧、
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class ImageChange {
/**
* 從path這個地址獲取一張圖片然後轉為base64碼
* @param imgName 圖片的名字 如:123.gif(是帶後綴的)
* @param path 123.gif圖片存放的路徑
* @return
* @throws Exception
*/
public static String getImageFromServer(String imgName,String path)throws Exception{
BASE64Encoder encoder = new sun.misc.BASE64Encoder();
File f = new File(path+imgName);
if(!f.exists()){
f.createNewFile();
}
BufferedImage bi = ImageIO.read(f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "gif", baos);
byte[] bytes = baos.toByteArray();
return encoder.encodeBuffer(bytes).trim();
}

/**
* 將一個base64轉換成圖片保存在 path 文件夾下 名為imgName.gif
* @param base64String
* @param path 是一個文件夾路徑
* @param imgName 圖片名字(沒有後綴)
* @throws Exception
*/
public static String savePictoServer(String base64String,String path,String imgName)throws Exception{
BASE64Decoder decoder = new sun.misc.BASE64Decoder();
byte[] bytes1 = decoder.decodeBuffer(base64String);
ByteArrayInputStream s = new ByteArrayInputStream(bytes1);
BufferedImage bi1 =ImageIO.read(s);

Date timeCur = new Date();
SimpleDateFormat fmtYY = new SimpleDateFormat("yyyy");
SimpleDateFormat fmtMM = new SimpleDateFormat("MM");
SimpleDateFormat fmtDD = new SimpleDateFormat("dd");
String strYY = fmtYY.format(timeCur);
String strMM = fmtMM.format(timeCur);
String strDD = fmtDD.format(timeCur);

String realPath = path+"/"+strYY+"/"+strMM+"/"+strDD;

File dir=new File(realPath);
if(!dir.exists()){
dir.mkdirs();
}
String fileName=path+"\\"+strYY+"\\"+strMM+"\\"+strDD +"\\"+imgName+".gif";
File w2 = new File(fileName);//可以是jpg,png,gif格式
ImageIO.write(bi1, "jpg", w2);//不管輸出什麼格式圖片,此處不需改動

return fileName;
}

public static void main(String[] args) throws Exception {
System.out.println(getImageFromServer("001001.gif","d:"));
}
}

6. 怎麼用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;
}
}

7. java base64 編碼什麼用

base64可以用來將binary的位元組序列數據編碼成ASCII字元序列構成的文本。使用時,在傳輸編碼方式中指定base64。使用的字元包括大小寫字母各26個,加上10個數字,和加號「+」,斜杠「/」,一共64個字元,等號「=」用來作為後綴用途。

8. java mail為啥 分段 base64

MimeBodyPart 這個類中的 setFileName 方法 用到一個 ParameterList 在ParameterList 的 toString 類中找到下面一段:
if (v instanceof MultiValue) {// ....ns = name + i + "*";//...} } else if (v instanceof Value) {/// ... } else {if (value.length() > 60 &&splitLongParameters && encodeParameters) { int seg = 0; name += "*"; /// ....}123456789101112131415

這個類在郵件附件屬於 MultiValue 會把 名字用name + i 隔開 ,在名字大於 60個字元的時候也會主動截斷,這也就是 javamail 中 附件的命名規則,名字太長會被截斷~~!
中文在base64 加密後,超過60個字元那是妥妥的有可能。這種截斷文件名的模式在某些客戶端,比如閃電郵中,並不能很好的支持。
參考資料來源:網路貼吧

9. java中如何用base64解碼圖片,並返回圖片,不保存。

給你發個我以前的工具類吧、
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class ImageChange {
/**
* 從path這個地址獲取一張圖片然後轉為base64碼
* @param imgName 圖片的名字 如:123.gif(是帶後綴的)
* @param path 123.gif圖片存放的路徑
* @return
* @throws Exception
*/
public static String getImageFromServer(String imgName,String path)throws Exception{
BASE64Encoder encoder = new sun.misc.BASE64Encoder();
File f = new File(path+imgName);
if(!f.exists()){
f.createNewFile();
}
BufferedImage bi = ImageIO.read(f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "gif", baos);
byte[] bytes = baos.toByteArray();
return encoder.encodeBuffer(bytes).trim();
}

/**
* 將一個base64轉換成圖片保存在 path 文件夾下 名為imgName.gif
* @param base64String
* @param path 是一個文件夾路徑
* @param imgName 圖片名字(沒有後綴)
* @throws Exception
*/
public static String savePictoServer(String base64String,String path,String imgName)throws Exception{
BASE64Decoder decoder = new sun.misc.BASE64Decoder();
byte[] bytes1 = decoder.decodeBuffer(base64String);
ByteArrayInputStream s = new ByteArrayInputStream(bytes1);
BufferedImage bi1 =ImageIO.read(s);

Date timeCur = new Date();
SimpleDateFormat fmtYY = new SimpleDateFormat("yyyy");
SimpleDateFormat fmtMM = new SimpleDateFormat("MM");
SimpleDateFormat fmtDD = new SimpleDateFormat("dd");
String strYY = fmtYY.format(timeCur);
String strMM = fmtMM.format(timeCur);
String strDD = fmtDD.format(timeCur);

String realPath = path+"/"+strYY+"/"+strMM+"/"+strDD;

File dir=new File(realPath);
if(!dir.exists()){
dir.mkdirs();
}
String fileName=path+"\\"+strYY+"\\"+strMM+"\\"+strDD +"\\"+imgName+".gif";
File w2 = new File(fileName);//可以是jpg,png,gif格式
ImageIO.write(bi1, "jpg", w2);//不管輸出什麼格式圖片,此處不需改動

return fileName;
}

public static void main(String[] args) throws Exception {
System.out.println(getImageFromServer("001001.gif","d:"));
}
}

10. Java 圖片base64編碼是對圖片存放路徑進行編碼還是對圖片本身位元組進行編碼

對圖片本身位元組進行編碼。你可以完成編碼後,把圖片刪除。拿著對應的編碼,解碼後還是能得到對應圖片的,所以可以證明以上結論。

熱點內容
天翼電視伺服器地址是啥 發布:2024-04-24 12:21:47 瀏覽:278
pythonstackless 發布:2024-04-24 11:20:18 瀏覽:123
高壓縮比發動機 發布:2024-04-24 11:13:16 瀏覽:345
數獨c語言 發布:2024-04-24 11:05:10 瀏覽:916
sql2008外網訪問 發布:2024-04-24 10:34:20 瀏覽:576
如何在伺服器中添加字 發布:2024-04-24 10:21:43 瀏覽:362
代寫Python 發布:2024-04-24 10:19:08 瀏覽:769
安卓手機如何破九宮鎖 發布:2024-04-24 10:05:47 瀏覽:678
攝像頭要什麼樣的配置好 發布:2024-04-24 09:30:24 瀏覽:365
存儲過程定義多個變數 發布:2024-04-24 09:04:13 瀏覽:762