为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

千巷猫影 2023-09-13 17:58:06
我需要帮助在 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;}
查看完整描述

1 回答

?
万千封印

TA贡献1891条经验 获得超3个赞

尝试以下代码并让我知道:


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;

}


查看完整回答
反对 回复 2023-09-13
  • 1 回答
  • 0 关注
  • 74 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信