JAVA HttpConnection POST发送图像和文本

JAVA HttpConnection POST发送图像和文本,java,image,post,http-status-code-400,telegram-bot,Java,Image,Post,Http Status Code 400,Telegram Bot,我试图通过HTTP POST发送文本和图像,但总是收到400错误。我想用它通过电报机器人发送一个图像,我正在使用这个答案中提供的代码,并对文本和咬合参数进行小的修改 可能是什么错误?提前感谢 String param = "chat_id=mi_id&photo="; String charset = "UTF-8"; String request = "https

我试图通过HTTP POST发送文本和图像,但总是收到400错误。我想用它通过电报机器人发送一个图像,我正在使用这个答案中提供的代码,并对文本和咬合参数进行小的修改

可能是什么错误?提前感谢

                    String param  = "chat_id=mi_id&photo=";
                    String charset = "UTF-8"; 
                    String request = "https://api.telegram.org/bot-botCOde/sendPhoto";
                    URL url = new URL( request );
                    File binaryFile = new File("/home/joseantonio/imagen.jpg");
                    String CRLF = "\r\n"; // Line separator required by multipart/form-data.
                    String boundary = Long.toHexString(System.currentTimeMillis());

                    HttpURLConnection conn= (HttpURLConnection) url.openConnection();           
                    conn.setDoOutput( true );
                    conn.setInstanceFollowRedirects( false );
                    conn.setRequestMethod( "POST" );
                    conn.setRequestProperty( "charset", "utf-8");
                    conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
                        conn.setUseCaches( false );
                    try{ 
                         OutputStream output = conn.getOutputStream();
                         PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);

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

                         writer.append("--" + boundary).append(CRLF);
                         writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
                         writer.append("Content-Type: image/jpg").append(CRLF);
                         writer.append("Content-Transfer-Encoding: binary").append(CRLF);
                         writer.append(CRLF).flush();                            
                         Files.copy(binaryFile.toPath(), output);
                         output.flush(); // Important before continuing with writer!
                         writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.

                         // End of multipart/form-data.
                         writer.append("--" + boundary + "--").append(CRLF).flush();

                        int responseCode2 = conn.getResponseCode();

                        System.out.println("\nSending 'GET' request to URL : " + request);
                        System.out.println("Response Code : " + responseCode2);

我相信您正在使用https url这一事实是导致您收到错误的原因。当您提交到https url时,您需要处理一些不同的事情。看一个例子