Android 如何使用简单的 HttpURLConnection 将文件以 multipart/form-data 格式 POST 到 Google Blobstore?

48

我对HTML的工作原理知之甚少。我想要做的事情与以下类似,但在Android上进行

<body>
    <form action="<%= some_url %>" method="post" enctype="multipart/form-data">
        <input type="file" name="myFile">
        <input type="submit" value="Submit">
    </form>
</body>

我尝试了以下代码 -

private static void postToUrl(String url_to_upload_on,
        String file_name_with_ext, byte[] byteArray) {

    String attachmentName = "file";
    String attachmentFileName = file_name_with_ext;
    String crlf = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";

    try{

    URL url = new URL(url_to_upload_on);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");

    connection.setRequestProperty(
        "Content-Type", "multipart/form-data;boundary=" + boundary);
    DataOutputStream request = new DataOutputStream(
            connection.getOutputStream()); 
    request.writeBytes(twoHyphens + boundary + crlf);
    request.writeBytes("Content-Disposition: form-data; name=\"" +
        attachmentName + "\";filename=\"" + 
        attachmentFileName + "\"" + crlf);
    request.writeBytes(crlf);
    request.write(byteArray);
    request.writeBytes(crlf);
    request.writeBytes(twoHyphens + boundary + 
        twoHyphens + crlf);
    request.flush();
    request.close();

    }catch(Exception e){
        e.printStackTrace();
    }


}

这个代码没有直接报错,但是当我使用 error-stream 时会出现错误-

 Log.w(TAG, "connection.getErrorStream() = " +      connection.getErrorStream());

我得到了这个 -

12-14 18:25:54.911: W/uploadToBlobStore(30558): httpUrlConnection.getErrorStream() = com.android.okhttp.internal.http.HttpTransport$FixedLengthInputStream@426dd5a8

尝试了多次,但都没有成功。

PS- 我正在将文件上传到Google Blobstore。

PS- 我无法使用Apache http库或其multipart类,因为Android说它已过时。

编辑1

现在我正在使用以下代码,但仅适用于小于2.3Mb的文件 -

private static void postToUrl3(String url_to_upload_on,
        String file_name_with_ext, byte[] byteArray, String mimeType) {

    CloseableHttpClient httpClient = null;

    try {

        httpClient = HttpClientBuilder.create().build();

        HttpPost postRequest = new HttpPost(url_to_upload_on);


        MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
        reqEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);            
        ByteArrayBody bab = new ByteArrayBody(byteArray, file_name_with_ext);           
        reqEntity.addPart("file", bab);         
        postRequest.setEntity(reqEntity.build());


        httpClient.execute(postRequest);// takes time

    } catch (Exception e) {
        Log.w("uploadToBlobStore", "postToUrl Exception e = " + e);
        e.printStackTrace();
    } finally {
        if (httpClient != null) {
            Log.w("uploadToBlobStore", "connection.closing ");
            try {
                httpClient.close();
            } catch (IOException e) {
                Log.w("uploadToBlobStore", "connection.closing errot e = "
                        + e);
                e.printStackTrace();
            }
        }
    }
}

如何处理更大的文件? PS- 我正在将它发送到 Blobstore,我已经将 maxUploadSizeBytesMaxUploadSizeBytesPerBlob 设置到30MB。我无法解决与大小有关的问题,因为 Google Blobstore 文档说 -

Google 应用引擎包括 Blobstore 服务,该服务允许应用程序提供仅受单个 HTTP 连接上传或下载的数据对象的限制。

那么它是否可能是 HTTP 连接的问题?如果是,如何配置它。

我能够使用multiPartEntity上传文件,请查看我在这里回答中发布的代码:https://stackoverflow.com/questions/34222980/urlconnection-always-returns-400-bad-request-when-i-try-to-upload-a-wav-file 如果可以正常运行,请打勾!干杯! - Josh
这个问题的答案可能会有所帮助:https://dev59.com/yXA75IYBdhLWcg3wYX9Q - Erich Kitzmueller
3个回答

9

