简体   繁体   中英

PDF file getting corrupted and size increases when uploading it to FTP server | Android Studio | java

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.

The problem was that I was setting the transfer mode incorrect way. Right way is:

      ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
      ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM