I try to upload a folder to Dropbox in my android code by this code:
try {
// Upload to Dropbox
InputStream inputStream = new FileInputStream(file);
dbxClient.files().uploadBuilder("/" + file.getName()) //Path in the user's Dropbox to save the file.
.withMode(WriteMode.OVERWRITE) //always overwrite existing file
.uploadAndFinish(inputStream);
Log.d("Upload Status", "Success");
} catch (DbxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
It works correct when 'file' is a directory. But when it is a folder, I will get this exception for the first line:
open failed: EISDIR(is a directory)
Can I fix this problem? Or it must be a directory?
Related
I am using Apache POI on android to export an excel file for my offline database. While creating a file instead of creating a .xls file its generates directories
//filename = "collectionreport.xls"
String path = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)+File.separator+fileName;
File file = new File(path);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file);
workbook.write(fileOutputStream);
Log.e(TAG, "Writing file" + file);
isSuccess = true;
} catch (IOException e) {
Log.e(TAG, "Error writing Exception: ", e);
isSuccess = false;
} catch (Exception e) {
Log.e(TAG, "Failed to save file due to Exception: ", e);
isSuccess = false;
}
error:
java.io.FileNotFoundException: /storage/emulated/0/Android/data/aa.bb.com.modules.tabbank/files/Download/CollectionReport163548565.xls (Is a directory)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:287)
at java.io.FileOutputStream.<init>(FileOutputStream.java:223)
at java.io.FileOutputStream.<init>(FileOutputStream.java:110)```
EDIT: It automatically worked after restarting my computer running on linux and re installing the application
I am getting the exception java.io.FileNotFoundException: D:\Selenium Reports\Daily Reports\Merged file.xls when I am trying to access excel file through java program.
I have tried with:
absolute path
relative path
read/write access to the file
checked whether the file is open
Still, it is not working, is it not working because I have guest access to the machine?
keep the .java file and the .xls file in the same folder and use the code:
try {
File f = new File("Merged file.xls");
if (!f.exists()) {
System.out.println("File does not exist");
if (!f.createNewFile())
System.out.println("File cannot be created");
else
System.out.println("File created");
} else {
System.out.println("File exists");
if(!f.canRead())
System.out.println("Error in reading. Need permission");
if(!f.canWrite())
System.out.println("Error in writing. Need permission");
}
} catch (IOException e) {
e.printStackTrace();
}
}
Trying to create and write to a file, but i get a FileNotFoundException every time, here is the method i am using:
public void saveFileAsPRN(Context context){
byte[] dataFile = getPrintableFileData();
String filename = "TestPrn.prn";
// instantiate a file object using the path
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), filename);
Log.e(TAG, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString());
//determine if the media is mounted with read & write access
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
Log.e(TAG, "media mounted"); //good
}else{
Log.e(TAG, "media NOT mounted"); //bad
}
//create directory if it does not exist
//the default Download directory should always exist
if (!file.mkdirs()) {
Log.e(TAG, "Directory not created");
}
// determine if the file exists, create it if it does not
if(!file.exists()){
try {
Log.e(TAG, "File does not exist, creating..");
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
Log.e(TAG, "File Exists");
}
//this makes the blank file visible in the file browser
MediaScannerConnection.scanFile(context, new String[]{Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/" + filename}, null, null);
//create output stream - send data; saving to file
OutputStream out = null;
try {
FileOutputStream fos = null;
fos = new FileOutputStream(file); // <---- CRASHES HERE; FileNotFoundException
out = new BufferedOutputStream(fos);
out.write(dataFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
A FileNotFoundException is raised on the following line:
fos = new FileOutputStream(file); // <---- CRASHES HERE;
The directory Exists, and a blank file is created in the target directory (visible by browsing target folder on PC).
Calling the method canWrite() on the File object returns true - i have write access.
The manifest contains: android.permission.WRITE_EXTERNAL_STORAGE"
So i'm out of ideas, i see several people have similar issues, but i cant find an answer.
Commenting out the following code fixed the issue:
//create directory if it does not exist
//the default Download directory should always exist
if (!file.mkdirs()) {
Log.e(TAG, "Directory not created");
}
that code does create a blank file, you can see it contained in the folder,
BUT - it's very misleading; you can't do anything with this file, i tried transferring it from my device to my PC and i couldn't, i also cannot open it. and you cannot open a stream to it in code.
Try this may helps you.
Replace this line
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), filename);
With
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(), filename);
So I'm uploading a file to my VPS (Linux Centos 5 64 bit) via FTP using Java. The code I'm using to upload to my VPS is
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect(serverip);
client.login("user, pass);
client.setFileType(FTPClient.BINARY_FILE_TYPE);
// Create an InputStream of the file to be uploaded
String filename = Shared.saveLocation + Shared.saveAs;
fis = new FileInputStream(filename);
// Store file to server
client.storeFile(Shared.saveAs, fis);
client.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
Now the code is working but what I want is to change where on the VPS it uploads the file to. Right now it's
serverip/HERE
I have some files so want to change it to
serverip/file/HERE
How can I go about doing so?
You can use the changeCurrentWorkingDirectory() method to change to the desired directory. Once you're in there, you can write the file using storeFile just like before.
changeCurrentWorkingDirectory returns true if the directory change was successful, otherwise it returns false. It takes a string which interpreted as the directory path. If the path starts with a slash, it's interpreted as absolute path starting at the ftproot directory. Otherwise it's interpreted as relative path.
Revised code could look something like this:
FTPClient client = new FTPClient();
FileInputStream fis = null;
try
{
client.connect(serverip);
client.login("user, pass);
client.setFileType(FTPClient.BINARY_FILE_TYPE);
// change directory to serverip/file/
if (client.changeWorkingDirectory("/file"))
{
// Create an InputStream of the file to be uploaded
String filename = Shared.saveLocation + Shared.saveAs;
fis = new FileInputStream(filename);
// Store file to server
client.storeFile(Shared.saveAs, fis);
}
client.logout();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (fis != null)
{
fis.close();
}
client.disconnect();
}
catch (IOException e)
{
e.printStackTrace();
}
}
hey i want to upload a file to a directory called 'screenshots' on my webserver via FTP using java. I have been using this code and it says that it stores the file successfully and connected successfully but when i check my screenshots directory via the cpanel i dont see the file that was uploaded any help?
public static void uploadFilee() {
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect("****************");
client.login("********", "********");
System.out.println("Connected Successfully");
String filename = "C:/Users/Christian/Desktop/screenshots/img_" + queueInfo.get("SessionID");
fis = new FileInputStream(filename);
client.storeFile(filename, fis);
System.out.println("Stored File Successfully");
client.logout();
} catch (IOException e) {
System.out.println("Error_1");
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
client.disconnect();
} catch (IOException e) {
System.out.println("Error_2");
e.printStackTrace();
}
}
}
`
You may want to review this page
http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html#_storeFile(java.lang.String,java.lang.String,java.io.InputStream)
you have your statements such as storefile in a try statement, but if they fail, they return false, not an exception.
changing your code to inspect the return values of each function should help you find where your problem lies.