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

时间:2018-01-16 13:41:40

标签: java ftp stream apache-commons-net

我从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不为null)

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

任何人都知道发生了什么事吗?

1 个答案:

答案 0 :(得分:1)

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

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

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

我并未声称这是您获得的例外的根本原因。但这很有可能。即使没有,你的代码无论如何也无法工作,所以试图在当前状态下修复它是没有意义的。