使用带有 HttpURLConnection 的 POST 发送文件

Posted

技术标签:

【中文标题】使用带有 HttpURLConnection 的 POST 发送文件【英文标题】:Sending files using POST with HttpURLConnection 【发布时间】:2012-07-30 19:11:26 【问题描述】:

由于android开发人员recommend使用HttpURLConnection类,我想知道是否有人可以为我提供一个很好的例子,说明如何通过POST发送位图“文件”(实际上是内存中的流)到Apache HTTP 服务器。我对 cookie 或身份验证或任何复杂的东西不感兴趣,但我只想拥有一个可靠且符合逻辑的实现。我在这里看到的所有示例看起来都更像是“让我们试试这个,也许它会起作用”。

现在,我有这个代码:

URL url;
HttpURLConnection urlConnection = null;
try 
    url = new URL("http://example.com/server.cgi");

    urlConnection = (HttpURLConnection) url.openConnection();

 catch (Exception e) 
    this.showDialog(getApplicationContext(), e.getMessage());

finally 
    if (urlConnection != null)
    
        urlConnection.disconnect();
    

showDialog 应该只显示AlertDialog(如果 URL 无效?)。

现在,假设我生成了一个像这样的位图:Bitmap image = this.getBitmap() 在从View 派生的控件内,我想通过 POST 发送它。实现这样的事情的正确程序是什么?我需要使用哪些类?我可以像this example 一样使用HttpPost 吗?如果是这样,我将如何为我的位图构造InputStreamEntity?我会发现要求首先将位图存储在设备上的文件中是令人反感的。


我还应该提到,我确实需要将原始位图的每个未更改的像素发送到服务器,所以我无法将其转换为 JPEG。

【问题讨论】:

参考this SO discussion 【参考方案1】:

我不知道为什么HttpURLConnection 类不提供任何发送文件的方法,而无需手动编写文件包装器。这是我最终做的,但如果有人知道更好的解决方案,请告诉我。

输入数据:

Bitmap bitmap = myView.getBitmap();

静态的东西:

String attachmentName = "bitmap";
String attachmentFileName = "bitmap.bmp";
String crlf = "\r\n";
String twoHyphens = "--";
String boundary =  "*****";

设置请求:

HttpURLConnection httpUrlConnection = null;
URL url = new URL("http://example.com/server.cgi");
httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setUseCaches(false);
httpUrlConnection.setDoOutput(true);

httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
httpUrlConnection.setRequestProperty(
    "Content-Type", "multipart/form-data;boundary=" + this.boundary);

启动内容包装器:

DataOutputStream request = new DataOutputStream(
    httpUrlConnection.getOutputStream());

request.writeBytes(this.twoHyphens + this.boundary + this.crlf);
request.writeBytes("Content-Disposition: form-data; name=\"" +
    this.attachmentName + "\";filename=\"" + 
    this.attachmentFileName + "\"" + this.crlf);
request.writeBytes(this.crlf);

Bitmap 转换为ByteBuffer

//I want to send only 8 bit black & white bitmaps
byte[] pixels = new byte[bitmap.getWidth() * bitmap.getHeight()];
for (int i = 0; i < bitmap.getWidth(); ++i) 
    for (int j = 0; j < bitmap.getHeight(); ++j) 
        //we're interested only in the MSB of the first byte, 
        //since the other 3 bytes are identical for B&W images
        pixels[i + j] = (byte) ((bitmap.getPixel(i, j) & 0x80) >> 7);
    


request.write(pixels);

结束内容包装:

request.writeBytes(this.crlf);
request.writeBytes(this.twoHyphens + this.boundary + 
    this.twoHyphens + this.crlf);

刷新输出缓冲区:

request.flush();
request.close();

得到响应:

InputStream responseStream = new 
    BufferedInputStream(httpUrlConnection.getInputStream());

BufferedReader responseStreamReader = 
    new BufferedReader(new InputStreamReader(responseStream));

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

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

responseStreamReader.close();

String response = stringBuilder.toString();

关闭响应流:

responseStream.close();

关闭连接:

