gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:59:29 29 4
gpt4 key购买 nike

我需要帮助在 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;
}

关于java - 如何将 PDF 文件上传到 Parse 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58185321/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com