1

私がやろうとしているのは、単純なテキスト ファイルを Java コードを使用して 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)
4

2 に答える 2

0

FtpURLConnection.getOutputStream例外をスローするコードは次のとおりです。

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

ご覧のとおり、問題はfilename、URL から解析されたコンポーネントが空または欠落していることです。

最も可能性の高い原因は、メソッドに不適切なパラメーターを指定しているuploadことです。残念ながら、そのメソッドを呼び出すコードが含まれていないため、これ以上先に進むことはできません。

于 2012-04-23T11:04:26.577 に答える
0

FileInputStream is関数を呼び出すときに、渡すパラメーター が null ではないことを確認してください。

于 2012-04-23T11:30:30.153 に答える