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

Pratik Chauhan

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.

blackapps

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.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Files get corrupted when downloaded from FTP server

분류에서Dev

Android uploading ANY file to Server

분류에서Dev

Uploading two times a file to FTP

분류에서Dev

how to upload a pdf file to ftp server using file upload controller?

분류에서Dev

uploading file to server saves file with invalid characters

분류에서Dev

Multi Thread java downloader downloading corrupted file?

분류에서Dev

Uploading a file via website to SFTP server

분류에서Dev

Getting the file size from StreamWriter

분류에서Dev

Uploading image to FTP using PHP

분류에서Dev

Is this a bug in gedit or a corrupted file?

분류에서Dev

Scrapy - getting the the file size and type from a URL without downloading the file?

분류에서Dev

Java file is not getting compiled in cron

분류에서Dev

Java program error in uploading file throught PHP on Windows 7

분류에서Dev

android studio screen size not same on emulator

분류에서Dev

JPEG corrupted after sending to server

분류에서Dev

Displaying a database in PDF using iText in Android Studio

분류에서Dev

PDF 파일 편집 Android Studio

분류에서Dev

Getting updates on Android Studio on Linux Ubuntu 14.04

분류에서Dev

Archive files in ZIP on FTP server before ftp_get() the ZIP file using PHP

분류에서Dev

Drupal uploading files to server

분류에서Dev

uploading converted image to server

분류에서Dev

Fancybox is getting the content size wrong when using jQuery-Chosen?

분류에서Dev

Keep getting errors when trying to knit a PDF using R Markdown

분류에서Dev

File uploading in struts

분류에서Dev

file uploading in php

분류에서Dev

HTML form file not uploading

분류에서Dev

CodeIgniter uploading files no file

분류에서Dev

Some upload file size issue on Android

분류에서Dev

Cannot open pdf file created by Android app

Related 관련 기사

뜨겁다태그

보관