2012-04-23 105 views
0

我想要做的是使用java代碼上傳一個簡單的文本文件到FTP服務器,但出現錯誤。我正在努力使其工作,但無法做到這一點。以下是代碼。將文件上傳到FTP服務器時出現異常

File file = new File("testFile.txt"); 
    FileOutputStream fo = new FileOutputStream(file); 
    PrintStream ps = new PrintStream(fo); 
    ps.println("BLA"); 
    ps.close();`enter code here` 
    fo.close(); 
uploadFile(file,file.getName()); 



public void upload(String ftpServer, String user, String password, 
    String fileName, FileInputStream is) throws MalformedURLException, 
    IOException 

      { 

     log("inside upload..........."); 
     if (ftpServer != null && fileName != null && is != null) 
     { 
      StringBuffer sb = new StringBuffer("ftp://"); 
      // check for authentication else assume its anonymous access. 
      if (user != null && password != null) 
      { 
       sb.append(user); 
       sb.append(':'); 
       sb.append(password); 
       sb.append('@'); 
      } 
      sb.append(ftpServer); 
      sb.append('/'); 
      sb.append(fileName); 
      /* 
      * type ==> a=ASCII mode, i=image (binary) mode, d= file directory 
      * listing 
      */ 
      sb.append(";type=i"); 

      BufferedInputStream bis = null; 
      BufferedOutputStream bos = null; 
      try 
      { 
       URL url = new URL(sb.toString()); 
       URLConnection urlc = url.openConnection(); 

       log("urlc::1 "+urlc); 

       bos = new BufferedOutputStream(urlc.getOutputStream()); 
       log("bos:: "+bos); 

       bis = new BufferedInputStream(is); 

       int i; 
       // read byte by byte until end of stream 
       while ((i = bis.read()) != -1) 
       { 
        log("i"+i); 
        bos.write(i); 
       } 
      } 
      finally 
      { 
       if (bis != null) 
        try 
       { 
         bis.close(); 
       } 
       catch (IOException ioe) 
       { 
        ioe.printStackTrace(); 
       } 
       if (bos != null) 
        try 
       { 
         bos.close(); 
       } 
       catch (IOException ioe) 
       { 
        ioe.printStackTrace(); 
       } 
      } 
     } 
     else 
     { 
      log("Input not available."); 
     } 
      } 

有關更多詳細信息,我正在使用java.net導入。

我得到錯誤:

Exception e is :: java.io.IOException: illegal filename for a PUT 
    at sun.net.www.protocol.ftp.FtpURLConnection.getOutputStream(Unknown Source) 
    at ToolFileUpload.upload(ToolFileUpload.java:72) 
    at APInterfaceTool.uploadFile(APInterfaceTool.java:861) 
    at APInterfaceTool.createInvoiceTextFile(APInterfaceTool.java:613) 
    at APInterfaceTool.generateOutBoundExtract(APInterfaceTool.java:426) 
+0

'java.io.IOException:PUT的非法文件名' 只要您的文件是'null'或其名稱的長度爲'0',就會發生。在這些線上進行調試。 – Dilawar 2012-04-23 10:26:53

+1

我發現問題是與FTP服務器。我在其他FTP服務器上嘗試了相同的代碼,它工作正常。謝謝大家的幫助 – 2012-04-23 13:04:54

回答

0

FtpURLConnection.getOutputStream拋出異常的代碼是:

decodePath(url.getPath()); 
if (filename == null || filename.length() == 0) { 
    throw new IOException("illegal filename for a PUT"); 
} 

正如你所看到的,問題是,已經被解析出來的filename組件的網址爲空或缺失。

最可能的原因是您正在向upload方法提供不適當的參數。不幸的是,你沒有包含調用該方法的代碼,所以我不能再進一步了。

+0

我從日誌中檢查過既沒有文件名也沒有文件長度爲零。此外,我已將整個方法放在上面。它是一種名爲上傳的方法。 – 2012-04-23 11:21:16

+0

我想這個獨立的程序上傳文件到FTP服務器。代碼運行正常,但文件不是在FTP上創建的 – 2012-04-23 11:40:15

+0

非常感謝您的支持。我發現這個問題與FTP服務器相同,因爲相同的代碼在其他FTP服務器上運行良好。 :D – 2012-04-23 12:58:54

0

當你調用該函數時,你確定你傳遞的參數FileInputStream is不爲空嗎?

+0

是的,我檢查它不是null。可能是什麼問題呢 ? – 2012-04-23 11:33:46

+0

它必須是代碼試圖創建一個已經存在的文件。我想[這](http://www.coderanch.com/t/277808/Streams/java/Copy-file-window-machine-Linux)可能會有所幫助。 – 2012-04-23 12:03:12

+0

我發現這個問題與FTP服務器有關。我在其他FTP服務器上嘗試了相同的代碼,它工作正常。感謝大家的幫助。 – 2012-04-23 12:57:53