K
K
kirawa2015-10-06 13:17:29
Java
kirawa, 2015-10-06 13:17:29

How to upload a list of files to an Ftp server?

This code stops working after transferring the first file. Help me figure out what's wrong.

@Override
    protected Void doInBackground(Void... voids){
        FTPClient client = new FTPClient();
        FileInputStream fis = null;
        OutputStream outputStream;
        byte[] buffer = new byte[BUFFER_SIZE];
        int totalBytesRead = 0;
        try {
            client.connect(host,port);
            client.login(username, password);
            client.setFileType(FTP.BINARY_FILE_TYPE);
            for (File file : uploadFile){
                fis = new FileInputStream(file);
                outputStream = client.storeFileStream(file.getName());
                int bytesRead;
                while ((bytesRead = fis.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                    totalBytesRead += bytesRead;
                    int percentCompleted = (totalBytesRead * 100 / length);
                    Log.d("PROGRESS", String.valueOf(totalBytesRead)+"  "+String.valueOf(percentCompleted));
                    onProgressUpdate(totalBytesRead);
                }
            }
            client.logout();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya, 2015-10-06
@iissakin

I bet on the absence of outputStream.flush();

S
sivabur, 2015-10-06
@sivabur

Try this www.codejava.net/java-se/networking/ftp/java-ftp-f...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question