httpUrlConnection.disconnect();

PS:当然我必须把请求包装在private class AsyncUploadBitmaps extends AsyncTask&lt;Bitmap, Void, String&gt;中,为了让Android平台开心,因为它不喜欢在主线程上有网络请求。

【讨论】:

这个问题终于有了一个完美解释的答案!谢谢!顺便说一句,我刚刚从 Android 开发者博客 (android-developers.blogspot.com/2011/09/…) 中找到了这篇文章,他们建议在 Apache HTTPClient 上使用 HTTPURLConnection。干杯! PS:根据this,` [error] => 3` 表示“上传的文件只是部分上传”,所以我可以假设存在一些缓冲问题,但我有不知道如何调试/修复这样的事情。 见我上面的评论。您需要将它们添加到url 变量中,如下所示:URL url = new URL("http://example.com/?param1=val1&amp;param2=val2");。您可以根据需要添加任意数量(尽管我认为有一些限制)。 太好了,只是错过了一件事:responseStreamReader 应该在 try/catch 的最后关闭。这样:try ALL YOUR CODE catch (IOException e) e.printStackTrace(); 最后 if (connection != null) connection.disconnect();尝试 if (responseStreamReader != null) responseStreamReader.close(); catch (IOException e) e.printStackTrace(); 完美答案!我建议看看this w3.org docs about forms【参考方案2】:

我实际上找到了一种使用 MultipartEntity 使用 HttpURLConnection 发送文件的更好方法

private static String multipost(String urlString, MultipartEntity reqEntity) 
    try 
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setUseCaches(false);
        conn.setDoInput(true);
        conn.setDoOutput(true);

        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.addRequestProperty("Content-length", reqEntity.getContentLength()+"");
        conn.addRequestProperty(reqEntity.getContentType().getName(), reqEntity.getContentType().getValue());

        OutputStream os = conn.getOutputStream();
        reqEntity.writeTo(conn.getOutputStream());
        os.close();
        conn.connect();

        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) 
            return readStream(conn.getInputStream());
        

     catch (Exception e) 
        Log.e(TAG, "multipart post error " + e + "(" + urlString + ")");
    
    return null;        


private static String readStream(InputStream in) 
    BufferedReader reader = null;
    StringBuilder builder = new StringBuilder();
    try 
        reader = new BufferedReader(new InputStreamReader(in));
        String line = "";
        while ((line = reader.readLine()) != null) 
            builder.append(line);
        
     catch (IOException e) 
        e.printStackTrace();
     finally 
        if (reader != null) 
            try 
                reader.close();
             catch (IOException e) 
                e.printStackTrace();
            
        
    
    return builder.toString();
 

假设您正在上传带有位图数据的图像:

    Bitmap bitmap = ...;
    String filename = "filename.png";
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
    ContentBody contentPart = new ByteArrayBody(bos.toByteArray(), filename);

    MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    reqEntity.addPart("picture", contentPart);
    String response = multipost("http://server.com", reqEntity);

还有,瞧!您的帖子数据将包含图片字段以及服务器上的文件名和路径。

【讨论】:

我注意到你没有调用 conn.disconnect(),是故意的吗? @MihaiTodor 您需要将位图压缩成一个文件,以减少通过网络传递的数据量。 您应该调用 setFixedLengthStreamingMode(reqEntity.getContentLength()) 而不是直接设置 Content-Length 标头。这样,您的数据在通过套接字发送之前不会被缓冲(至少在较新的设备上,在 android 2.3 或更低版本上,它们似乎无论如何都会被缓冲)。我通过使用 okhttp 作为传输解决了旧设备上的缓冲问题,并且可以正常工作。 太糟糕了 MultipartEntity 没有与 Android SDK 绑定 @mente 它与 httpmime 捆绑在一起。我将其用作 Gradle 依赖项:org.apache.httpcomponents:httpmime:4.1.1【参考方案3】:

使用MultipartUtility 以简单的方式上传带有某些参数的服务器上的文件。

MultipartUtility.java

