我有以下将大型映像写入磁盘的代码。我的应用程序服务器正在运行的内存,我想知道如果我能以某种方式优化如下:优化将字节数组映像写入磁盘的代码
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 –