Interface Transfer Protocol: HTTP
Interface request method: POST
Data encoding format: UTF-8
Data transmission format: multipart/form-data
need:Transfer files to the given interface using multipart/form-data format.
Tools:
import .slf4j.Slf4j; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import .X509Certificate; import ; import ; /** * <p> * * @author Master of Flower Rat * @version :1.0 * @date 2024/4/12 15:10 */ @Slf4j public class SendFileUtils { /** * Use multipart/form-data to transfer files * Method of sending files * @param url interface address * @param file */ public static String sendMultipartFile(String url, File file) { //Get HttpClient CloseableHttpClient client = getHttpClient(); HttpPost httpPost = new HttpPost(url); fillMethod(httpPost,()); // Request parameter configuration RequestConfig requestConfig = ().setSocketTimeout(60000).setConnectTimeout(60000) .setConnectionRequestTimeout(10000).build(); (requestConfig); String res = ""; String fileName = ();//file name try { MultipartEntityBuilder builder = (); (("UTF-8")); (HttpMultipartMode.BROWSER_COMPATIBLE); /** * Suppose there are two parameters that need to be transmitted * Parameter name:filaName value "file name" * Parameter name: file value: file (This parameter value is a file object) */ //Normal parameters in the form ("filaName ",new StringBody("source", ("text/plain", Consts.UTF_8))); // File parameters in the form Note that the first parameter must be written with the parameter name ("file", file, ("multipart/form-data",Consts.UTF_8), fileName); HttpEntity entity = (); (entity); HttpResponse response = (httpPost);// Perform a submission if (().getStatusCode() == HttpStatus.SC_OK) { // Return the response result res = ((), ("UTF-8")); }else { res = "Response failed"; ("The response failed!"); } return res; } catch (Exception e) { (); ("Called HttpPost failed!" + ()); } finally { if (client != null) { try { (); } catch (IOException e) { ("Close HttpPost connection failed!"); } } } ("Data transmission was successful!!!!!!!!!!!!!!!!!!!!!!!!"); return res; } /** * Get HttpClient * @return */ private static CloseableHttpClient getHttpClient(){ SSLContext sslContext = null; try { sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { return true; } }).build(); } catch (Exception e) { (); throw new RuntimeException(e); } SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, ); CloseableHttpClient client = ().setSSLSocketFactory(sslConnectionSocketFactory).build(); return client; } /** * Add header file information * @param requestBase * @param timestamp */ private static void fillMethod(HttpRequestBase requestBase, long timestamp){ // Here is an example, which header information needs to be added to add it yourself //Set timestamp, nginx, underscores_in_headers on; put it in http configuration, otherwise nginx will ignore the header information containing "_" ("timestamp",(timestamp)); (()); } }
Required dependencies:
<!-- /artifact//httpclient --> <dependency> <groupId></groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency>
Summarize
This is the article about sending post requests to java using multipart/form-data format file data to interface. For more related Java sending post request file data to interface content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!