imageIO.write问题 用imageIO.write(imgFile,‘jpg’,new FILE("D:/YANG.JPG")) 写文件失败 抛异常之后我想删除删除生成的0字节的yang.jpg文件 也在下面做了方法可是在有的机器上能删除有的机器上删除不了看了API说要关闭流可是不知道怎么关闭 有高手指教一下吗谢谢啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 此回复为自动发出,仅用于显示而已,并无任何其他特殊作用楼主【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 % 敬礼! 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(); } } 你的img是个什么东西??????????? 是个图片类型的文件 不过为了特意造成他报错 是由EXCEL文件更改后缀名而来的 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; } 代码问题多多,看注释 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(); } } 你原来写的finally删了,根本没用!!!! 其实是 无论有没有生成0字节文件都要删除的 因为那个DUMMY文件 只是个替身文件 后边还有个方法上传真正的文件 目的主要是 验证上传是否成功的 我试试 把DELFILE和DELFOLD方法用TRY CATCH包起来 效果如何。 我不明白你为什么要delFold,这个要是人家本来就有的目录呢,也删除??? 不是原来有的 是我后来创建的 如果里面有除了DUMMY的文件 就不删除 如果没有 就删除 就是这个意思 楼上的同志们。。都几年了还没解决? ImageIO.write里的第三个参数是OutputStream OK? 你可以这样 ImageIO.write(img,'jpg',new DataOutputStream(new File("path"))); 浮躁啊看下API先 ls的同志,ImageIO.write里的第三个参数有3种形式的 服务器端使用java.nio:远程主机强迫关闭了一个现有的连接 "a|b".split("|").length 显视不了绘制图 beanshell的问题 再问eclipse编写junit的问题? Jar双击运行问题! 关于JEditPane的使用问题 菜鸟问题:请问如何设定鼠标指针到一个控件上? 200分 请教: 按行读文件,每一行中以TAB分隔各个字段,有些字段是中文.在线等待 如何设置package路径 JTree节点编辑问题 能否在java中执行一些创建数据库,表之类的sql
楼主【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 %
敬礼!
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();
}
}
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;
}
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();
}
}