public class MultipartUtility 

    private final String boundary;
    private static final String LINE_FEED = "\r\n";
    private HttpURLConnection httpConn;
    private String charset;
    private OutputStream outputStream;
    private PrintWriter writer;

    /**
     * This constructor initializes a new HTTP POST request with content type
     * is set to multipart/form-data
     *
     * @param requestURL
     * @param charset
     * @throws IOException
     */
    public MultipartUtility(String requestURL, String charset)
            throws IOException 
        this.charset = charset;

        // creates a unique boundary based on time stamp
        boundary = "===" + System.currentTimeMillis() + "===";

        URL url = new URL(requestURL);
        Log.e("URL", "URL : " + requestURL.toString());
        httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setUseCaches(false);
        httpConn.setDoOutput(true); // indicates POST method
        httpConn.setDoInput(true);
        httpConn.setRequestProperty("Content-Type",
                "multipart/form-data; boundary=" + boundary);
        httpConn.setRequestProperty("User-Agent", "CodeJava Agent");
        httpConn.setRequestProperty("Test", "Bonjour");
        outputStream = httpConn.getOutputStream();
        writer = new PrintWriter(new OutputStreamWriter(outputStream, charset),
                true);
    

    /**
     * Adds a form field to the request
     *
     * @param name  field name
     * @param value field value
     */
    public void addFormField(String name, String value) 
        writer.append("--" + boundary).append(LINE_FEED);
        writer.append("Content-Disposition: form-data; name=\"" + name + "\"")
                .append(LINE_FEED);
        writer.append("Content-Type: text/plain; charset=" + charset).append(
                LINE_FEED);
        writer.append(LINE_FEED);
        writer.append(value).append(LINE_FEED);
        writer.flush();
    

    /**
     * Adds a upload file section to the request
     *
     * @param fieldName  name attribute in <input type="file" name="..." />
     * @param uploadFile a File to be uploaded
     * @throws IOException
     */
    public void addFilePart(String fieldName, File uploadFile)
            throws IOException 
        String fileName = uploadFile.getName();
        writer.append("--" + boundary).append(LINE_FEED);
        writer.append(
                "Content-Disposition: form-data; name=\"" + fieldName
                        + "\"; filename=\"" + fileName + "\"")
                .append(LINE_FEED);
        writer.append(
                "Content-Type: "
                        + URLConnection.guessContentTypeFromName(fileName))
                .append(LINE_FEED);
        writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
        writer.append(LINE_FEED);
        writer.flush();

        FileInputStream inputStream = new FileInputStream(uploadFile);
        byte[] buffer = new byte[4096];
        int bytesRead = -1;
        while ((bytesRead = inputStream.read(buffer)) != -1) 
            outputStream.write(buffer, 0, bytesRead);
        
        outputStream.flush();
        inputStream.close();

        writer.append(LINE_FEED);
        writer.flush();
    

    /**
     * Adds a header field to the request.
     *
     * @param name  - name of the header field
     * @param value - value of the header field
     */
    public void addHeaderField(String name, String value) 
        writer.append(name + ": " + value).append(LINE_FEED);
        writer.flush();
    

    /**
     * Completes the request and receives response from the server.
     *
     * @return a list of Strings as response in case the server returned
     * status OK, otherwise an exception is thrown.
     * @throws IOException
     */
    public String finish() throws IOException 
        StringBuffer response = new StringBuffer();

        writer.append(LINE_FEED).flush();
        writer.append("--" + boundary + "--").append(LINE_FEED);
        writer.close();

        // checks server's status code first
        int status = httpConn.getResponseCode();
        if (status == HttpURLConnection.HTTP_OK) 
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    httpConn.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null) 
                response.append(line);
            
            reader.close();
            httpConn.disconnect();
         else 
            throw new IOException("Server returned non-OK status: " + status);
        

        return response.toString();
    

uploadfile连同参数。

注意:将此代码放在下面的非 ui-thread 中以获得响应。

String charset = "UTF-8";
String requestURL = "YOUR_URL";

