有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

将文件上载到FTP服务器时发生java异常

我试图做的是使用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。净进口

我发现了一个错误:

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)

共 (2) 个答案

  1. # 1 楼答案

    FtpURLConnection.getOutputStream中引发异常的代码是:

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

    如您所见,问题在于从URL解析出来的filename组件为空或缺失

    最可能的原因是您为upload方法提供了不合适的参数。不幸的是,您还没有包含调用该方法的代码,所以我不能再进一步了

  2. # 2 楼答案

    调用函数时,您确定要传递的参数FileInputStream is不为空吗