美文网首页
使用HttpURLConnection进行multipart/f

使用HttpURLConnection进行multipart/f

作者: mFrankHon | 来源:发表于2020-08-16 14:28 被阅读0次
        @WorkerThread
        private String postImage(String urlStr, String formDataKey, String formData, String fileKey, String fileContentType, File file) {
            String dash = "--";
            String boundary = generateBoundary();
            String newLine = "\r\n";
            HttpURLConnection connection = null;
            DataOutputStream outputStream = null;
            FileInputStream inputStream = null;
            try {
                URL url = new URL(urlStr);
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Connection", "Keep-Alive");
                connection.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");
                connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setUseCaches(false);
    
                outputStream = new DataOutputStream(connection.getOutputStream());
                // form data
                String knowledgeRequest = dash + boundary + newLine +
                        "Content-Disposition: form-data; name=\"" + formDataKey + "\"" + newLine +
                        "Content-Type: text/plain; charset=utf-8" + newLine +
                        newLine +// important !
                        formData + newLine +
                        dash + boundary + newLine;
                outputStream.writeBytes(knowledgeRequest);
                // file data
                String fileHeader = "Content-Disposition: form-data; name=\"" + fileKey + "\"; filename=\"" + file.getName() + "\"" + newLine +
                        "Content-Type:" + fileContentType + newLine +
                        "Content-Transfer-Encoding: binary" + newLine +
                        newLine;// important !
                outputStream.writeBytes(fileHeader);
                byte[] buffer = new byte[1024];
                int count;
                inputStream = new FileInputStream(file);
                while (true) {
                    count = inputStream.read(buffer);
                    if (count == -1) {
                        break;
                    }
                    outputStream.write(buffer, 0, count);
                }
                outputStream.writeBytes(newLine);// important !
                outputStream.writeBytes(dash + boundary + dash + newLine);
                outputStream.flush();
                int statusCode = connection.getResponseCode();
                StringBuilder response = new StringBuilder();
                if (statusCode == 200) {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    String line;
                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }
                    return response.toString();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (connection != null) {
                        connection.disconnect();
                    }
                    if (outputStream != null) {
                        outputStream.close();
                    }
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    
        private String generateBoundary() {
            return "--------------------------" +
                    UUID.randomUUID().toString().replace("-", "");
        }
    

    相关文章

      网友评论

          本文标题:使用HttpURLConnection进行multipart/f

          本文链接:https://www.haomeiwen.com/subject/kmvtjktx.html