如何在android中使用HttpURLConnection上传包含其他字符串数据的文件?

2024-01-31

我想使用 HttpURLConnection 在一个请求中将包含其他字符串数据的文件上传到服务器(不使用 MultiPartEntityBuilder)...目前我可以发送文件,但不能发送其他字符串数据!这是我当前将文件发送到服务器的代码:

    HttpURLConnection.setFollowRedirects(false);
    HttpURLConnection connection = null;
    String fileName = sourceFile.getName();

    try {
        connection = (HttpURLConnection) new URL(FILE_UPLOAD_URL).openConnection();
        connection.setRequestMethod("POST");
        String boundary = "---------------------------boundary";
        String tail = "\r\n--" + boundary + "--\r\n";
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        connection.setRequestProperty("token", sharedpreferences.getString("token", ""));
        connection.setRequestProperty("app_version", app_version);
        connection.setRequestProperty("api_version", api_version);
        connection.setDoOutput(true);

        String metadataPart = "--"
                + boundary
                + "\r\n"
                + "Content-Disposition: form-data; name=\"metadata\"\r\n\r\n"
                + ""
                + "\r\n";

        String fileHeader1 = "--"
                + boundary
                + "\r\n"
                + "Content-Disposition: form-data; name=\"myFile\"; filename=\""
                + fileName
                + "\"\r\n"
                + "Content-Type: application/octet-stream\r\n"
                + "Content-Transfer-Encoding: binary\r\n";

        long fileLength = sourceFile.length() + tail.length();
        String fileHeader2 = "Content-length: " + fileLength + "\r\n";
        String fileHeader = fileHeader1 + fileHeader2 + "\r\n";
        String stringData = metadataPart + fileHeader;

        long requestLength = stringData.length() + fileLength;
        connection.setRequestProperty("Content-length", "" + requestLength);
        connection.setFixedLengthStreamingMode((int) requestLength);
        connection.connect();

        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
        out.writeBytes(stringData);
        out.flush();

        int progress = 0;
        int bytesRead;
        byte buf[] = new byte[1024];
        BufferedInputStream bufInput = new BufferedInputStream(new FileInputStream(sourceFile));
        while ((bytesRead = bufInput.read(buf)) != -1) {
            // write output
            out.write(buf, 0, bytesRead);
            out.flush();
            progress += bytesRead; // Here progress is total uploaded bytes

            publishProgress((int) ((progress * 100) / sourceFile.length())); // sending progress percent to publishProgress
        }

        // Write closing boundary and close stream
        out.writeBytes(tail);
        out.flush();
        out.close();

        // Get server response
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        StringBuilder builder = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }

    } catch (Exception e) {
        // Exception
    } finally {
        if (connection != null) connection.disconnect();
    }
    return null;

任何帮助将不胜感激!!谢谢...


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在android中使用HttpURLConnection上传包含其他字符串数据的文件? 的相关文章

随机推荐