Android HTTP POST 文件上传不适用于 HttpUrlConnection

标签 android http-post out-of-memory httpurlconnection

我正在尝试通过 HTTP POST 从我的设备将文件上传到服务器。我有两个方法 upload1 和 upload2。

Upload1 使用 HttpPost 类并且它可以工作,但是对于较大的文件,它会抛出内存不足异常。

Upload2 使用HttpURLConnection,但它不起作用。 (我从服务器收到 BAD REQUEST 消息。)我希望 upload2 正常工作,因为它使用流发送数据并且不会抛出内存不足异常。 我在wireshark中查看了包,标题似乎是相同的,但是upload1的长度是380,upload2只有94。可能是什么问题?

private void upload1(File file) {

        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url + "?recname="
                    + fileName);

            // ///////////////////////////////////////
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "---------------------------This is the boundary";

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(baos);

            FileInputStream fin = new FileInputStream(file);

            byte audioData[] = new byte[(int) file.length()];

            fin.read(audioData);
            fin.close();

            // Send a binary file
            dos.writeBytes(twoHyphens + boundary + lineEnd);

            dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\""
                    + fileName + "\"" + lineEnd);
            dos.writeBytes("Content-Type: audio/mp4" + lineEnd);
            dos.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
            dos.writeBytes(lineEnd);
            dos.write(audioData);
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
            dos.flush();
            dos.close();

            ByteArrayInputStream content = new ByteArrayInputStream(
                    baos.toByteArray());
            BasicHttpEntity entity = new BasicHttpEntity();
            entity.setContent(content);

            entity.setContentLength(baos.toByteArray().length);

            httppost.addHeader("Content-Type", "multipart/form-data; boundary="
                    + boundary);

            httppost.setEntity(entity);

            // //////////////////////////////////

            HttpResponse response = httpclient.execute(httppost);

            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent(), "UTF-8"));
            StringBuilder builder = new StringBuilder();

            for (String line = null; (line = reader.readLine()) != null;) {
                builder.append(line).append("\n");
            }

            reader.close();
            message = builder.toString();
            System.out.println(message);

        } catch (UnknownHostException e) {
            message = "Error! Server is unreachable. Check you internet connection!";
        } catch (Exception e) {
            message = "error: " + e.toString();
        }

    }

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private void upload2(File file) {

        HttpURLConnection connection = null;

        String pathToOurFile = file.getPath();
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "---------------------------This is the boundary";

        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024;

        try {

            fileInputStream = new FileInputStream(new File(pathToOurFile));

            URL server_url = new URL(url+ "?recname="
                    + fileName);
            connection = (HttpURLConnection) server_url.openConnection();

            // Allow Inputs & Outputs
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);

            // Enable POST method
            connection.setRequestMethod("POST");

            String bodyHeader = twoHyphens
                    + boundary
                    + lineEnd
                    + "Content-Disposition: form-data; name=\"file\";filename=\""
                    + fileName + "\"" + lineEnd + "Content-Type: audio/mp4"
                    + lineEnd + "Content-Transfer-Encoding: binary" + lineEnd
                    + lineEnd + twoHyphens + boundary + twoHyphens ;

            byte[] bodyHeaderAray = bodyHeader.getBytes();

            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type",
                    "multipart/form-data;boundary=" + boundary);
            connection.setRequestProperty("Expect", "100-continue");

            // Content-Length
            int bodyHeaderSize = (int) file.length() + bodyHeaderAray.length;
            System.out.println("body header size: " + bodyHeaderSize);
            // connection.setFixedLengthStreamingMode(bodyHeaderSize);

            outputStream = new DataOutputStream(connection.getOutputStream());
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream
                    .writeBytes("Content-Disposition: form-data; name=\"file\";filename=\""
                            + fileName + "\"");
            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes("Content-Type: audio/mp4" + lineEnd);
            outputStream.writeBytes("Content-Transfer-Encoding: binary"
                    + lineEnd);

            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];

            // Read file
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while (bytesRead > 0) {
                outputStream.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }

            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(twoHyphens + boundary + twoHyphens
                    + lineEnd);

            fileInputStream.close();
            outputStream.flush();
            outputStream.close();

            // Responses from the server (code and message)
            int serverResponseCode = connection.getResponseCode();
            String serverResponseMessage = connection.getResponseMessage();

            message = serverResponseMessage;

        } catch (Exception ex) {
            System.out.println(ex);
        }
    }

最佳答案

根据您上传文件时遇到的问题。您应该使用Multipart机制将文件上传到服务器。

    httpclient-4.1.jar
    httpcore-4.1.jar
    httpmime-4.1.jar
    apache-mime4j-0.6.1.jar

为此,您需要在项目中添加几个库并将其用于 HTTP 连接和文件数据。

    private boolean uploadFile(File mFile) {
            boolean success = true;
            String filename = mFile.getName();
            MultipartEntity data_to_send = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);
            try {
                data_to_send.addPart(
                        "name",
                        new StringBody(
                                filename.substring(filename.lastIndexOf("/") + 1)));
                data_to_send.addPart("fileData", new FileBody(mFile));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            try {
                String responseData = ConsumeWebService.sendRequest(data_to_send,
                        Global.BASE_URL + serviceUrl);
                if (TextUtils.isEmpty(responseData)
                        || responseData.equals(ConsumeWebService.ERROR_CODE)) {
                    success = false;
                }
            } catch (Exception e) {
                success = false;
                e.printStackTrace();
            }
            return success;
        }


public static String sendRequest(MultipartEntity data, String url) {
        String response = "";
        response = postData(url, data);
        return response;
    }


private static String postData(String url, MultipartEntity data) {
        String strResponse = "";

        try {
            Log.d(Global.TAG, "Post URL is " + url);
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(data);
            strResponse = httpClient.execute(httpPost,
                    new BasicResponseHandler());
        } catch (UnsupportedEncodingException e) {
            strResponse = ERROR_CODE;
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            strResponse = ERROR_CODE;
            e.printStackTrace();
        } catch (IOException e) {
            strResponse = ERROR_CODE;
            e.printStackTrace();
        }
        return strResponse;
    }

关于Android HTTP POST 文件上传不适用于 HttpUrlConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17857034/

相关文章:

android - 64K 方法计数超出错误,接近 62.5K 方法引用计数

android - android 多久请求一次位置更新?

android - 对 Android 文档 : Fragments 的澄清

javascript - 如何使用 Angular 正确实现 post

php - 引号和 mysql 插入的问题

java - "Requested array size exceeds VM limit"位于 java.io.PrintWriter.newLine

object - 在 RavenDB 中存储大文档导致客户端内存不足

android - 在sqlite android中创建用户定义的函数?

java - "java.lang.OutOfMemoryError : unable to create new native Thread"

Python 将数据发布到 url