2013-05-31 20 views
0

我想爲文件上傳測試一個示例程序,但它顯示錯誤「FileNotFoundException」。FileUpload在Java-Windows中拋出錯誤的方法

import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 



public class TestUpload { 

    /** 
    * @param args 
    */ 
    public boolean handleFileUpload(){ 


     BufferedOutputStream bos = null; 
     BufferedInputStream bis = null; 
     boolean isFileUplodedCorrectly = true; 
     try { 
      bos = new BufferedOutputStream(new FileOutputStream(new File("D:\\vishu.jpeg"))); 
      bis = new BufferedInputStream(new FileInputStream(new File("D:\\vishuGreetings.jpeg"))); 


      byte[] b = new byte[1024]; 

      while (bis.read(b) != -1) 
       bos.write(b); 
      bos.flush(); 
     } catch (Exception e) { 
      isFileUplodedCorrectly = false; 
      e.printStackTrace(); 
      System.out.print("Exception in FileUpload Utils " + e); 
     } finally { 
      try { 
       bos.close(); 

      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 
     return isFileUplodedCorrectly; 

    } 

public static void main(String[] args) { 
     // TODO Auto-generated method stub 
    TestUpload tu=new TestUpload(); 
    System.out.println("Status"+tu.handleFileUpload()); 

    } 

} 

實際上文件存在那裏。請檢查。

java.io.FileNotFoundException: D:\vishuGreetings.jpeg (The system cannot find the file specified) 
    at java.io.FileInputStream.open(Native Method) 
    at java.io.FileInputStream.<init>(FileInputStream.java:106) 
    at TestUpload.handleFileUpload(TestUpload.java:23) 
    at TestUpload.main(TestUpload.java:52) 
Exception in FileUpload Utils java.io.FileNotFoundException: D:\vishuGreetings.jpeg (The system cannot find the file specified)Statusfalse 

文件d:/vishuGreetings.jpeg存在there.But我得到未發現異常的same.Please檢查所提供的代碼並恢復文件。

回答

0

我解決了問題,給了.jpg而不是jpeg,發現它工作正常。當我檢查文件的屬性時,它是jpeg。這就是爲什麼我在代碼中的文件nmae中使用擴展名jpeg的原因。

相關問題