首页 新闻 会员 周边

new FileOutputStream(file)导致tomcat突然挂掉

0
悬赏园豆:15 [已解决问题] 解决于 2020-06-04 10:55

new FileOutputStream(file)导致tomcat突然挂掉,有遇到过类似问题的吗
并没有接到抛出异常,直接就中止服务了。
想问下有遇到过类似问题的,请指教下,谢谢。

无殇-x的主页 无殇-x | 初学一级 | 园豆:9
提问于:2019-05-21 10:00

文件有多大?

dudu 4年前

@dudu: 大约几百k的文件,但是调用频率有点频繁,这个有影响吗

萧_无殇 4年前
< >
分享
最佳答案
0

有没有放在 try 中?

try (OutputStream out = new FileOutputStream(file)) {
    //...
}
收获园豆:15
dudu | 高人七级 |园豆:31003 | 2019-05-21 11:03

public static Writer out = null;

try{

        FileOutputStream fileOutputStream = new FileOutputStream(file);
        
    //后边的都没走
        
        
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"UTF-8");
        
        
        out = new BufferedWriter(outputStreamWriter);
        
        template.process(dataMap,out);
        
        
    }catch(Exception e){
        e.printStackTrace();
       
        throw new RuntimeException(e);
    }finally {
        //如果out不是null,才需要close()
        if (out != null){
            try {
                
                out.flush();
                out.close();
                out = null;
                
            } catch (IOException e) {
                
                e.printStackTrace();
            }
        }
    }

放啦,在try里的 ,还有finally,可是没有抛出异常,连finally都没走,就是走到这个语句之后服务就直接断了,后边的都没走。

无殇-x | 园豆:9 (初学一级) | 2019-05-21 11:09
其他回答(2)
0

加个锁试试

三人乐乐 | 园豆:4819 (老鸟四级) | 2019-05-22 13:21
0

这个问题后来解决没?我也遇到这个问题了。。。

sunshine_wei | 园豆:202 (菜鸟二级) | 2019-09-16 19:59

把系统中所有的io流都记得关掉,有很多流没关导致的这个问题

支持(1) 反对(0) 无殇-x | 园豆:9 (初学一级) | 2020-06-04 10:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册