最近写了一个邮件客户端
想达成foxmail这种效果,第一次查看邮件的时候,将邮件附件保存到本地
//上面代码省略
File saveFile = new File(savePath + name + extName);
InputStream ipt=bodyPart.getInputStream();
OutputStream out=new FileOutputStream(saveFile);
int count = bodyPart.getSize();
System.out.println(count)
long start = System.currentTimeMillis();
int b = 0;
byte[] buffer = new byte[count];
while (b != -1){
b = ipt.read(buffer);
out.write(buffer);
}
ipt.close();
out.close();
out.flush();
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end - start) + "毫秒");
Console 输出:
mailtest.zip
6252780
耗时:15091毫秒
一个4M大小的附件就要十几秒,请问我代码哪些地方有问题?还能怎么优化???
io inputstream javamail
------解决方案--------------------
试试这个吧。5.5M 耗时:779毫秒
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
BufferedInputStream inBuff = null;
BufferedOutputStream outBuff = null;
try {
// 新建文件输入流并对它进行缓冲
inBuff = new BufferedInputStream(new FileInputStream(
"D:\\soft\\apache-tomcat-6.0.20.exe"));
// 新建文件输出流并对它进行缓冲
outBuff = new BufferedOutputStream(new FileOutputStream(
"D:\\test\\test"));
// 缓冲数组
byte[] b = new byte[1024];
int len;
while ((len = inBuff.read(b)) != -1) {
outBuff.write(b, 0, len);
}
// 刷新此缓冲的输出流
outBuff.flush();
} finally {
// 关闭流
if (inBuff != null)
inBuff.close();
if (outBuff != null)
outBuff.close();
}
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end - start) + "毫秒");
}