首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android中的HTTPComponents multipart/form-data

Android中的HTTPComponents multipart/form-data
EN

Stack Overflow用户
提问于 2012-10-07 04:24:32
回答 1查看 629关注 0票数 0

如何使用Apache HTTPComponents在查询后发送文件?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-07 04:24:32

一种可能的解决方案是自己生成HTTP头,就像这里解释的那样- http://en.wikipedia.org/wiki/MIME (参见“多部分消息”)

我写了这样的函数。也许它不是写得很好,但它工作得很好。

代码语言:javascript
复制
private static final String REQUEST_BOUNDARY = "somethingUniqueForYourQuery";

private static String sendRequest(String url, String method, String params, File file, String fileField) {
    HttpURLConnection httpConn = null;
    StringBuilder httpContent = new StringBuilder();
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    byte[] fileContent = new byte[0];
    int fileSize = 0;

    // trying to read file
    if (file != null) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);

            httpContent.append(twoHyphens + REQUEST_BOUNDARY + lineEnd);
            httpContent.append("Content-Disposition: form-data; name=\"" + fileField + "\"; filename=\"" + file.getName() + "\"" + lineEnd);
            httpContent.append(lineEnd);

            fileSize = fileInputStream.available();
            fileContent = new byte[fileSize];
            fileInputStream.read(fileContent, 0, fileSize);
            fileInputStream.close();
        }
        catch (Exception e){
            Log.d(DEBUG_TAG, "Exception occured: " + e.toString());
        }
    }

    // trying to perform request
    try {
        httpConn = (HttpURLConnection) new URL(url).openConnection();

        if (httpConn != null) {
            httpConn.setDoInput(true);
            httpConn.setDoOutput(true);
            httpConn.setUseCaches(false);
            httpConn.setConnectTimeout(CONNECTION_TIMEOUT_STRING);
            httpConn.setRequestMethod(method);

            if (file != null && httpContent.length() > 0) {
                httpConn.setRequestProperty("Connection", "Keep-Alive");
                httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + REQUEST_BOUNDARY);

                DataOutputStream dos = new DataOutputStream(httpConn.getOutputStream());
                if (params != null) {
                    dos.writeBytes(params);
                }
                dos.writeBytes(httpContent.toString());
                dos.write(fileContent, 0, fileSize);
                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + REQUEST_BOUNDARY + twoHyphens + lineEnd);
                dos.flush();
                dos.close();
            }
            else if (params != null) {
                PrintWriter out = new PrintWriter(httpConn.getOutputStream());
                out.print(params);
                out.close();
            }

            httpConn.connect();

            int response = httpConn.getResponseCode();
            BufferedReader rd;

            if (httpConn.getErrorStream() == null) {
                rd = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
            } else {
                rd = new BufferedReader(new InputStreamReader(httpConn.getErrorStream()));
            }

            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = rd.readLine()) != null) {
                sb.append(line + "\n");
            }
            if (rd != null) {
                rd.close();
            }
            return sb.toString();
        } else {
            Log.d(DEBUG_TAG, "Connection Error");
        }
    }
    catch (Exception e){
        Log.d(DEBUG_TAG, "Exception occured: " + e.toString());
    }
    finally {
        if (httpConn != null) {
            httpConn.disconnect();
        }
    }
    return null;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12763539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档