MultipartUtility multipart = new MultipartUtility(requestURL, charset);
multipart.addFormField("param_name_1", "param_value");
multipart.addFormField("param_name_2", "param_value");
multipart.addFormField("param_name_3", "param_value");
multipart.addFilePart("file_param_1", new File(file_path));
String response = multipart.finish(); // response from server.

【讨论】:

感谢@Jay,您刚刚拯救了我的项目和我的生命。 :D【参考方案4】:

Jaydipsinh Zala 的解决方案对我不起作用,我不知道为什么,但它似乎接近解决方案。

因此,将这一个与 Mihai Todor 的出色解决方案和解释相结合,结果就是目前适合我的这个课程。如果对某人有帮助:

MultipartUtility2V.java

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;

public class MultipartUtilityV2 
    private HttpURLConnection httpConn;
    private DataOutputStream request;
    private final String boundary =  "*****";
    private final String crlf = "\r\n";
    private final String twoHyphens = "--";

    /**
     * This constructor initializes a new HTTP POST request with content type
     * is set to multipart/form-data
     *
     * @param requestURL
     * @throws IOException
     */
    public MultipartUtilityV2(String requestURL)
            throws IOException 

        // creates a unique boundary based on time stamp
        URL url = new URL(requestURL);
        httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setUseCaches(false);
        httpConn.setDoOutput(true); // indicates POST method
        httpConn.setDoInput(true);

        httpConn.setRequestMethod("POST");
        httpConn.setRequestProperty("Connection", "Keep-Alive");
        httpConn.setRequestProperty("Cache-Control", "no-cache");
        httpConn.setRequestProperty(
                "Content-Type", "multipart/form-data;boundary=" + this.boundary);

        request =  new DataOutputStream(httpConn.getOutputStream());
    

    /**
     * Adds a form field to the request
     *
     * @param name  field name
     * @param value field value
     */
    public void addFormField(String name, String value)throws IOException  
        request.writeBytes(this.twoHyphens + this.boundary + this.crlf);
        request.writeBytes("Content-Disposition: form-data; name=\"" + name + "\""+ this.crlf);
        request.writeBytes("Content-Type: text/plain; charset=UTF-8" + this.crlf);
        request.writeBytes(this.crlf);
        request.writeBytes(value+ this.crlf);
        request.flush();
    

    /**
     * Adds a upload file section to the request
     *
     * @param fieldName  name attribute in <input type="file" name="..." />
     * @param uploadFile a File to be uploaded
     * @throws IOException
     */
    public void addFilePart(String fieldName, File uploadFile)
            throws IOException 
        String fileName = uploadFile.getName();
        request.writeBytes(this.twoHyphens + this.boundary + this.crlf);
        request.writeBytes("Content-Disposition: form-data; name=\"" +
                fieldName + "\";filename=\"" +
                fileName + "\"" + this.crlf);
        request.writeBytes(this.crlf);

        byte[] bytes = Files.readAllBytes(uploadFile.toPath());
        request.write(bytes);
    

    /**
     * Completes the request and receives response from the server.
     *
     * @return a list of Strings as response in case the server returned
     * status OK, otherwise an exception is thrown.
     * @throws IOException
     */
    public String finish() throws IOException 
        String response ="";

        request.writeBytes(this.crlf);
        request.writeBytes(this.twoHyphens + this.boundary +
                this.twoHyphens + this.crlf);

        request.flush();
        request.close();

        // checks server's status code first
        int status = httpConn.getResponseCode();
        if (status == HttpURLConnection.HTTP_OK) 
            InputStream responseStream = new
                    BufferedInputStream(httpConn.getInputStream());

            BufferedReader responseStreamReader =
                    new BufferedReader(new InputStreamReader(responseStream));

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

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

            response = stringBuilder.toString();
            httpConn.disconnect();
         else 
            throw new IOException("Server returned non-OK status: " + status);
        

        return response;
    

【讨论】:

这是只对我有用的解决方案。非常感谢。 但是为什么你不在 addFilePart() 结束时刷新?我看到您在 addFormField() 结束时进行了冲洗。 @Georgevik【参考方案5】:

