从 android 应用程序 java HttpURLConnection 多部分表单数据发送图像文件和文本

Sending Image files and Text from android app java HttpURLConnection multipart form-data

您好,我正在尝试使用多部分表单数据 HttpURLConnection java 上传文件和文本,以下代码示例:

String boundary = UUID.randomUUID().toString();
    final String LINE_FEED = "\r\n";
    OutputStream outputStream;
    PrintWriter writer;

URL url_upload = new URL(url_api );

HttpURLConnection conn = (HttpURLConnection) url_upload.openConnection();
            conn.setUseCaches(false);
            conn.setDoOutput(true); // indicates POST method
            conn.setDoInput(true);
            conn.setRequestMethod("POST");
            
            conn.setRequestProperty( "Content-Type", "multipart/form-data;boundary=" + boundary);
            conn.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");
            conn.setRequestProperty("Connection", "Keep-Alive");



            outputStream = conn.getOutputStream();
            writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true);
            //DataOutputStream request = new DataOutputStream(conn.getOutputStream());

            //Username
            writer.append("--").append(boundary).append(LINE_FEED);
            writer.append("Content-Disposition: form-data; name=\"" + "Username" + "\"")
                    .append(LINE_FEED);
            writer.append("Content-Type: text/plain; charset=").append("UTF-8").append(
                    LINE_FEED);
            writer.append(LINE_FEED);
            writer.append(Username).append(LINE_FEED);
            writer.flush();

            //Password
            writer.append("--").append(boundary).append(LINE_FEED);
            writer.append("Content-Disposition: form-data; name=\"" + "Password" + "\"")
                    .append(LINE_FEED);
            writer.append("Content-Type: text/plain; charset=").append("UTF-8").append(
                    LINE_FEED);
            writer.append(LINE_FEED);
            writer.append(Password);
            writer.flush();
            writer.append(LINE_FEED);


            if(Food_image_file_name != null)
            {
                Log.i("File:", String.valueOf(file1));
                

                writer.append("--").append(boundary).append(LINE_FEED);
                writer.append("Content-Disposition: form-data; name=\"" + "file1" + "\"; filename=\"").append(file1.getName()).append("\"")
                        .append(LINE_FEED);
                writer.append("Content-Type: ").append(URLConnection.guessContentTypeFromName(file1.getName()))
                        .append(LINE_FEED);
                writer.append("Content-Transfer-Encoding: binary");
                writer.append(LINE_FEED);
                writer.flush();
                writer.append(LINE_FEED);

                FileInputStream inputStream = new FileInputStream(String.valueOf(file1.toPath()));
                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();


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


其他详细信息: I/File:: /storage/emulated/0/DCIM/100ANDRO/DSC_0117.JPG I/Api 响应:400,错误请求

注意: 我从 postman 测试了服务器,它与 Postman 一起工作没有问题,文件从 postman.

成功上传到服务器

这个 link 是另一个 post 网络样本 api 我用来阅读请求的

更新: 我不得不稍微修改一下我的网站 api,因为我没有正确使用,代码现在可以正常工作了

I/File:: /storage/emulated/0/Pictures/Viber/IMG-8bf36fec3b3c01df23c7faf763e2eac7-V.jpg
I/Boundary: ****
I/Image Content type: image/jpeg

在 Blob 图像 Azure 中,我看到图像不可见,它已损坏。

所以我的最后一期是写图片,下面的正确代码,现在就像一个魅力 ;):

writer.append("--").append(boundary).append(LINE_FEED);
                writer.append("Content-Disposition: form-data; name=\"" + "file1" + "\"; filename=\"").append(file1.getName()).append("\"")
                        .append(LINE_FEED);
                writer.append("Content-Type: ").append(mimeType)
                        .append(LINE_FEED);
                writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
                writer.append(LINE_FEED);
                writer.flush();

所以我的最后一期是写图片,下面的正确代码,现在很有魅力 ;):

writer.append("--").append(boundary).append(LINE_FEED);
                writer.append("Content-Disposition: form-data; name=\"" + "file1" + "\"; filename=\"").append(file1.getName()).append("\"")
                        .append(LINE_FEED);
                writer.append("Content-Type: ").append(mimeType)
                        .append(LINE_FEED);
                writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
                writer.append(LINE_FEED);
                writer.flush()