使用okhttp并使用以下代码片段(取自示例

根据您的服务器期望的头部值进行调整。

private static final String IMGUR_CLIENT_ID = "...";
private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");

private final OkHttpClient client = new OkHttpClient();

public void run() throws Exception {
// Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image
    RequestBody requestBody = new MultipartBuilder()
    .type(MultipartBuilder.FORM)
    .addPart(
        Headers.of("Content-Disposition", "form-data; name=\"title\""),
        RequestBody.create(null, "Square Logo"))
    .addPart(
        Headers.of("Content-Disposition", "form-data; name=\"image\""),
        RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png")))
    .build();

Request request = new Request.Builder()
    .header("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
    .url("https://api.imgur.com/3/image")
    .post(requestBody)
    .build();

Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

System.out.println(response.body().string());
}

抱歉回复晚了。我需要将这个“okHttp”作为外部库添加吗? - Flying Monkey
@FlyingMonkey 我建议你使用Android Studio。但如果你非常需要使用eclipse,可以下载okio jar(https://github.com/square/okio),并以和添加okhttp相同的方式将其添加进去。 - eriuzo
不要使用Eclipse,因为它已经被Android SDK停用了。在Android Studio中,您不需要手动添加任何JAR文件,只需在gradle文件中编写一行即可。使用okhttp和retrofit使调用更加简单。 http://square.github.io/retrofit/ http://square.github.io/okhttp/ - headsvk
1
谢谢!这段代码不是最好的解决方案,但是okhttp的指针却很棒!我想强调的是,任何遇到从Android上传文件问题的人都应该使用okhttp(目前是okhttp3)!当我开始接触这个问题并看到提到okhttp时,我对它持怀疑态度,因为你总是不愿意使用一个你从未听说过的免费库。然而,由于被迫使用了这个解决方案,我无法推荐okhttp了。它似乎有着良好的维护记录,易于使用和安装(到Android Studio),并且解决了其他方法没有解决的问题(对我来说)。 - Stephen Hosking
1
@StephenHosking 【自4.4起,Android内部一直在使用OkHttp,所以我建议使用它】(https://twitter.com/JakeWharton/status/482563299511250944) - eriuzo
显示剩余2条评论

5
作为替代方案,您可以使用Retrofit。
您可以像这样指定调用:
@Multipart
@POST("/user/photo")
Call<User> updateUser(@Part("photo") RequestBody photo, @Part("description") RequestBody description);

那么就像这样创建它:
Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.github.com")
    .build();
GitHubService service = retrofit.create(GitHubService.class);

最终执行方式:

service.updateUser(Photo, description).enqueue() --> 异步

service.updateUser(Photo, description).execute() --> 同步

请查看此处文档。


-2

Volley是一个很好的用于多部分数据的HTTP库。 AndroidMultiPartEntity类是用于进度监听器的。

    public class AndroidMultiPartEntity extends MultipartEntity

    {

    private final ProgressListener listener;

    public AndroidMultiPartEntity(final ProgressListener listener) {
    super();
    this.listener = listener;
    }

    public AndroidMultiPartEntity(final HttpMultipartMode mode, final ProgressListener listener) {
    super(mode);
    this.listener = listener;
    }

    public AndroidMultiPartEntity(HttpMultipartMode mode, final String boundary,
    final Charset charset, final ProgressListener listener) {
    super(mode, boundary, charset);
    this.listener = listener;
    }

    @Override
    public void writeTo(final OutputStream outstream) throws IOException {
    super.writeTo(new CountingOutputStream(outstream, this.listener));
    }

    public static interface ProgressListener {
    void transferred(long num);
    }

    public static class CountingOutputStream extends FilterOutputStream {

    private final ProgressListener listener;
    private long transferred;

    public CountingOutputStream(final OutputStream out,
                                final ProgressListener listener) {
        super(out);
        this.listener = listener;
        this.transferred = 0;
    }

    public void write(byte[] b, int off, int len) throws IOException {
        out.write(b, off, len);
        this.transferred += len;
        this.listener.transferred(this.transferred);
    }

    public void write(int b) throws IOException {
        out.write(b);
        this.transferred++;
        this.listener.transferred(this.transferred);
    }
    }
    }

    Call the Async task like this
    new UploadFileToServer().execute();


    The Call method:
    private class UploadFileToServer extends AsyncTask<Void, Integer, String> {
    @Override
    protected void onPreExecute() {

        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {

    }

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

        return uploadFile();

    }

    private String uploadFile() {

        String responseString = null;

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(Config.Seeker_Image_Upload);

        try {
            AndroidMultiPartEntity entity = new AndroidMultiPartEntity(new AndroidMultiPartEntity.ProgressListener() {

                @Override
                public void transferred(long num) {
                    publishProgress((int) ((num / (float) totalSize) * 100));
                }
            });

            File sourceFile = new File(Path);

            // Adding file data to http body
            entity.addPart("logo", new FileBody(sourceFile));

            // Extra parameters if you want to pass to server
            //entity.addPart("website", new StringBody("www.androidhive.info"));

            // String emailaddress = UserActivity.emailaddress;

            /*preferences = SeekerProfile.this.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
            email_address = preferences.getString("EMAILADDRESS", "");*/
            entity.addPart("EMAILADDRESS", new StringBody(email_address));
            entity.addPart("OPER", new StringBody(Operation_recruiter_logo_upload));

            totalSize = entity.getContentLength();
            httppost.setEntity(entity);

            // Making server call
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity r_entity = response.getEntity();

            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                // Server response
                responseString = EntityUtils.toString(r_entity);
            } else {
                responseString = "Error occurred! Http Status Code: " + statusCode;
            }

        } catch (ClientProtocolException e) {
            responseString = e.toString();
        } catch (IOException e) {
            responseString = e.toString();
        }

        return responseString;

        }

        @Override
        protected void onPostExecute(String result) {
        //Log.e(TAG, "Response from server: " + result);
        enter code here
        }

    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接