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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 表单请求_java模拟表单请求

發布時間:2025/3/19 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 表单请求_java模拟表单请求 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

表單普通字段

public class FormFieldKeyValuePair {

private static final long serialVersionUID = 1L;

// The form field used for receivinguser's input,

// such as "username" in ""

private String key;

// The value entered by user in thecorresponding form field,

// such as "Patrick" the abovementioned formfield "username"

private String value;

public FormFieldKeyValuePair(String key, String value)

{

this.key = key;

this.value = value;

}

public String getKey()

{

return key;

}

public void setKey(String key) {

this.key = key;

}

public String getValue()

{

return value;

}

public void setValue(String value)

{

this.value = value;

}

}

表單文件

import java.io.Serializable;

public class UploadFileItem implements Serializable {

private static final long serialVersionUID = 1L;

// The form field name in a form used foruploading a file,

// such as "upload1" in ""

private String formFieldName;

// File name to be uploaded, thefileName contains path,

// such as "E:\\some_file.jpg"

private String fileName;

public UploadFileItem(String formFieldName, String fileName)

{

this.formFieldName = formFieldName;

this.fileName = fileName;

}

public String getFormFieldName()

{

return formFieldName;

}

public void setFormFieldName(String formFieldName)

{

this.formFieldName = formFieldName;

}

public String getFileName()

{

return fileName;

}

public void setFileName(String fileName)

{

this.fileName = fileName;

}

}

模擬表單的類

import java.io.BufferedReader;

import java.io.DataInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.ArrayList;

public class HttpPostEmulator {

// 每個post參數之間的分隔。隨意設定,只要不會和其他的字符串重復即可。

private static final String BOUNDARY = "----------HV2ymHFg03ehbqgZCaKO6jyH";

public String sendHttpPostRequest(String serverUrl,

ArrayList generalFormFields,

ArrayList filesToBeUploaded) throws Exception {

// 向服務器發送post請求

URL url = new URL(serverUrl/* "http://127.0.0.1:8080/test/upload" */);

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// 發送POST請求必須設置如下兩行

connection.setDoOutput(true);

connection.setDoInput(true);

connection.setUseCaches(false);

connection.setRequestMethod("POST");

connection.setRequestProperty("Connection", "Keep-Alive");

connection.setRequestProperty("Charset", "UTF-8");

connection.setRequestProperty("Content-Type",

"multipart/form-data; boundary=" + BOUNDARY);

// 頭

String boundary = BOUNDARY;

// 傳輸內容

StringBuffer contentBody = new StringBuffer("--" + BOUNDARY);

// 尾

String endBoundary = "\r\n--" + boundary + "--\r\n";

OutputStream out = connection.getOutputStream();

// 1. 處理文字形式的POST請求

for (FormFieldKeyValuePair ffkvp : generalFormFields)

{

contentBody.append("\r\n")

.append("Content-Disposition: form-data; name=\"")

.append(ffkvp.getKey() + "\"")

.append("\r\n")

.append("\r\n")

.append(ffkvp.getValue())

.append("\r\n")

.append("--")

.append(boundary);

}

String boundaryMessage1 = contentBody.toString();

out.write(boundaryMessage1.getBytes("utf-8"));

// 2. 處理文件上傳

for (UploadFileItem ufi : filesToBeUploaded)

{

contentBody = new StringBuffer();

contentBody.append("\r\n")

.append("Content-Disposition:form-data; name=\"")

.append(ufi.getFormFieldName() + "\"; ") // form中field的名稱

.append("filename=\"")

.append(ufi.getFileName() + "\"") // 上傳文件的文件名,包括目錄

.append("\r\n")

.append("Content-Type:application/octet-stream")

.append("\r\n\r\n");

String boundaryMessage2 = contentBody.toString();

out.write(boundaryMessage2.getBytes("utf-8"));

// 開始真正向服務器寫文件

File file = new File(ufi.getFileName());

DataInputStream dis = new DataInputStream(new FileInputStream(file));

int bytes = 0;

byte[] bufferOut = new byte[(int) file.length()];

bytes = dis.read(bufferOut);

out.write(bufferOut, 0, bytes);

dis.close();

contentBody.append("------------HV2ymHFg03ehbqgZCaKO6jyH");

String boundaryMessage = contentBody.toString();

out.write(boundaryMessage.getBytes("utf-8"));

// System.out.println(boundaryMessage);

}

out.write("------------HV2ymHFg03ehbqgZCaKO6jyH--\r\n"

.getBytes("UTF-8"));

// 3. 寫結尾

out.write(endBoundary.getBytes("utf-8"));

out.flush();

out.close();

// 4. 從服務器獲得回答的內容

String strLine = "";

String strResponse = "";

InputStream in = connection.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(in));

while ((strLine = reader.readLine()) != null)

{

strResponse += strLine + "\n";

}

// System.out.print(strResponse);

return strResponse;

}

}

測試代碼

import java.util.ArrayList;

public class Test {

public static void main(String[] args) {

// 設定服務地址

String serverUrl = "http://localhost:8088/api/extension/upload_file";//上傳地址

// 設定要上傳的普通Form Field及其對應的value

ArrayList ffkvp = new ArrayList();

// 設定要上傳的文件

ArrayList ufi = new ArrayList();

ufi.add(new UploadFileItem("file", "D:\\1.jpg"));

HttpPostEmulator hpe = new HttpPostEmulator();

String response;

try {

response = hpe.sendHttpPostRequest(serverUrl, ffkvp, ufi);

System.out.println("Responsefrom server is: " + response);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

總結

以上是生活随笔為你收集整理的java 表单请求_java模拟表单请求的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。