简体   繁体   中英

Sending photos by ftp

I'm developing a system to perform maintenance of machines, I need to send some photos. When the user submits the photos without close or exit the activity, sending is done successfully. When the user leaves the activity, returning the photos are not sent, the following log:

java.lang.NullPointerException
Rejecting registerization due to +iget-object-quick v7, v9, (#8)
at java.io.File.fixSlashes(File.java:185)
at java.io.File.<init>(File.java:134)
at java.io.FileInputStream.<init>(FileInputStream.java:105)
at br.com.tetsistemas.rcbmanutencoes01.ManutencaoConclusaoActivity.envioFTP(ManutencaoConclusaoActivity.java:600)
at br.com.tetsistemas.rcbmanutencoes01.ManutencaoConclusaoActivity.run(ManutencaoConclusaoActivity.java:549)
at java.lang.Thread.run(Thread.java:856)

Follows the code of the activity:

// Script to send one of the photos

helper.getFoto_plataforma2().setOnClickListener(new OnClickListener() { 
            @Override
            public void onClick(View v) {



                    nomeArquivoPlataforma2 = "Foto_Plataforma2_" + hora_final + ".jpg";

                    localPlataforma2 = STORAGE_SERVICE + "/extSdCard/"
                            + "/RCBManutencoes/" + nomeArquivoPlataforma2 + ".jpg";     


                File arquivo = new File(localPlataforma2);
                //URI que informa onde o arquivo resultado deve ser salvo
                Uri localFoto = Uri.fromFile(arquivo);

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, localFoto);
                startActivityForResult(intent, TAKE_PLATAFORMA2);

            }
        });


protected void onActivityResult(int requestCode, int resultCode, 
            Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //Verificacao do resultado da nossa requisicao
        if (requestCode == TAKE_PLATAFORMA) {
            if (resultCode == Activity.RESULT_OK) {
                helper.carregarFotoPlataforma(this.localPlataforma);
            } else {
                localPlataforma = null;
            }
        }


@Override
    public void run() {

        String diretorioPlataforma = localPlataforma;
        String diretorioPlataforma2 = localPlataforma2;
        String diretorioHorimetro = localHorimetro;
        String diretorioProblema = localProblema;
        String diretorioProblema2 = localProblema2;
        String diretorioCausa = localPlataforma;
        String diretorioCausa2 = localPlataforma2;

        String nomeArqPlataforma = nomeArquivoPlataforma;
        String nomeArqPlataforma2 = nomeArquivoPlataforma2;
        String nomeArqHorimetro = nomeArquivoHorimetro;
        String nomeArqProblema = nomeArquivoProblema;
        String nomeArqProblema2 = nomeArquivoProblema2;
        String nomeArqCausa = nomeArquivoCausa;
        String nomeArqCausa2 = nomeArquivoCausa2;

    final TextView notificacao = (TextView) findViewById(R.id.notificacao);

    try {

        envioFTP("login", "senha", diretorioPlataforma, diretorioPlataforma2, diretorioHorimetro, 
                diretorioProblema, diretorioProblema2, diretorioCausa, diretorioCausa2,
                nomeArqPlataforma, nomeArqPlataforma2, nomeArqHorimetro, nomeArqProblema, nomeArqProblema2, nomeArqCausa, nomeArqCausa2);

        handler.post(new Runnable() {

    public void run() {
        notificacao.setText("Arquivo enviado com sucesso");
        notificacao.setVisibility(View.VISIBLE);
        }
    });

    } finally {
        dialog.dismiss();
        }
    }

    private void envioFTP(String login, String senha, String diretorioPlataforma, String diretorioPlataforma2,
    String diretorioHorimetro, String diretorioProblema, String diretorioProblema2, String diretorioCausa, String diretorioCausa2, 
    String nomeArqPlataforma, String nomeArqPlataforma2, String nomeArqHorimetro, String nomeArqProblema, String nomeArqProblema2, 
    String nomeArqCausa, String nomeArqCausa2) {

        FTPClient ftp = new FTPClient();

        try {

            ftp.connect("url do ftp", 21);
            ftp.login(login, senha);
            ftp.changeWorkingDirectory("MANUTENCOES");  

            FileInputStream arqEnviarPlataforma = new FileInputStream(diretorioPlataforma);
            FileInputStream arqEnviarPlataforma2 = new FileInputStream(diretorioPlataforma2);
            FileInputStream arqEnviarHorimetro = new FileInputStream(diretorioHorimetro);
            FileInputStream arqEnviarProblema = new FileInputStream(diretorioProblema);
            FileInputStream arqEnviarProblema2 = new FileInputStream(diretorioProblema2);
            FileInputStream arqEnviarCausa = new FileInputStream(diretorioCausa);
            FileInputStream arqEnviarCausa2 = new FileInputStream(diretorioCausa2);

            ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE);
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);

            ftp.storeFile(nomeArqPlataforma, arqEnviarPlataforma);
            ftp.storeFile(nomeArqPlataforma2, arqEnviarPlataforma2);
            ftp.storeFile(nomeArqHorimetro, arqEnviarHorimetro);
            ftp.storeFile(nomeArqProblema, arqEnviarProblema);
            ftp.storeFile(nomeArqProblema2, arqEnviarProblema2);
            ftp.storeFile(nomeArqCausa, arqEnviarCausa);
            ftp.storeFile(nomeArqCausa2, arqEnviarCausa2);

            ftp.logout();
            ftp.disconnect();

        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {

            e.printStackTrace();
        }

    }

