java ftp上传空文件

时间:2018-06-04 09:57:50

标签: java file-upload ftp

我正在尝试上传到我的服务器一个简单的文本文件~120b

我可以看到程序进入我的服务器并创建文件 - 但不会复制

中的数据

它只是创建一个空的txt文件 (代码也是清除旧数据并将新数据写入文件 - 但我不认为这是问题) 我错过了什么\做错了?

long upload = TimeBetweenDates(SendTime,Time);

            if (upload > 10)

            {
                String udp = String.join("!",
                date,
                time,
                status,
                final,
                );

             //   SendUDP(udp);

                output.append ( udp );

                UploadFTP();

               output.close();


                //this part for clearing the data in the file
                PrintWriter writer = new PrintWriter(PathOfFile);
                writer.print("");
                writer.close();

                SendTime = Calendar.getInstance();
            }

  public static void UploadFTP ()
    {
        String ftpUrl = "ftp://%s:%s@%s/%s";
        String host = "10.0.0.1";
        String user = "FTP";
        String pass = "FTP";
        String filePath="/home/pi/SendData.log";
        String uploadPath = "/car.txt";

        ftpUrl = String.format(ftpUrl,user,pass,host,uploadPath);
        System.out.println("Upload URL:  " + ftpUrl);
        try
        {
            URL url = new URL(ftpUrl);
            URLConnection conn = url.openConnection();
            OutputStream outputStream = conn.getOutputStream();
            FileInputStream inputStream = new FileInputStream(filePath);

            byte[] buffer = new byte [4096];
            int bytesRead = -1;
            while ((bytesRead = inputStream.read(buffer)) != -1)
            {
                outputStream.write(buffer,0,bytesRead);
            }

            inputStream.close();
            outputStream.close();
            System.out.println("File Upload Finish!");
        }


        catch (IOException ex)
        {
            ex.printStackTrace();
        }



        }
  • 运行代码时没有出现任何错误
  • 如果我调试它,我可以看到文件在清除之前是123b而在之后是0b(所以代码没问题)

0 个答案:

没有答案