尝试将文件上载到Google云端存储时获取FileNotFoundException

时间:2016-05-22 07:47:21

标签: java android http google-cloud-storage

我尝试将图片从Android上传到云端存储。我关注如何使用JSON API将文件上传到Google云端存储this official guide。这是我的代码

private class uploadImage extends AsyncTask<File, Void, String> {

    File file = mPhotoFile;

    private String delimiter = "--";

    @Override
    protected void onPreExecute() {
        Toast.makeText(getActivity(), mPhotoFile.getPath(), Toast.LENGTH_SHORT).show();
    }

    @Override
    protected String doInBackground(File... params) {


        try {
            URL url = new URL("https://www.googleapis.com/upload/storage/v1/b/backend-images/o?uploadType=media&name=myObject?key=my_key");
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            urlConnection.setRequestMethod("POST");
            urlConnection.setChunkedStreamingMode(0);
            urlConnection.setRequestProperty("Content-Type", "image/jpeg");
            urlConnection.setRequestProperty("Content-Length", String.valueOf(mPhotoFile.getPath().getBytes().length));
            urlConnection.setRequestProperty("Authorization", "my_key");
            urlConnection.setDoOutput(true);
            urlConnection.setDoOutput(true);


            OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
            InputStream responseStream = new BufferedInputStream(urlConnection.getInputStream());
            BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));

            out.write(("Content-Type: image/jpeg\r\n").getBytes());
            out.write(("Content-Length: " + String.valueOf(mPhotoFile.getPath().getBytes().length)).getBytes());
            out.write("\r\n".getBytes());
            out.write(mPhotoFile.getPath().getBytes());
            out.write("\r\n".getBytes());

            String line = "";
            StringBuilder stringBuilder = new StringBuilder();

            while((line = responseStreamReader.readLine()) != null) {
                stringBuilder.append(line).append("\n");
            }

            String response = stringBuilder.toString();

            Log.i("CloudStorage", response);
        } catch (IOException e) {
            e.printStackTrace();
            return e.getMessage();
        }

        return "Everything was a success";
    }


}

我使用公共API访问方法并将Api密钥附加到the guide says I could之类的链接以授权请求

以下是错误

05-22 10:24:01.798 3747-4045/com.example.kid.uimockup W/System.err:    java.io.FileNotFoundException:https://www.googleapis.com/upload/storage/v1/b/backend-images/o?uploadType=media&name=myObject?key=my_key
05-22 10:24:01.798 3747-4045/com.example.kid.uimockup W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:197)
05-22 10:24:01.798 3747-4045/com.example.kid.uimockup W/System.err:     at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
05-22 10:24:01.798 3747-4045/com.example.kid.uimockup W/System.err:     at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
05-22 10:24:01.798 3747-4045/com.example.kid.uimockup W/System.err:     at com.example.kid.uimockup.HomeFragment$uploadImage.doInBackground(HomeFragment.java:636)
05-22 10:24:01.803 3747-4045/com.example.kid.uimockup W/System.err:     at com.example.kid.uimockup.HomeFragment$uploadImage.doInBackground(HomeFragment.java:605)
05-22 10:24:01.803 3747-4045/com.example.kid.uimockup W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-22 10:24:01.803 3747-4045/com.example.kid.uimockup W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-22 10:24:01.803 3747-4045/com.example.kid.uimockup W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-22 10:24:01.803 3747-4045/com.example.kid.uimockup W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-22 10:24:01.803 3747-4045/com.example.kid.uimockup W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-22 10:24:01.803 3747-4045/com.example.kid.uimockup W/System.err:     at java.lang.Thread.run(Thread.java:818)

如果这是客户端或服务器端的问题我不知道

1 个答案:

答案 0 :(得分:1)

我做了一些改动后才开始工作。我收到了401 Unauthorized Error代码,这意味着我没有授权访问该存储桶。

因此,我没有附加查询参数key=api_key,而是附加了access_token=auth_token来授权请求​​。

然后我将allUsers权限添加到我的存储桶中(公开让所有人都可以写和读)并且它有效。