You should specify a condition if the user sends directly to the server then run this code:

        FileInputStream arqEnviarPlataforma = new FileInputStream(diretorioPlataforma);
        FileInputStream arqEnviarPlataforma2 = new FileInputStream(diretorioPlataforma2);
        FileInputStream arqEnviarHorimetro = new FileInputStream(diretorioHorimetro);
        FileInputStream arqEnviarProblema = new FileInputStream(diretorioProblema);
        FileInputStream arqEnviarProblema2 = new FileInputStream(diretorioProblema2);
        FileInputStream arqEnviarCausa = new FileInputStream(diretorioCausa);
        FileInputStream arqEnviarCausa2 = new FileInputStream(diretorioCausa2);

Else if the user saves locally and decides to send later you should specify some condition!! In your code the above lines get called for both conditions, thats why you get null pointer exception, you should specify a separate logic for sending the information later which you saved locally.
PS: I've noticed you haven't closed the InputStreams !!
you should close all the InputStream objects in a finally block

finally{
arqEnviarHorimetro.close();
// close all others
}

Without seeing your whole code this is just a wild assumption, since you're getting NullPointerException in your second case.

Yes, you are correct, but the logic to save locally is not the same, it follows all the code for this class:

package br.com.tetsistemas.rcbmanutencoes01;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

import br.com.tetsistemas.rcbmanutencoes01.helper.ManutencaoHelper;
import br.com.tetsistemas.rcbmanutencoes01.modelo.bean.Manutencao;
import br.com.tetsistemas.rcbmanutencoes01.modelo.dao.ManutencaoDAO;
import br.com.tetsistemas.rcbmanutencoes1.R;
import br.com.tetsistemas.rcbmanutencoes01.sincronizar.JdbcConnection;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

@SuppressLint("SimpleDateFormat")
public class ManutencaoConclusaoActivity extends Activity implements Runnable{

    private ManutencaoHelper helper;
    static int TAKE_PLATAFORMA = 1;
    static int TAKE_PLATAFORMA2 = 2;
    static int TAKE_HORIMETRO = 3;
    static int TAKE_PROBLEMA = 4;
    static int TAKE_PROBLEMA2 = 5;
    static int TAKE_CAUSA = 6;
    static int TAKE_CAUSA2 = 7;
    private static final String TAG = null;

    private String localPlataforma;
    private String localPlataforma2;
    private String localHorimetro;
    private String localProblema;
    private String localProblema2;
    private String localCausa;
    private String localCausa2;

    private Manutencao manutencaoParaSerAlterada = null;

    Bitmap foto_plataforma;
    Bitmap foto_plataforma2;
    Bitmap foto_horimetro;
    Bitmap foto_problema;
    Bitmap foto_problema2;
    Bitmap foto_causa;
    Bitmap foto_causa2;

    private ProgressDialog dialog;
    private Handler handler = new Handler();
    private Button btFTP;
    private Button btConcluir;
    private Spinner spRetorno;
    private String[] arRetorno = new String[]{"Sim", "Não"};

    private String nomeArquivoPlataforma;
    private String nomeArquivoPlataforma2;
    private String nomeArquivoHorimetro;
    private String nomeArquivoProblema;
    private String nomeArquivoProblema2;
    private String nomeArquivoCausa;
    private String nomeArquivoCausa2;