https://***.com/a/33149413/6481542 这个答案让我完成了将大文件上传到开发 Django 服务器的 90%,但我必须使用 setFixedLengthStreamingMode 才能使其正常工作。这需要在编写内容之前设置 Content-Length,因此需要对上述答案进行相当大的重写。这是我的最终结果

public class MultipartLargeUtility 
    private final String boundary;
    private static final String LINE_FEED = "\r\n";
    private HttpURLConnection httpConn;
    private String charset;
    private OutputStream outputStream;
    private PrintWriter writer;
    private final int maxBufferSize = 4096;
    private long contentLength = 0;
    private URL url;

    private List<FormField> fields;
    private List<FilePart> files;

    private class FormField 
        public String name;
        public String value;

        public FormField(String name, String value) 
            this.name = name;
            this.value = value;
        
    

    private class FilePart 
        public String fieldName;
        public File uploadFile;

        public FilePart(String fieldName, File uploadFile) 
            this.fieldName = fieldName;
            this.uploadFile = uploadFile;
        
    

    /**
     * This constructor initializes a new HTTP POST request with content type
     * is set to multipart/form-data
     *
     * @param requestURL
     * @param charset
     * @throws IOException
     */
    public MultipartLargeUtility(String requestURL, String charset, boolean requireCSRF)
            throws IOException 
        this.charset = charset;

        // creates a unique boundary based on time stamp
        boundary = "===" + System.currentTimeMillis() + "===";
        url = new URL(requestURL);
        fields = new ArrayList<>();
        files = new ArrayList<>();

        if (requireCSRF) 
            getCSRF();
        
    

    /**
     * Adds a form field to the request
     *
     * @param name  field name
     * @param value field value
     */
    public void addFormField(String name, String value)
            throws UnsupportedEncodingException 
        String fieldContent = "--" + boundary + LINE_FEED;
        fieldContent += "Content-Disposition: form-data; name=\"" + name + "\"" + LINE_FEED;
        fieldContent += "Content-Type: text/plain; charset=" + charset + LINE_FEED;
        fieldContent += LINE_FEED;
        fieldContent += value + LINE_FEED;
        contentLength += fieldContent.getBytes(charset).length;
        fields.add(new FormField(name, value));
    

    /**
     * Adds a upload file section to the request
     *
     * @param fieldName  name attribute in <input type="file" name="..." />
     * @param uploadFile a File to be uploaded
     * @throws IOException
     */
    public void addFilePart(String fieldName, File uploadFile)
            throws IOException 
        String fileName = uploadFile.getName();

        String fieldContent = "--" + boundary + LINE_FEED;
        fieldContent += "Content-Disposition: form-data; name=\"" + fieldName
                + "\"; filename=\"" + fileName + "\"" + LINE_FEED;
        fieldContent += "Content-Type: "
                + URLConnection.guessContentTypeFromName(fileName) + LINE_FEED;
        fieldContent += "Content-Transfer-Encoding: binary" + LINE_FEED;
        fieldContent += LINE_FEED;
        // file content would go here
        fieldContent += LINE_FEED;
        contentLength += fieldContent.getBytes(charset).length;
        contentLength += uploadFile.length();
        files.add(new FilePart(fieldName, uploadFile));
    

    /**
     * Adds a header field to the request.
     *
     * @param name  - name of the header field
     * @param value - value of the header field
     */
    //public void addHeaderField(String name, String value) 
    //    writer.append(name + ": " + value).append(LINE_FEED);
    //    writer.flush();
    //

    /**
     * Completes the request and receives response from the server.
     *
     * @return a list of Strings as response in case the server returned
     * status OK, otherwise an exception is thrown.
     * @throws IOException
     */
    public List<String> finish() throws IOException 
        List<String> response = new ArrayList<String>();
        String content = "--" + boundary + "--" + LINE_FEED;
        contentLength += content.getBytes(charset).length;

        if (!openConnection()) 
            return response;
        

        writeContent();

        // checks server's status code first
        int status = httpConn.getResponseCode();
        if (status == HttpURLConnection.HTTP_OK) 
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    httpConn.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null) 
                response.add(line);
            
            reader.close();
            httpConn.disconnect();
         else 
            throw new IOException("Server returned non-OK status: " + status);
        
        return response;
    

    private boolean getCSRF()
            throws IOException 
        /// First, need to get CSRF token from server
        /// Use GET request to get the token
        CookieManager cookieManager = new CookieManager();
        CookieHandler.setDefault(cookieManager);
        HttpURLConnection conn = null;

        conn = (HttpURLConnection) url.openConnection();

        conn.setUseCaches(false); // Don't use a Cached Copy
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.getContent();
        conn.disconnect();

        /// parse the returned object for the CSRF token
        CookieStore cookieJar = cookieManager.getCookieStore();
        List<HttpCookie> cookies = cookieJar.getCookies();
        String csrf = null;
        for (HttpCookie cookie : cookies) 
            Log.d("cookie", "" + cookie);
            if (cookie.getName().equals("csrftoken")) 
                csrf = cookie.getValue();
                break;
            
        
        if (csrf == null) 
            Log.d(TAG, "Unable to get CSRF");
            return false;
        
        Log.d(TAG, "Received cookie: " + csrf);

        addFormField("csrfmiddlewaretoken", csrf);
        return true;
    

    private boolean openConnection()
            throws IOException 
        httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setUseCaches(false);
        httpConn.setDoOutput(true);    // indicates POST method
        httpConn.setDoInput(true);
        //httpConn.setRequestProperty("Accept-Encoding", "identity");
        httpConn.setFixedLengthStreamingMode(contentLength);
        httpConn.setRequestProperty("Connection", "Keep-Alive");
        httpConn.setRequestProperty("Content-Type",
                "multipart/form-data; boundary=" + boundary);
        outputStream = new BufferedOutputStream(httpConn.getOutputStream());
        writer = new PrintWriter(new OutputStreamWriter(outputStream, charset),
                true);
        return true;
    

    private void writeContent()
            throws IOException 

        for (FormField field : fields) 
            writer.append("--" + boundary).append(LINE_FEED);
            writer.append("Content-Disposition: form-data; name=\"" + field.name + "\"")
                    .append(LINE_FEED);
            writer.append("Content-Type: text/plain; charset=" + charset).append(
                    LINE_FEED);
            writer.append(LINE_FEED);
            writer.append(field.value).append(LINE_FEED);
            writer.flush();
        

        for (FilePart filePart : files) 
            String fileName = filePart.uploadFile.getName();
            writer.append("--" + boundary).append(LINE_FEED);
            writer.append(
                    "Content-Disposition: form-data; name=\"" + filePart.fieldName
                            + "\"; filename=\"" + fileName + "\"")
                    .append(LINE_FEED);
            writer.append(
                    "Content-Type: "
                            + URLConnection.guessContentTypeFromName(fileName))
                    .append(LINE_FEED);
            writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
            writer.append(LINE_FEED);
            writer.flush();

            FileInputStream inputStream = new FileInputStream(filePart.uploadFile);
            int bufferSize = Math.min(inputStream.available(), maxBufferSize);
            byte[] buffer = new byte[bufferSize];
            int bytesRead = -1;
            while ((bytesRead = inputStream.read(buffer, 0, bufferSize)) != -1) 
                outputStream.write(buffer, 0, bytesRead);
            
            outputStream.flush();
            inputStream.close();
            writer.append(LINE_FEED);
            writer.flush();
        

        writer.append("--" + boundary + "--").append(LINE_FEED);
        writer.close();
    

