Upload file to FTP - android

I'm trying to upload a file to an FTP using apache commons ftp.
I am using a code that I have seen on several websites, including stackoverflow
Android FTP Library
The problem is that in the line:
Buffin = new BufferedInputStream (new FileInputStream (file));
I can not put any paths in "file", eclipse does not validate any values ​​or path-
What would have to indicate in "new FileInputStream"?
I do not know I'm doing wrong.
Thank you very much and best regards

You need a File object to pass it to the FileInputStream.
Buffin = new BufferedInputStream(new FileInputStream(new File("/path/to/file"));
And you can't Upload file to FTP because FTP is not a place, it is a protocol.

Create a File object with the path and pass it in.

You can do this:
Buffin = new BufferedInputStream (new FileInputStream (<path to your file>));
Details here:
FileInputStream

Related

libGDX android assets file not found

I'm testing my app (with android, core and desktop modules) on desktop, everything works fine.
Now that I try to run on Android, it gives me FileNotFound. I linked android asset to desktopLauncher before running on desktop, but Am I meant to do something similar for android?
File file = Gdx.files.internal("liver_initial_joints.json").file();
FileInputStream fis = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
fis.read(data);
fis.close();
str = new String(data, "UTF-8");
I have my file at .../android/assets/liver_initial_joints.json
Android does not provide direct java.io.File access to the internal app directory. If you look at the javadocs for libgdx's FileHandle.file(), you'll see that it says it's not usable for anything but Absolute and External types of files.
If you just need to read a string or byte array, you can use fileHandle.readString() or fileHandle.readBytes(). If you need an input stream, use fileHandle.read();.
For example:
byte[] data = Gdx.files.internal("liver_initial_joints.json").read();
str = new String(data, "UTF-8");
I find a really easy way to do this is with:
InputStream is = context.getAssets().open("liver_initial_joints.json");

Read Downloaded Text File - Android

So my program works like this:
It downloads a .txt file from the dropbox and saves it to downloads directory.
Uri uri = Uri.parse(file);
DownloadManager.Request r = new DownloadManager.Request(uri);
// This put the download in the same Download dir the browser uses
r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "upload");
r.allowScanningByMediaScanner();
// Start download
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(r);
And the code is working fine, it downloads the file called upload.txt.
So now i want to read from it. I have been looking for some codes and nothing seems to be working...
Here is my current code, it throws a FileNotFoundException but file is located there.
Any tips?
String text="";
FileInputStream Fin=new FileInputStream(Environment.DIRECTORY_DOWNLOADS+"upload");
byte[] b=new byte[100];
Fin.read(b);
text=new String(b);
Fin.close();
Ofc i have tried putting "upload.txt", and even "/upload" and "/upload.txt" but still nothing.
If anyone can give me code that reads a text file, it would be awesome, or if someone could give me a code that can get the source code form a html site ( without JSOUP and other parsers, i have tried that, i want to implement my own parser ).
Thanks!!
Change
FileInputStream Fin=new FileInputStream(Environment.DIRECTORY_DOWNLOADS+"upload");
to
FileInputStream Fin=new FileInputStream(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "upload"));
You should use Environment.getExternalStoragePublicDirectory to get the path.
Or if you like more,
FileInputStream Fin=new FileInputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/upload");
Change
r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "upload");
too with
r.setDestinationInExternalPublicDir(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "upload");
Took me ages, but finally:
You should add to the manifest the various permissions (android.permission.READ_EXTERNAL_STORAGE...)
But this is not enough!
you should explicitly ask for these permissions in the onCreate():
requestPermissions({android.permission.READ_EXTERNAL_STORAGE,...}, 200);
Only afterwards you can access these directories.

Unable to read file from the Downloads folder for an android app

I want to create an inputstream for a file that is stored in the Downloads folder of my phone.
Earlier I had the required file as an asset and I read it with this code:
InputStream fin;
fin = getAssets().open(FILENAME);
However now I want to be able to open a file stored in my downloads folder /mnt/sdcard/Download as an InputStream NOT as a FileInputStream Object. I am using a library which requires an InputStream only as an argument so I can not use the FileInputStream
methods because I get a fatal exception if I do and the App stops.
How do I go about this? I tried to find some helper function from the documentation, but could not.
Edit: I also tried editing the code to this-
String path = "/mnt/sdcard/Download"+FILENAME;
fin = new BufferedInputStream(new FileInputStream(path));
However still when I try to read the file, the App crashes.
I had the same issue as yours :
AssetManager am=getAssets();
InputStream is=am.open("file1.xls");
InputStream is = new FileInputStream(filepath);
Workbook wb = Workbook.getWorkbook(is);
Here I used to access file1.xls from the assets folder.
I fixed it by using :
File filepath = newFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/"+"file1.xls"); // fist part gets the directory of download folder and last part is your file name
InputStream is = new FileInputStream(filepath);
Workbook wb = Workbook.getWorkbook(is);
Don't forget to get the storage permission first to access the storage.

UTF-8 with apache commons-net

I have problem with Unicode, when I save file in arabic name on sdcard, all things will be good, but when file uploaded to ftp server by commons-net, I will get file name as pic
any solution please?
This is a part of code which related with FTPClient:
FTPClient ftpClient = new FTPClient();
try {
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
conf.setServerLanguageCode("ar");
ftpClient.configure(conf);
ftpClient.setControlEncoding("UTF-8");
ftpClient.setAutodetectUTF8(true);
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
Using filenames in non-latin letters will cause a lot of problems. Many programs will not load or display such files. I would question myself, if there is really a need in this naming of files.

Read local compressed XML file(gzip) in Android application

I don't know how to get an InputStream(Read gzipped local xml file) from the locally stored gzip xml file.
employee.gz
If someone Can help I really appreciate. Thanks
This link works with zip. http://techdroid.kbeanie.com/2010/10/unzip-files-in-android.html
I am not sure if it will work with gz files, but you could give it a try. There's a documentation on GZIPInputStream class on the dev docs.
http://developer.android.com/reference/java/util/zip/GZIPInputStream.html
This piece of code works.
GZIPInputStream inputStream = new GZIPInputStream(new FileInputStream(new File(
"path to file")));
String str = IOUtils.convertStreamToString(inputStream);
I have used a util class which converts the input stream to a string. You might want to do the reading part manually.

Categories

Resources