    private EditText edHoraFinal;
    String hora_final;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conclusao);

        helper = new ManutencaoHelper(this);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, arRetorno);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spRetorno = (Spinner) findViewById(R.id.spPlataformaRetorno);
        spRetorno.setAdapter(adapter);

        if (savedInstanceState != null) {
            localPlataforma = (String) savedInstanceState.getSerializable("localArquivo");
        }
        if (localPlataforma != null) {
            helper.carregarFotoPlataforma(localPlataforma);
        }

        if(getIntent().getStringExtra("cliente")!=null){
            String horimetro = getIntent().getStringExtra("cliente");
            EditText edCliente = (EditText) findViewById(R.id.edCliente);
            edCliente.setText(horimetro);
        }   

        if(getIntent().getStringExtra("plataforma")!=null){
            String horimetro = getIntent().getStringExtra("plataforma");
            EditText EdPlataforma = (EditText) findViewById(R.id.edPlataforma);
            EdPlataforma.setText(horimetro);
        }

        if(getIntent().getStringExtra("horimetro")!=null){
            String horimetro = getIntent().getStringExtra("horimetro");
            EditText edHorimetro = (EditText) findViewById(R.id.edPlataformaHorimetro);
            edHorimetro.setText(horimetro);
        }   

        if(getIntent().getStringExtra("descricao_problema")!=null){
            String descricao_problema = getIntent().getStringExtra("descricao_problema");
            EditText edDescricaoProblema = (EditText) findViewById(R.id.edPlataformaDescricaoProblema);
            edDescricaoProblema.setText(descricao_problema);
        }   

        if(getIntent().getStringExtra("causa_problema")!=null){
            String causa_problema = getIntent().getStringExtra("causa_problema");
            EditText edCausaProblema = (EditText) findViewById(R.id.edPlataformaCausaProblema);
            edCausaProblema.setText(causa_problema);
        }

        if(getIntent().getStringExtra("descricao_peca")!=null){
            String descricao_peca = getIntent().getStringExtra("descricao_peca");
            EditText edDescricaoPeca = (EditText) findViewById(R.id.edPlataformaDescricaoPeca);
            edDescricaoPeca.setText(descricao_peca);
        }   

        if(getIntent().getStringExtra("numero_peca")!=null){
            String numero_peca = getIntent().getStringExtra("numero_peca");
            EditText edNumeroPeca = (EditText) findViewById(R.id.edPlataformaNumeroPeca);
            edNumeroPeca.setText(numero_peca);
        }   

        if(getIntent().getStringExtra("quantidade")!=null){
            String quantidade = getIntent().getStringExtra("quantidade");
            EditText edQuantidade = (EditText) findViewById(R.id.edPlataformaQuantidade);
            edQuantidade.setText(quantidade);
        }   

        if(getIntent().getStringExtra("tipo")!=null){
            String tipo = getIntent().getStringExtra("tipo");
            EditText edTipo = (EditText) findViewById(R.id.edPlataformaTipo);
            edTipo.setText(tipo);
        }   

        if(getIntent().getStringExtra("retorno")!=null){
            String retorno = getIntent().getStringExtra("retorno");
            EditText edRetorno = (EditText) findViewById(R.id.edPlataformaRetorno);
            edRetorno.setText(retorno);
        }   

        if(getIntent().getStringExtra("tecnico")!=null){
            String tecnico = getIntent().getStringExtra("tecnico");
            EditText edTecnico = (EditText) findViewById(R.id.edPlataformaTecnico);
            edTecnico.setText(tecnico);
        }   

        if(getIntent().getStringExtra("hora_final")!=null){
            hora_final = getIntent().getStringExtra("hora_final");
            edHoraFinal = (EditText) findViewById(R.id.edHoraFinal);
            edHoraFinal.setText(hora_final);
        }

        helper.getFoto_plataforma().setOnClickListener(new OnClickListener() {  
            @Override
            public void onClick(View v) {


                    nomeArquivoPlataforma = "Foto_Plataforma_" + hora_final + ".jpg";

                    localPlataforma = STORAGE_SERVICE + "/extSdCard/"
                            + "/RCBManutencoes/Foto_Plataforma_" + hora_final + ".jpg";


                File arquivo = new File(localPlataforma);
                //URI que informa onde o arquivo resultado deve ser salvo
                Uri localFoto = Uri.fromFile(arquivo);

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, localFoto);
                startActivityForResult(intent, TAKE_PLATAFORMA);
            }
        });

        helper.getFoto_plataforma2().setOnClickListener(new OnClickListener() { 
            @Override
            public void onClick(View v) {



                    nomeArquivoPlataforma2 = "Foto_Plataforma2_" + hora_final + ".jpg";

                    localPlataforma2 = STORAGE_SERVICE + "/extSdCard/"
                            + "/RCBManutencoes/" + nomeArquivoPlataforma2 + ".jpg";     


                File arquivo = new File(localPlataforma2);
                //URI que informa onde o arquivo resultado deve ser salvo
                Uri localFoto = Uri.fromFile(arquivo);

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, localFoto);
                startActivityForResult(intent, TAKE_PLATAFORMA2);

            }
        });

        helper.getFoto_horimetro().setOnClickListener(new OnClickListener() {   
            @Override
            public void onClick(View v) {

                    nomeArquivoHorimetro = "Foto_Horimetro_" + hora_final + ".jpg";

                    localHorimetro = STORAGE_SERVICE + "/extSdCard/"
                            + "/RCBManutencoes/Foto_Horimetro_" + hora_final + ".jpg";      


                File arquivo = new File(localHorimetro);
                //URI que informa onde o arquivo resultado deve ser salvo
                Uri localFoto = Uri.fromFile(arquivo);

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, localFoto);
                startActivityForResult(intent, TAKE_HORIMETRO);

            }
        });

        helper.getFoto_problema().setOnClickListener(new OnClickListener() {    

            @Override
            public void onClick(View v) {



                    nomeArquivoProblema = "Foto_Problema_" + hora_final + ".jpg";

                    localProblema = STORAGE_SERVICE + "/extSdCard/"
                            + "/RCBManutencoes/Foto_Problema_" + hora_final + ".jpg";


                File arquivo = new File(localProblema);
                //URI que informa onde o arquivo resultado deve ser salvo
                Uri localFoto = Uri.fromFile(arquivo);

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, localFoto);
                startActivityForResult(intent, TAKE_PROBLEMA);

            }
        });

        helper.getFoto_problema2().setOnClickListener(new OnClickListener() {   

            @Override
            public void onClick(View v) {


                    nomeArquivoProblema2 = "Foto_Problema2_" + hora_final + ".jpg";

                    localProblema2 = STORAGE_SERVICE + "/extSdCard/"
                            + "/RCBManutencoes/Foto_Problema2_" + hora_final + ".jpg";      


                File arquivo = new File(localProblema2);
                //URI que informa onde o arquivo resultado deve ser salvo
                Uri localFoto = Uri.fromFile(arquivo);

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, localFoto);
                startActivityForResult(intent, TAKE_PROBLEMA2);

            }
        });

        helper.getFoto_causa().setOnClickListener(new OnClickListener() {   

            @Override
            public void onClick(View v) {


                    nomeArquivoCausa = "Foto_CausaProblema_" + hora_final + ".jpg";

                    localCausa = STORAGE_SERVICE + "/extSdCard/"
                            + "/RCBManutencoes/Foto_CausaProblema_" + hora_final + ".jpg";


                File arquivo = new File(localCausa);
                //URI que informa onde o arquivo resultado deve ser salvo
                Uri localFoto = Uri.fromFile(arquivo);

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, localFoto);
                startActivityForResult(intent, TAKE_CAUSA);

            }
        });

        helper.getFoto_causa2().setOnClickListener(new OnClickListener() {  

            @Override
            public void onClick(View v) {


                    nomeArquivoCausa2 = "Foto_CausaProblema2_" + hora_final + ".jpg";

                    localCausa2 = STORAGE_SERVICE + "/extSdCard/"
                            + "/RCBManutencoes/Foto_CausaProblema2_" + hora_final + ".jpg";



                File arquivo = new File(localCausa2);
                //URI que informa onde o arquivo resultado deve ser salvo
                Uri localFoto = Uri.fromFile(arquivo);

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, localFoto);
                startActivityForResult(intent, TAKE_CAUSA2);

            }
        });



        manutencaoParaSerAlterada = (Manutencao) getIntent().getSerializableExtra(
                "MANUTENCAO_SELECIONADA");

        if (manutencaoParaSerAlterada != null) {
            // Atualiza a tela com dados da manutencao
            helper.setManutencao(manutencaoParaSerAlterada);


        }

        btConcluir = (Button) findViewById(R.id.sbSalvar);
        btConcluir.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                // Utilizacao do Helper para recuperar dados da Manutencao
                Manutencao manutencao = helper.getManutencao();

                // Criacao do objeto DAO - inicio da conexao com o BD
                ManutencaoDAO dao = new ManutencaoDAO(ManutencaoConclusaoActivity.this);

                // Verificacao para salvar ou cadastrar a manutencao
                if (manutencao.getId() == null) {
                    dao.cadastrar(manutencao);
                } else {
                    dao.alterar(manutencao);
                }
                // Encerramento da conexao com o Banco de Dados
                dao.close();
                // Encerrando a Activity
                finish();
            }
        });

        btFTP = (Button) findViewById(R.id.sbFTP);
        btFTP.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog = ProgressDialog.show(ManutencaoConclusaoActivity.this, "Conectando com o servidor ", "Enviando arquivo, por favor, aguarde...", false, true);
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
                StrictMode.setThreadPolicy(policy);
                 Connection connection = null;

                    PreparedStatement preparedStatement = null;

                    connection = JdbcConnection.getConnection();
                    /*String query = "INSERT INTO ManutencaoTablet ( "
                            + "id, developer, empresa, inativo) VALUES"
                            + "(?,?,?,?)";*/
                    String query = "INSERT INTO ManutencaoTablet ( "
                            + "cliente, maquina, horimetro, descricao_problema, causa_problema, "
                            + "descricao_peca, numero_peca, quantidade, tipo, retorno, tecnico, hora_final)"
                            + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";

                    try {
                        preparedStatement = connection.prepareStatement(query);

                        preparedStatement.setString(1, helper.getManutencao().getCliente());
                        preparedStatement.setString(2, helper.getManutencao().getMaquina());
                        preparedStatement.setString(3, helper.getManutencao().getHorimetro());
                        preparedStatement.setString(4, helper.getManutencao().getDescricao_problema());
                        preparedStatement.setString(5, helper.getManutencao().getCausa_problema());
                        preparedStatement.setString(6, helper.getManutencao().getDescricao_peca());
                        preparedStatement.setString(7, helper.getManutencao().getNumero_peca());
                        preparedStatement.setString(8, helper.getManutencao().getQuantidade());
                        preparedStatement.setString(9, helper.getManutencao().getTipo());
                        preparedStatement.setString(10, helper.getManutencao().getRetorno());
                        preparedStatement.setString(11, helper.getManutencao().getTecnico());
                        preparedStatement.setString(12, helper.getManutencao().getHora_final());
                        preparedStatement.executeBatch();
                        preparedStatement.executeUpdate();


                        preparedStatement.close();
                        connection.close();


                         Log.i(TAG, "INSERÇÃO REALIZADAAAA!!");

                    }catch (SQLException e) {
                         e.printStackTrace();

                    }finally {
                         if (connection != null) {
                             try {
                                 connection.close();
                             } catch (SQLException e) {
                                 e.printStackTrace();

                             }
                         }
                    }
                // Faz o envio do arquivo FTP.
                new Thread(ManutencaoConclusaoActivity.this).start();
            }
        });
    }

    @Override   
    protected void onActivityResult(int requestCode, int resultCode, 
            Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //Verificacao do resultado da nossa requisicao
        if (requestCode == TAKE_PLATAFORMA) {
            if (resultCode == Activity.RESULT_OK) {
                helper.carregarFotoPlataforma(this.localPlataforma);
            } else {
                localPlataforma = null;
            }
        }

        if (requestCode == TAKE_PLATAFORMA2) {
            if (resultCode == Activity.RESULT_OK) {
                helper.carregarFotoPlataforma2(this.localPlataforma2);
            } else {
                localPlataforma2 = null;
            }
        }

        if (requestCode == TAKE_HORIMETRO) {
            if (resultCode == Activity.RESULT_OK) {
                helper.carregarFotoHorimetro(this.localHorimetro);
            } else {
                localHorimetro = null;
            }
        }

        if (requestCode == TAKE_PROBLEMA) {
            if (resultCode == Activity.RESULT_OK) {
                helper.carregarFotoProblema(this.localProblema);
            } else {
                localProblema = null;
            }
        }

        if (requestCode == TAKE_PROBLEMA2) {
            if (resultCode == Activity.RESULT_OK) {
                helper.carregarFotoProblema2(this.localProblema2);
            } else {
                localProblema2 = null;
            }
        }

        if (requestCode == TAKE_CAUSA) {
            if (resultCode == Activity.RESULT_OK) {
                helper.carregarFotoCausa(this.localCausa);
            } else {
                localCausa = null;
            }
        }

        if (requestCode == TAKE_CAUSA2) {
            if (resultCode == Activity.RESULT_OK) {
                helper.carregarFotoCausa2(this.localCausa2);
            } else {
                localCausa2 = null;
            }
        }

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable("localArquivo", localPlataforma);
    }


    @Override
    public void run() {

        String diretorioPlataforma = localPlataforma;
        String diretorioPlataforma2 = localPlataforma2;
        String diretorioHorimetro = localHorimetro;
        String diretorioProblema = localProblema;
        String diretorioProblema2 = localProblema2;
        String diretorioCausa = localPlataforma;
        String diretorioCausa2 = localPlataforma2;

        String nomeArqPlataforma = nomeArquivoPlataforma;
        String nomeArqPlataforma2 = nomeArquivoPlataforma2;
        String nomeArqHorimetro = nomeArquivoHorimetro;
        String nomeArqProblema = nomeArquivoProblema;
        String nomeArqProblema2 = nomeArquivoProblema2;
        String nomeArqCausa = nomeArquivoCausa;
        String nomeArqCausa2 = nomeArquivoCausa2;

    final TextView notificacao = (TextView) findViewById(R.id.notificacao);

    try {

        envioFTP("login", "pass", diretorioPlataforma, diretorioPlataforma2, diretorioHorimetro, 
                diretorioProblema, diretorioProblema2, diretorioCausa, diretorioCausa2,
                nomeArqPlataforma, nomeArqPlataforma2, nomeArqHorimetro, nomeArqProblema, nomeArqProblema2, nomeArqCausa, nomeArqCausa2);

        handler.post(new Runnable() {

    public void run() {
        notificacao.setText("Arquivo enviado com sucesso");
        notificacao.setVisibility(View.VISIBLE);
        }
    });

    } finally {
        dialog.dismiss();
        }
    }

    private void envioFTP(String login, String senha, String diretorioPlataforma, String diretorioPlataforma2,
    String diretorioHorimetro, String diretorioProblema, String diretorioProblema2, String diretorioCausa, String diretorioCausa2, 
    String nomeArqPlataforma, String nomeArqPlataforma2, String nomeArqHorimetro, String nomeArqProblema, String nomeArqProblema2, 
    String nomeArqCausa, String nomeArqCausa2) {

        FTPClient ftp = new FTPClient();

        try {

            ftp.connect("url", 21);
            ftp.login(login, senha);
            ftp.changeWorkingDirectory("MANUTENCOES");  

            FileInputStream arqEnviarPlataforma = new FileInputStream(diretorioPlataforma);
            FileInputStream arqEnviarPlataforma2 = new FileInputStream(diretorioPlataforma2);
            FileInputStream arqEnviarHorimetro = new FileInputStream(diretorioHorimetro);
            FileInputStream arqEnviarProblema = new FileInputStream(diretorioProblema);
            FileInputStream arqEnviarProblema2 = new FileInputStream(diretorioProblema2);
            FileInputStream arqEnviarCausa = new FileInputStream(diretorioCausa);
            FileInputStream arqEnviarCausa2 = new FileInputStream(diretorioCausa2);

            ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE);
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);

            ftp.storeFile(nomeArqPlataforma, arqEnviarPlataforma);
            ftp.storeFile(nomeArqPlataforma2, arqEnviarPlataforma2);
            ftp.storeFile(nomeArqHorimetro, arqEnviarHorimetro);
            ftp.storeFile(nomeArqProblema, arqEnviarProblema);
            ftp.storeFile(nomeArqProblema2, arqEnviarProblema2);
            ftp.storeFile(nomeArqCausa, arqEnviarCausa);
            ftp.storeFile(nomeArqCausa2, arqEnviarCausa2);

            ftp.logout();
            ftp.disconnect();

        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {

            e.printStackTrace();
        }

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM