In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the method of "Java http calling interface to submit forms and documents". In daily operation, I believe that many people have doubts about the methods of submitting forms and documents by calling Java http interface. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Java http calling interface to submit forms and documents". Next, please follow the editor to study!
Java HttpURLConnection usage
/ * * / package com.demo.java;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLConnection;import java.util.List;import java.util.Map;/** *
Description:
* * @ author saohuo * @ date November 4, 2019 * @ version * / public class MultipartFormDataPost {/ / multipart/form-data; form field submission separator private static String boundary = "- 69695201314"; public void submit (Map generalField, List files) throws Exception {HttpURLConnection connection = null; OutputStream os = null URL url = new URL ("http:"); connection = (HttpURLConnection) url.openConnection (); connection.setDoInput (true); connection.setDoOutput (true); connection.setRequestMethod ("POST"); connection.setUseCaches (false); connection.setRequestProperty ("Connection", "Keep-Alive") Connection.setRequestProperty ("Accept-Charset", "UTF-8"); connection.setRequestProperty ("content-type", "multipart/form-data;boundary=" + boundary); os = new DataOutputStream (connection.getOutputStream ()); / / text parameter assembly StringBuffer textFormBody = new StringBuffer (); Map formField = generalField FormField.forEach ((KJE v)-> {textFormBody.append ("\ r\ n"). Append ("- -") .append (boundary) .append ("\ r\ n"); textFormBody.append ("Content-Disposition: form-data; name=\"+ k +"\ "); textFormBody.append ("\ r\ n\ r\ n ") / / there should be two carriage return line feed textFormBody.append (v);}) between the name and data; / / submit the text form os.write (textFormBody.toString () .getBytes ("UTF-8")) / / File data if (files! = null & &! files.isEmpty ()) {int fileIndex = 0; for (File file: files) {String filename = file.getName () String fileMinetype = URLConnection.getFileNameMap (). GetContentTypeFor (filename); StringBuffer fileFormBody = new StringBuffer (); fileFormBody.append ("\ r\ n"). Append ("- -") .append (boundary) .append ("\ r\ n") FileFormBody.append ("Content-Disposition: form-data; name=\" files ["+ fileIndex+"]\ "; filename=\"+ filename +"\ "); fileFormBody.append ("\ r\ n "); fileFormBody.append (" Content-Type: "+ fileMinetype) FileFormBody.append ("\ r\ n\ r\ n"); / / basic information of the submission file (file name, file length, file type, etc.) os.write (fileFormBody.toString (). GetBytes ()); InputStream fileStream = new FileInputStream (file) DataInputStream fileDataIs = new DataInputStream (fileStream); int filebytes = 0; byte [] filebufferOut = new byte [1024] While ((filebytes = fileDataIs.read (filebufferOut))! =-1) {os.write (filebufferOut, 0, filebytes);} fileDataIs.close (); fileIndex++ }} / / File write end / / Last write end flag byte [] endBodyData = ("\ r\ nmuri -" + boundary + "- -\ r\ n") .getBytes (); os.write (endBodyData); os.flush () Int responseCode = connection.getResponseCode (); / / submit the result status code System.out.println (responseCode);}} at this point, the study on "the method of Java http calling the interface to submit forms and files" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.