同时从 FTP 读取文件并将文件保存到 FTP

Reading file from and saving file to FTP at the same time

我在从 FTP 下载文件、在内存中处理它,然后将几个文件保存到同一个 FTP 服务器时遇到问题。

我想要实现的目标:我想下载 csv 文件(比如 1000 行),将其拆分为较小的文件(每个 100 行)并将它们保存到不同的目录。

我已成功连接到 FTP,已成功下载文件并对其进行处理,但我无法保存那些较小的文件。

代码:

public boolean getFile1(final String filePath, final String fileName) {
    String remoteFile = filePath + "/" + fileName;
    BufferedReader reader = null;
    try{
        InputStream is = ftpClient.retrieveFileStream(remoteFile);
        reader = new BufferedReader(new InputStreamReader(is));
        String line;
        String file ="";
        int i = 1;
        int max = 100;
        int current = 0;
        while((line = reader.readLine()) != null){
            if(current < max){
                file+=line+"\n";
                current++;
            }
            else{
                System.out.println("Else; File: " +file);
                InputStream stream = new ByteArrayInputStream(file.getBytes(StandardCharsets.UTF_8.name()));

                putFile(stream, "test", "MOCK_COPY"+i+".csv");

                i++;
                file = "";
                current = 0;
                file+=line+"\n";
            }
        }
        if(!file.isEmpty()){
            System.out.println("End File: " +file);
            InputStream is2 = IOUtils.toInputStream(file, "UTF-8");

            putFile(is2, "test", "MOCK_COPY"+i+".csv");

        }
    } catch (IOException e){
        e.printStackTrace();
    } finally {
        try{
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    return true;
}

public Boolean putFile(InputStream inputStream, final String filePath, final String fileName) throws IOException {
    Long ts = System.currentTimeMillis();
    String remoteFile = filePath + "/" + fileName;

    System.out.println("Start uploading file");

//        ftpClient.changeWorkingDirectory("test");
////this overwrites the existing file
//        ftpClient.storeFile(remoteFile, inputStream);
//inputStream.close();

    OutputStream outputStream = ftpClient.storeFileStream(remoteFile);
    byte[] bytesIn = new byte[4096];
    int read = 0;
    while ((read = inputStream.read(bytesIn)) != -1) {
        outputStream.write(bytesIn, 0, read);
    }
    inputStream.close();
    outputStream.close();

    System.out.println("The file is uploaded successfully in " + (System.currentTimeMillis() - ts) / 1000 + "s");
    return true;
}

连接:

public void connectToServer(String server, String user, String pass, Integer port) 
        throws WebApplicationException {
    port = port == null ? 21 : null;
    server = server.replace("ftp://","");



    try {
        ftpClient = new FTPClient();
        ftpClient.connect(server, port);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        System.out.println("Connected to ftp");
    }
    catch (IOException e) {
        e.printStackTrace();
        throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)
                .entity("FTP error: " + e.getMessage()).type(MediaType.TEXT_PLAIN).build());
    }
}

我从 OutputStream 收到空指针异常(但 ftpCLient 和 remotefile 不为空)

我观察到,如果我只尝试使用该方法上传文件,一切都会很顺利。

有人知道发生了什么事吗?

使用 FTP 协议,您无法通过同一连接同时下载和上传。

  • 要么将下载和上传过程分开。

  • 或者打开两个连接,一个用于下载,一个用于上传。


我并不是说这是您遇到异常的根本原因。但这是很有可能的。即使没有,您的代码也无法正常工作,因此没有必要尝试将其修复为当前状态。