2013-02-18 87 views
0

我在我的應用程序中編寫了AJAX文件上傳功能。從我的筆記本電腦運行它時,它完美地工作。當我嘗試使用相同的應用程序完全相同的文件,但部署在JBoss服務器上,我得到以下異常:從JSP上傳文件時發生java.io.FileNotFoundException

2013-02-18 11:30:02,796 ERROR [STDERR] java.io.FileNotFoundException: C:\Users\MyUser\Desktop\TestFile.pdf (The system cannot find the file specified). 

getFileData方法:

private byte[] getFileData(File file) { 

    FileInputStream fileInputStream = null; 
    byte[] bytFileData = null; 

    try { 
     fileInputStream = new FileInputStream(file); 
    } catch (FileNotFoundException e1) { 
     e1.printStackTrace(); 
    } 

    if (fileInputStream != null) { 
     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
     byte[] bytBuffer = new byte[1024]; 

     try { 
      for (int readNum; (readNum = fileInputStream.read(bytBuffer)) != -1;) { 
       byteArrayOutputStream.write(bytBuffer, 0, readNum); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     bytFileData = byteArrayOutputStream.toByteArray(); 
    } 

    return bytFileData; 
} 

獲得文件內容的變量(從上面的方法):

byte[] bytFileData = this.getFileData(file); 

把該文件:

private boolean makeFile(File folderToMake, File fileToMake, byte[] bytFileData) { 

    Boolean booSuccess = false; 
    FileOutputStream fileOutputStream = null; 

    try { 

     if (!folderToMake.exists()) { 
      folderToMake.mkdirs(); 
     } 

     if (!fileToMake.exists()) { 

      if (fileToMake.createNewFile() == true) { 

       booSuccess = true; 

       fileOutputStream = new FileOutputStream(fileToMake); 

       fileOutputStream.write(bytFileData); 
       fileOutputStream.flush(); 
       fileOutputStream.close(); 
      } 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
     booSuccess = false; 
    } 

    return booSuccess; 
} 

有什麼想法嗎?

謝謝

查爾斯

回答

2

看來你只是傳遞了文件路徑作爲請求到服務器的一部分,而不是實際上傳文件,然後試圖使用文件路徑來訪問文件。

這將在您的筆記本電腦上工作,因爲代碼在本地運行時,可以訪問您的文件系統,並且能夠找到該文件。它不會在服務器上部署,因爲它是完全獨立的機器,因此無法訪問您的文件系統。

你需要修改客戶端(AJAX)的代碼實際上上傳的文件,然後修改您的服務器端代碼使用該上傳的文件。請注意,AJAX文件上傳通常是不可能的 - 有一些框架的插件,如jQuery,它們使用替代方法來提供這種功能。

我不是100%,但我認爲正確的AJAX文件上傳可以使用HTML5的特性是可能的,但是對於瀏覽器的支持很可能會是相當差的現在。