邮件收取时,我把邮件信息上传到服务器,使用io操作,但如果3MB都会出现内存溢出 ,希望大家给点建议,怎么在读取时清除未使用内存
                  File storefile = new File(uploadPath+"/"+fileName);
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(storefile));
bis = new BufferedInputStream(in);
while ( bis.read() != -1) {
bos.write(bis.read());
}
bos.flush();
} catch (Exception exception) {
throw new Exception("文件保存失败!");
} finally {
bos.close();
bis.close();
}

解决方案 »

  1.   

    代码有问题吧.
    try {
    bos = new BufferedOutputStream(new FileOutputStream(storefile));
    bis = new BufferedInputStream(in);
    byte[] buf=new byte[4096];
    int len=-1
    while ((len=bis.read(buf)) != -1) {
    bos.write(buf,0,len);
    }
    } catch (Exception exception) {
    throw new Exception("文件保存失败!");
    } finally {
    bos.close();
    bis.close();
    }
      

  2.   

    上面的代码试了  读取一个5.34M的文件,再读 3M的文件就出现,请问一下nio 有具体的例子吗
      

  3.   

    虚拟机内存都加到1G了         -Xms256m -Xmx1024m