0
訪問包含在Stripe中的FileBean中的File對象的最佳方式是什麼?我試圖將文件存儲在亞馬遜的S3中,它需要一個字節數組。似乎很簡單,如果我可以到達File對象。Stripes框架,Filebean和File對象
訪問包含在Stripe中的FileBean中的File對象的最佳方式是什麼?我試圖將文件存儲在亞馬遜的S3中,它需要一個字節數組。似乎很簡單,如果我可以到達File對象。Stripes框架,Filebean和File對象
FileBean
有一個getInputStream()
方法,它允許讀取FileBean的每個字節。如果你真的想把所有內容都存儲在字節數組中(這是一個壞主意,尤其是如果文件可能很大),那麼從流中讀取evrything並將其寫入ByteArrayOutputStream:
byte[] buffer = new byte[1024];
InputStream in = fileBean.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
int read;
while ((read = in.read(buffer)) >= 0) {
out.write(buffer, 0, read);
}
byte[] contentAsByteArray = out.toByteArray();