用imageIO.write(imgFile,‘jpg’,new FILE("D:/YANG.JPG")) 写文件失败 抛异常之后我想删除删除生成的0字节的yang.jpg文件 也在下面做了方法可是在有的机器上能删除有的机器上删除不了看了API说要关闭流可是不知道怎么关闭 有高手指教一下吗谢谢啊

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【fushunyang】截止到2008-07-18 08:52:21的历史汇总数据(不包括此帖):
    发帖的总数量:5                        发帖的总分数:100                      每贴平均分数:20                       
    回帖的总数量:5                        得分贴总数量:3                        回帖的得分率:60%                      
    结贴的总数量:5                        结贴的总分数:100                      
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:100.00%               结分的百分比:100.00%                  
    无满意结贴率:0.00  %               无满意结分率:0.00  %                  
    敬礼!
      

  2.   

    BufferedImage wideRangeFrontImage = null;

    try {
    wideRangeFrontImage = ImageIO.read(img.getInputStream());
    //dummy file保存
    ImageIO.write(wideRangeFrontImage, "jpg", new File(path+"/"+"dummy.jpg")); } catch (Exception e) {
    System.out.println("===== UnexpectedException Happen! =====");
    System.out.println("Date : " + new Date());
    System.out.println("method :  VisibilityPhotoUploadLogic.commitLogicExecute" );
    e.printStackTrace();
    System.out.println("============ Error Log End  ===========");
    throw e;
    }finally{
    //dummy file 削除
    delFile(path+"/"+"dummy.jpg");
    delFold(path);
    }
     }
    /**
     * 
     *  fold削除
     * 
     */
    public void delFold(String filepath) throws IOException{
    File f = new File(filepath);     
    if(f.exists() && f.isDirectory()){
        if(f.listFiles().length==0){
            f.delete();
        }
    }
    }
    /**
     * 
     *  dummy file削除
     * 
     */
    public void delFile(String filepath) throws IOException{
    File f = new File(filepath);     
    if(f.exists() ){
            f.delete();
    }
    }
      

  3.   

    你的img是个什么东西???????????
      

  4.   

    是个图片类型的文件  不过为了特意造成他报错  是由EXCEL文件更改后缀名而来的
      

  5.   


    public boolean commitLogicExecute() {

    boolean result = false;

    FormFile wideRangeFrontFile = null;
    FormFile wideRangeLeftFile = null;
    FormFile wideRangeRightFile = null;
    FormFile closeRangeFrontFile = null;
    FormFile closeRangeLeftFile = null;
    FormFile closeRangeRightFile = null;
    try {
    //アップロードPATHを取得する
    getPath(); wideRangeFrontFile = (FormFile) this.form.get("wideRangeFrontFile");
    wideRangeLeftFile = (FormFile) this.form.get("wideRangeLeftFile");
    wideRangeRightFile = (FormFile) this.form.get("wideRangeRightFile");
    closeRangeFrontFile = (FormFile) this.form.get("closeRangeFrontFile");
    closeRangeLeftFile = (FormFile) this.form.get("closeRangeLeftFile");
    closeRangeRightFile = (FormFile) this.form.get("closeRangeRightFile");
    validateImg(wideRangeFrontFile); validateImg(wideRangeLeftFile); validateImg(wideRangeRightFile); validateImg(closeRangeFrontFile); validateImg(closeRangeLeftFile); validateImg(closeRangeRightFile);



    result = true;

    } catch (Exception e) {
    System.out.println("===== UnexpectedException Happen! =====");
    System.out.println("Date : " + new Date());
    System.out.println("method :  VisibilityPhotoUploadLogic.commitLogicExecute" );
    e.printStackTrace();
    System.out.println("============ Error Log End  ===========");
    } finally {

    if (wideRangeFrontFile != null) {
    wideRangeFrontFile.destroy();
    wideRangeFrontFile = null;
    }
    if(wideRangeLeftFile != null){
    wideRangeLeftFile.destroy();
    wideRangeLeftFile = null;
    }
    if(wideRangeRightFile != null){
    wideRangeRightFile.destroy();
    wideRangeRightFile = null;
    }
    if(closeRangeFrontFile != null){
    closeRangeFrontFile.destroy();
    closeRangeFrontFile = null;
    }
    if(closeRangeLeftFile != null){
    closeRangeLeftFile.destroy();
    closeRangeLeftFile = null;
    }
    if(closeRangeRightFile != null){
    closeRangeRightFile.destroy();
    closeRangeRightFile = null;
    }
    }


    return result;

    }
      

  6.   

    代码问题多多,看注释
            BufferedImage wideRangeFrontImage = null;        try
            {
                wideRangeFrontImage = ImageIO.read(img.getInputStream());
                // dummy file保存
                ImageIO.write(wideRangeFrontImage, "jpg", new File(path + "/" + "dummy.jpg"));
            }
            catch(Exception e)
            {
                System.out.println("===== UnexpectedException Happen! =====");
                System.out.println("Date : " + new Date());
                System.out.println("method :  VisibilityPhotoUploadLogic.commitLogicExecute");
                System.out.println("============ Error Log End  ===========");
                
                //我不知道你到底想干嘛,删除文件和删除目录你都放在finally里,意思就是不管有没有异常,最后都要执行这两个方法
                //但是看你帖子里写的解释好像只是有异常产生了才删除那个0字节的文件,那就必须放在catch里面
                //===================================================
                try
                {
                    //该方法声明的时候throws IOException,所以在调用的时候必须用try...catch包起,否则编译不通过
                    delFile(path + "/" + "dummy.jpg");
                }
                catch(IOException ie)
                {
                    e.printStackTrace();
                }            try
                {
                    //该方法声明的时候throws IOException,所以在调用的时候必须用try...catch包起,否则编译不通过
                    delFold(path);
                }
                catch(IOException ie)
                {
                    e.printStackTrace();
                }
                //===================================================            /*这里throw e必须要用try...catch块包起,否则编译不通过,但是我不明白这一块你想干什么,完全没有必要的!!
                try
                {
                    throw e;
                }
                catch(Exception e1)
                {
                    e1.printStackTrace();
                }
                */
                
                e.printStackTrace();
            }        
        
        /**
         * fold削除
         */
        public void delFold(String filepath) throws IOException
        {
            File f = new File(filepath);        if(f.exists() && f.isDirectory())
            {
                if(f.listFiles().length == 0)
                {
                    f.delete();
                }
            }
        }    /**
         * dummy file削除
         */
        public void delFile(String filepath) throws IOException
        {
            File f = new File(filepath);        if(f.exists())
            {
                f.delete();
            }
        }
      

  7.   

    你原来写的finally删了,根本没用!!!!
      

  8.   

    其实是 无论有没有生成0字节文件都要删除的  因为那个DUMMY文件 只是个替身文件   后边还有个方法上传真正的文件 目的主要是 验证上传是否成功的 我试试 把DELFILE和DELFOLD方法用TRY CATCH包起来 效果如何。
      

  9.   

    我不明白你为什么要delFold,这个要是人家本来就有的目录呢,也删除???
      

  10.   

    不是原来有的  是我后来创建的 如果里面有除了DUMMY的文件 就不删除 如果没有 就删除 就是这个意思
      

  11.   

    楼上的同志们。。都几年了还没解决? ImageIO.write里的第三个参数是OutputStream OK?   你可以这样    ImageIO.write(img,'jpg',new DataOutputStream(new File("path"))); 浮躁啊看下API先
      

  12.   

    ls的同志,ImageIO.write里的第三个参数有3种形式的