0
對於Spring Rest API,我試圖在Request Body中發送Base 64編碼字符串中的圖像數據 - 我將使用以下方法將其解碼並存儲爲服務器文件系統中的圖像文件。在Rest API中發送圖像文件數據
public static BufferedImage decodeToImage(String image) {
BufferedImage image = null;
byte[] imageByte;
try {
BASE64Decoder decoder = new BASE64Decoder();
imageByte = decoder.decodeBuffer(image);
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
image = ImageIO.read(bis);
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
編碼方法,
public static String encodeToString(BufferedImage image, String type) {
String imageString = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ImageIO.write(image, type, bos);
byte[] imageBytes = bos.toByteArray();
BASE64Encoder encoder = new BASE64Encoder();
imageString = encoder.encode(imageBytes);
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return imageString;
}
然而,對於100kb的尺寸的圖像文件 - Base 64編碼的字符串的長度爲1萬個字約這個龐大的數據在其餘的API請求體中是不可行的。
有沒有更好的方式發送圖像數據/上傳圖像到服務器使用休息Web服務?
_「對於100 KB大小的圖像文件 - 基64的長度編碼的字符串爲100萬個字符」 _你做錯了什麼,膨脹係數是4/3(六個輸入位爲8位編碼)。由於您沒有顯示編碼代碼,所以我們無法幫助您。 –
100kb文件= 100 * 1024 * 8位= 819,200,其中4/3擴展因子大約爲100萬。 –
標準方法是傳遞多部分文件 –