日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

java 接口表单提交_Java http 调用接口提交表单以及文件

發(fā)布時(shí)間:2024/7/23 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 接口表单提交_Java http 调用接口提交表单以及文件 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Java HttpURLConnection 使用

/**

*

*/

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;

/**

*

描述:

*

* @author saohuo

* @date 2019年11月4日

* @version

*/

public class MultipartFormDataPost {

// multipart/form-data; 表單各字段提交分隔符

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());

// 文本參數(shù)組裝

StringBuffer textFormBody = new StringBuffer();

Map formField = generalField;

formField.forEach((k,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");//名稱(chēng)與數(shù)據(jù)之間要有兩個(gè)回車(chē)換行

textFormBody.append(v);

});

// 提交文本表單

os.write(textFormBody.toString().getBytes("UTF-8"));

// 文件數(shù)據(jù)

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");

// 提交文件基本信息(文件名、文件長(zhǎng)度、文件類(lèi)型等)

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++;

}

} // 文件寫(xiě)入結(jié)束

// 最后寫(xiě)入結(jié)束標(biāo)識(shí)

byte[] endBodyData = ("\r\n--" + boundary + "--\r\n").getBytes();

os.write(endBodyData);

os.flush();

int responseCode = connection.getResponseCode();

// 提交結(jié)果狀態(tài)碼

System.out.println(responseCode);

}

}

總結(jié)

以上是生活随笔為你收集整理的java 接口表单提交_Java http 调用接口提交表单以及文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。