When I am uploading a pdf file to windows server it's getting corupted when downloading from FileZilla. The File size is also increasing in bytes. Some files gets corrupted or some files only have half content.
Any help or code would be appreciated. Thanks!
On Click to choose file from phone's directory:
btnSelectFile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("application/pdf");
startActivityForResult(Intent.createChooser(intent, "Select PDF File"), 21);
}
});
On Activity Result:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 21:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
uri = data.getData();
uriString = uri.toString();
myFile = new File(uriString);
path = myFile.getAbsolutePath();
displayName = null;
file1 = Uri.fromFile(new File(path));
if (uriString.startsWith("content://")) {
Cursor cursor = null;
try {
cursor = getActivity().getContentResolver().query(uri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
textFileSelectedOrNot.setText(displayName);
fileName = textFileSelectedOrNot.getText().toString();
}
} finally {
cursor.close();
}
} else if (uriString.startsWith("file://")) {
displayName = myFile.getName();
textFileSelectedOrNot.setText(displayName);
fileName = textFileSelectedOrNot.getText().toString();
}
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
Creating directory on server and uploading file:
class CreateDir extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
String server = "XX.XXX.X.XX";
String username = "XXXXXXX";
String password = "XXXXX";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, 2102);
ftpClient.login(username, password);
ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
Log.d(TAG, "CONNECTED");
String dirPath = "/ws.gajeratrust.org/TestingApp/" + fileId + "/";
boolean created = ftpClient.makeDirectory(dirPath);
FTPUtils.mkdir(ftpClient, dirPath);
InputStream inputStream =getContentResolver().openInputStream(uri);
ftpClient.storeFile(dirPath+"/"+fileName,inputStream);
inputStream.close();
ftpClient.logout();
ftpClient.disconnect();
Log.d(TAG, "DISCONNECTED");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Please check the code any alterations or something I have coded wrong, please comment it.
InputStream inputStream = new FileInputStream(file);
Replace that by
InputStream inputStream = getContentResolver().openInputStream(data.getData());
You can throw away all code messing around with File and Cursor instances.
Also i wonder what those four lines before the line i quoted should do.