我有以下將大型映像寫入磁盤的代碼。我的應用程序服務器正在運行的內存,我想知道如果我能以某種方式優化如下:優化將字節數組映像寫入磁盤的代碼
public void writeImgToDisk(byte[] base64AttachmentInBytes, String dmxi){
String destinationAndFileName = "";
String fileNameMinusExtension = getCurrentTimeStampForFileNaming();
String extension="";
try {
TikaConfig config = TikaConfig.getDefaultConfig();
InputStream stream = new ByteArrayInputStream(base64AttachmentInBytes);
MediaType mediaType = config.getMimeRepository().detect(stream, new Metadata());
MimeType mimeType;
try {
mimeType = config.getMimeRepository().forName(mediaType.toString());
extension = mimeType.getExtension();
System.out.println("Extension is:"+ extension);
File folder = new File("Z:\\images\\"+ sref);
if (!folder.exists()){
folder.mkdir();
}
destinationAndFileName = "Z:\\images\\"+ dmxi + "\\" + fileNameMinusExtension+ extension;
System.out.println("destinationAndFileName is:"+ destinationAndFileName);
} catch (MimeTypeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream input = new ByteArrayInputStream(base64AttachmentInBytes);
OutputStream output = new FileOutputStream(destinationAndFileName);
IOUtils.copy(input, output);
System.out.println("It was written..");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
這工作,但它是一個瓶頸,在高容量呼叫的原因。
CodeReview.stackexchange.com將是一個很好的地方。 – Qix
而不是在內存中有整個字節數組(base64AttachmentInBytes),然後繞着'read'ing循環說8K字節,然後寫入它們。即直接使用'ByteArrayInputStream'上的方法而不是'IOUtils' –
請參閱此頁上的第一個示例http://www.studytrails.com/java-io/byte-reading-writing.jsp –