FileOutputStream不在文件中写入内容

时间:2018-10-10 10:53:08

标签: java fileoutputstream

我正在尝试从数据库读取blob(PDF)文件,所以问题是在“下载”文件夹中创建的空白文件。告诉我如何在该文件中写入内容。请告诉我我的代码在做什么错了吗?

    protected String doInBackground(String... params) {
                String get_image_url = 
    context.getResources().getString(R.string.ip) + 
    "/USA/USAgetdeptdocument.php";
                try {
                URL url = new URL(get_image_url);                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String post_data = URLEncoder.encode("offset", "UTF-8") + "=" + URLEncoder.encode(offset, "UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                if (inputStream == null) {
                    return "failed";
                } else {

                    String filepath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/" + filename;

                    FileOutputStream out = null;
                    DataOutputStream dos;
                    out = new FileOutputStream(new File(filepath));
                    dos = new DataOutputStream(out);
                    int readcount;
                    byte[] buffer = new byte[inputStream.available()];
                    while ((readcount=inputStream.read(buffer,0,buffer.length))!=-1){
                        dos.write(buffer, 0, readcount);
                    }
                    inputStream.close();
                    return "success";
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

0 个答案:

没有答案