0
I have the following screen:
i even asked another question, which was having trouble loading the directory files in the spinner and has already been solved, the problem is now by clicking the Import button, to import the csv file inside the Import folder that is located in my FTP, need to grab this csv file and transfer it into android, but error occurs java.io.FileNotFoundException: /storage/emulated/0/mesa.csv: open failed: EISDIR (Is a directory)
i’m pretty sure it’s typo because, I picked up a tutorial to try to do this part of importing and exporting a CSV file, but in the case of import code I got very confused, follow the code:
Import class TFP:
package realsysten.com.br.sigarestaurante;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.app.ProgressDialog;
import java.io.File;
import java.util.ArrayList;
import org.apache.commons.net.ftp.FTPFile;
import android.os.Environment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
/**
* Created by Vitor on 14/06/2016.
*/
public class importaFtpActivity extends AppCompatActivity {
Spinner spImport;
ArrayList<String> arquivosFTP = new ArrayList<String>();
ProgressDialog dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.importa_ftp);
spImport = (Spinner) findViewById(R.id.spImport);
ImportItens();
Button b2 = (Button) findViewById(R.id.btnImpInfos);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(importaFtpActivity.this, "FTP",
"Sincrozinzando dados...", false, true);
dialog.setCancelable(false);
ChamaImport();
}
});
Button biv = (Button) findViewById(R.id.btnVoltar);
biv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(importaFtpActivity.this, OpcoesActivity.class);
startActivity(i);
finish();
}
});
}
public void ImportItens() {
new Thread(new Runnable() {
@Override
public void run() {
listarArquivosFTP();
}
}).start();
}
public void ChamaImport() {
new Thread(new Runnable() {
@Override
public void run() {
efetuarDownload();
dialog.dismiss();
}
}).start();
}
public void listarArquivosFTP() {
FTPController ftp = new FTPController();
ftp.conectar("192.168.2.5", "vitor", "248693751qQ", 21);
ftp.mudarDiretorio("/import");
FTPFile[] arquivos = ftp.dir("/import");
if (arquivos != null) {
int lenght = arquivos.length;
for (int i = 0; i < lenght; i++) {
FTPFile f = arquivos[i];
if (f.isFile()) {
arquivosFTP.add(f.getName());
}
}
final ArrayAdapter<String> arraAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, arquivosFTP);
runOnUiThread(new Runnable() {
@Override
public void run() {
spImport.setAdapter(arraAdapter);
}
});
}
}
public void efetuarDownload() {
String lstrArq = "";
try {
FTPController ftp = new FTPController();
lstrArq = "/" + spImport.getSelectedItem().toString();
File lArquivos = new File(Environment.getExternalStorageDirectory(), lstrArq);
ftp.conectar("192.168.2.5", "vitor", "248693751qQ", 21);
ftp.download("/Import", spImport.getSelectedItem().toString(), lArquivos.toString());
} catch (Exception e) {
e.getStackTrace();
}
}
}
Class Ftpcontroller:
package realsysten.com.br.sigarestaurante;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import android.os.Environment;
import android.util.Log;
/**
* Created by Vitor on 14/06/2016.
*/
public class FTPController{
FTPClient mFTP;
private String TAG = "classeFTP";
public FTPFile[] dir(String diretorio) {
try {
FTPFile[] ftpFiles = mFTP.listFiles(diretorio);
return ftpFiles;
} catch (Exception e) {
Log.e(TAG, "Erro: não foi possivel listar os arquivos e pastas do diretorio " +
diretorio + " . " + e.getMessage());
}
return null;
}
public boolean mudarDiretorio(String diretorio) {
try {
mFTP.changeWorkingDirectory(diretorio);
} catch (Exception e) {
Log.e(TAG, "Erro: não foi possivel mudar o diretorio para " + diretorio);
}
return false;
}
public boolean desconecta() {
try {
mFTP.disconnect();
mFTP = null;
return true;
} catch (Exception e) {
Log.e(TAG, "Erro: ao desconectar. " + e.getMessage());
}
return false;
}
public boolean conectar(String host, String usuario, String senha, int porta){
try {
mFTP = new FTPClient();
mFTP.connect(host, porta);
if (FTPReply.isPositiveCompletion(mFTP.getReplyCode())) {
boolean status = mFTP.login(usuario, senha);
mFTP.setFileType(FTP.BINARY_FILE_TYPE);
mFTP.enterLocalPassiveMode();
return status;
}
} catch (Exception e) {
Log.e(TAG, "ERRO: não foi possivel conectar " + host);
}
return false;
}
public boolean download(String diretorioOrigem, String arqOrigem, String arqDestino) {
boolean status = false;
try {
File caminho = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
"/Import");
if (!caminho.exists()) {
caminho.mkdir();
}
mudarDiretorio(diretorioOrigem);
FileOutputStream desFileStream = new FileOutputStream(arqDestino);
mFTP.setFileType(FTP.BINARY_FILE_TYPE);
mFTP.enterLocalActiveMode();
status = mFTP.retrieveFile(arqOrigem, desFileStream);
desFileStream.close();
desconecta();
return status;
} catch (Exception e) {
Log.e(TAG, "Erro: Falha ao efetuar download. " + e.getMessage());
}
return status;
}
public boolean upload(String diretorio, String nomeArquivo) {
boolean status = false;
try {
FileInputStream arqEnviar = new FileInputStream(Environment.getExternalStorageDirectory() + diretorio);
mFTP.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
mFTP.setFileType(FTPClient.STREAM_TRANSFER_MODE);
mFTP.storeFile(nomeArquivo, arqEnviar);
desconecta();
return status;
} catch (Exception e) {
Log.e(TAG, "Erro: falha ao efetuar upload. " + e.getMessage());
}
return status;
}
}
for import I use:
Button b2 = (Button) findViewById(R.id.btnImpInfos);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(importaFtpActivity.this, "FTP",
"Sincrozinzando dados...", false, true);
dialog.setCancelable(false);
ChamaImport();
}
});
Chamaimport:
public void ChamaImport() {
new Thread(new Runnable() {
@Override
public void run() {
efetuarDownload();
dialog.dismiss();
}
}).start();
}
effectDownload:
public void efetuarDownload() {
String lstrArq = "";
try {
FTPController ftp = new FTPController();
lstrArq = "/" + spImport.getSelectedItem().toString();
File lArquivos = new File(Environment.getExternalStorageDirectory(), lstrArq);
ftp.conectar("192.168.2.5", "vitor", "248693751qQ", 21);
ftp.download("/Import", spImport.getSelectedItem().toString(), lArquivos.toString());
} catch (Exception e) {
e.getStackTrace();
}
}
download:
public boolean download(String diretorioOrigem, String arqOrigem, String arqDestino) {
boolean status = false;
try {
File caminho = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
"/Import");
if (!caminho.exists()) {
caminho.mkdir();
}
mudarDiretorio(diretorioOrigem);
FileOutputStream desFileStream = new FileOutputStream(arqDestino);
mFTP.setFileType(FTP.BINARY_FILE_TYPE);
mFTP.enterLocalActiveMode();
status = mFTP.retrieveFile(arqOrigem, desFileStream);
desFileStream.close();
desconecta();
return status;
} catch (Exception e) {
Log.e(TAG, "Erro: Falha ao efetuar download. " + e.getMessage());
}
return status;
}
I believe the typo is here:
ftp.download("/Import", spImport.getSelectedItem().toString(), lArquivos.toString());
but when debugging the error occurs in the download method of the Ftpcontroller class on the line:
FileOutputStream desFileStream = new FileOutputStream(arqDestino);
If anyone can give me a hand I really appreciate.
What is the content of the variables arqOrigem and arqLong you are coming to the download method?
– Reginaldo Rigo
@Reginaldorigo diretorioOrigem: "/Import' arqOrigem: "mesa.csv" arqDestino: "/Storage/Emulated/0/mesa.csv"
– Taha tsu