如何将 PDF 文件上传到 Parse 服务器?

How do I upload a PDF file to Parse server?

我需要帮助使用 android Studio 中的 JAVA 将 pdf 文件上传到解析服务器。

我尝试使用以下代码:

private ParseObject uploadPDFToParse(File PDFFile, ParseObject po, String columnName){

    if(PDFFile != null){
        Log.d("EB", "PDFFile is not NULL: " + PDFFile.toString());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(PDFFile));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        int read;
        byte[] buff = new byte[1024];
        try {
            while ((read = in.read(buff)) > 0)
            {
                out.write(buff, 0, read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] pdfBytes = out.toByteArray();

        // Create the ParseFile
        ParseFile file = new ParseFile(PDFFile.getName() , pdfBytes);
        po.put(columnName, file);

        // Upload the file into Parse Cloud
        file.saveInBackground();
        po.saveInBackground();
    }
    return po;
}

我收到这个错误:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.jackady.notebytebylev3l, PID: 31048 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://com.mi.android.globalFileexplorer.myprovider/external_files/Download/CourseRegistrationReport (1).pdf flg=0x1 }} to activity {com.jackady.notebytebylev3l/com.jackady.notebytebylev3l.uploadPDF}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.BufferedInputStream.read(byte[])' on a null object reference at android.app.ActivityThread.deliverResults(ActivityThread.java:4419) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4461) at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:201) at android.app.ActivityThread.main(ActivityThread.java:6806) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.BufferedInputStream.read(byte[])' on a null object reference at com.jackady.notebytebylev3l.uploadPDF.uploadPDFToParse(uploadPDF.java:43) at com.jackady.notebytebylev3l.uploadPDF.onActivityResult(uploadPDF.java:140) at android.app.Activity.dispatchActivityResult(Activity.java:7590) at android.app.ActivityThread.deliverResults(ActivityThread.java:4412) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4461)  at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:201)  at android.app.ActivityThread.main(ActivityThread.java:6806)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

尝试以下代码并告诉我:

private ParseObject uploadPDFToParse(File PDFFile, ParseObject po, String columnName){

    if(PDFFile != null){
        Log.d("EB", "PDFFile is not NULL: " + PDFFile.toString());
        int size = (int) file.length();
        byte[] pdfBytes = new byte[size];

        try {
            BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
            buf.read(pdfBytes, 0, pdfBytes.length);
            buf.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Create the ParseFile
        ParseFile file = new ParseFile(PDFFile.getName() , pdfBytes);
        po.put(columnName, file);

        // Upload the file into Parse Cloud
        file.save();
        po.saveInBackground();
    }
    return po;
}