将 PDF 文件上传到 FTP 服务器时损坏且大小增加 | Android 工作室 | java

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

当我将 pdf 文件上传到 windows 服务器时,它在从 FileZilla 下载时损坏了。文件大小也以字节为单位增加。 部分文件损坏或部分文件只有一半内容。

任何帮助或代码将不胜感激。 谢谢!

点击从phone的目录中选择文件:

   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);
        }
    });

在 Activity 结果:

 @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);
}

正在服务器上创建目录并上传文件:

  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;
    }
}

请检查代码是否有改动或我写错的地方,请评论。

InputStream inputStream = new FileInputStream(file);

将其替换为

InputStream inputStream = getContentResolver().openInputStream(data.getData());

您可以扔掉所有乱用 File 和 Cursor 实例的代码。

我也想知道我引用的那行之前的那四行应该做什么。

问题是我设置的传输模式不正确。 正确的方法是:

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