用法与上述答案大致相同,但我已经包含了 Django 默认使用表单的 CSRF 支持

boolean useCSRF = true;
MultipartLargeUtility multipart = new MultipartLargeUtility(url, "UTF-8",useCSRF);
multipart.addFormField("param1","value");
multipart.addFilePart("filefield",new File("/path/to/file"));
List<String> response = multipart.finish();
Log.w(TAG,"SERVER REPLIED:");
for(String line : response) 
    Log.w(TAG, "Upload Files Response:::" + line);

【讨论】:

嗨尼克,我如何在接收端读取发布的文件?我想读取文件并写入磁盘。【参考方案6】:

基于米海的解决方案,如果有人遇到像我的服务器上发生的那样在服务器上保存图像的问题。将 Bitmap to bytebuffer 部分更改为:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,bos);
        byte[] pixels = bos.toByteArray();

【讨论】:

【参考方案7】:

我没有对此进行测试,但您可以尝试使用 PipedInputStream 和 PipedOutputStream。它可能看起来像:

final Bitmap bmp = … // your bitmap

// Set up Piped streams
final PipedOutputStream pos = new PipedOutputStream(new ByteArrayOutputStream());
final PipedInputStream pis = new PipedInputStream(pos);

// Send bitmap data to the PipedOutputStream in a separate thread
new Thread() 
    public void run() 
        bmp.compress(Bitmap.CompressFormat.PNG, 100, pos);
    
