I have the following screen:
Ievenaskedanotherquestion,whichwashavingtroubleloadingthedirectoryfilesinthespinnerandhasalreadybeensolved,theproblemnowiswhenclickingtheImportbutton,toimportthecsvfileintotheImportfolderthatislocatedinthemyFTP,Ineedtogetthiscsvfileandtransferitintotheandroid,butthejava.io.FileNotFoundException:/storage/emulated/0/mesa.csv:openfailed:EISDIR(Isadirectory)
erroroccurs
I'mprettysureit'satypobecauseIgotatutorialtotrytodothispartofimportingandexportingaCSVfile,butinthecaseoftheimportcodeIwasveryconfused,p>
ImportclassFTP:
packagerealsysten.com.br.sigarestaurante;importandroid.content.Intent;importandroid.os.Bundle;importandroid.support.v7.app.AppCompatActivity;importandroid.app.ProgressDialog;importjava.io.File;importjava.util.ArrayList;importorg.apache.commons.net.ftp.FTPFile;importandroid.os.Environment;importandroid.view.View;importandroid.widget.ArrayAdapter;importandroid.widget.Button;importandroid.widget.Spinner;/***CreatedbyVitoron14/06/2016.*/publicclassimportaFtpActivityextendsAppCompatActivity{SpinnerspImport;ArrayList<String>arquivosFTP=newArrayList<String>();ProgressDialogdialog;@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.importa_ftp);spImport=(Spinner)findViewById(R.id.spImport);ImportItens();Buttonb2=(Button)findViewById(R.id.btnImpInfos);b2.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){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();
}
}
}
FTPController class:
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();
}
DownloadDownload:
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 think the typo is here:
ftp.download("/Import", spImport.getSelectedItem().toString(), lArquivos.toString());
But when debugging the error occurs in the FTPController class download method on the line:
FileOutputStream desFileStream = new FileOutputStream(arqDestino);
If anyone can give me a hand I appreciate it a lot.