java - 上传文件到 FTP 服务器时出现异常

标签 java ftp

我想做的是使用 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)

最佳答案

FtpURLConnection.getOutputStream中抛出异常的代码是:

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

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

最可能的原因是您向 upload 方法提供了不适当的参数。不幸的是,您没有包含调用该方法的代码,因此我无法进一步进行。

关于java - 上传文件到 FTP 服务器时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10278752/

相关文章:

java - 需要将字符串转换为我可以发送的 Intent 吗?

php - 在不影响用户的情况下推送网站更新

java - LRU 和 MRU 缓存逐出策略的排序键数据结构

java - 如何模拟快餐店的顾客

angularjs - 将 gulp 项目部署到 azurewebsites

linux - 使用 bash 脚本批量重命名 FTP 服务器上的文件

html - 生产网站的图片 GET 请求

java - FTPClient 无法打开文件,没有那个文件或目录

java - 一个类怎么会有自己类型的成员,这不是无限递归吗?

java - generatedId = true 的 getDao 异常