.start();

// Send POST request
try 
    // Construct InputStreamEntity that feeds off of the PipedInputStream
    InputStreamEntity reqEntity = new InputStreamEntity(pis, -1);

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    reqEntity.setContentType("binary/octet-stream");
    reqEntity.setChunked(true);
    httppost.setEntity(reqEntity);
    HttpResponse response = httpclient.execute(httppost);
 catch (Exception e) 
    e.printStackTrace()

【讨论】:

【参考方案8】:

这是我使用发布请求上传照片时所做的。

public void uploadFile(int directoryID, String filePath) 
    Bitmap bitmapOrg = BitmapFactory.decodeFile(filePath);
    ByteArrayOutputStream bao = new ByteArrayOutputStream();

    String upload_url = BASE_URL + UPLOAD_FILE;
    bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);

    byte[] data = bao.toByteArray();

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost(upload_url);
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    try 
        // Set Data and Content-type header for the image
        FileBody fb = new FileBody(new File(filePath), "image/jpeg");
        StringBody contentString = new StringBody(directoryID + "");

        entity.addPart("file", fb);
        entity.addPart("directory_id", contentString);
        postRequest.setEntity(entity);

        HttpResponse response = httpClient.execute(postRequest);
        // Read the response
        String jsonString = EntityUtils.toString(response.getEntity());
        Log.e("response after uploading file ", jsonString);

     catch (Exception e) 
        Log.e("Error in uploadFile", e.getMessage());
    

注意:此代码需要库,因此请按照说明here 获取库。

【讨论】:

很高兴知道有一种方法可以使用最新版本的HttpClient(您的链接已过时。请改用this one),Android 人员只保留向后版本兼容性,而不是使用内置的HttpURLConnection。另一方面,设置它似乎需要更多的工作,所以也许不值得。【参考方案9】:

我发现使用 okHttp 更容易,因为我无法让这些解决方案发挥作用:https://***.com/a/37942387/447549

【讨论】:

【参考方案10】:

我尝试了上述解决方案,但没有一个对我有用。

但是http://www.baeldung.com/httpclient-post-http-request。第 6 行 POST 多部分请求在几秒钟内完成

public void whenSendMultipartRequestUsingHttpClient_thenCorrect() 
  throws ClientProtocolException, IOException 
    CloseableHttpClient client = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost("http://www.example.com");

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.addTextBody("username", "John");
    builder.addTextBody("password", "pass");
    builder.addBinaryBody("file", new File("test.txt"),
      ContentType.APPLICATION_OCTET_STREAM, "file.ext");

    HttpEntity multipart = builder.build();
    httpPost.setEntity(multipart);

    CloseableHttpResponse response = client.execute(httpPost);
    client.close();

【讨论】:

以上是关于使用带有 HttpURLConnection 的 POST 发送文件的主要内容,如果未能解决你的问题,请参考以下文章

带有 CookieManager 的 HttpURLConnection 是不是应该自动处理会话 cookie?

HttpURLConnection读取403错误的响应内容[重复]

HttpURLConnection 应用程序因 ESP8266 崩溃

Jersey 2 - 带有 .target HTTPS

安全使用 HttpURLConnection

HttpURLConnection的使用步骤