日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

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

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

表單普通字段

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參數(shù)之間的分隔。隨意設(shè)定,只要不會和其他的字符串重復(fù)即可。

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

public String sendHttpPostRequest(String serverUrl,

ArrayList generalFormFields,

ArrayList filesToBeUploaded) throws Exception {

// 向服務(wù)器發(fā)送post請求

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

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

// 發(fā)送POST請求必須設(shè)置如下兩行

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;

// 傳輸內(nèi)容

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

// 開始真正向服務(wù)器寫文件

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. 寫結(jié)尾

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

out.flush();

out.close();

// 4. 從服務(wù)器獲得回答的內(nèi)容

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) {

// 設(shè)定服務(wù)地址

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

// 設(shè)定要上傳的普通Form Field及其對應(yīng)的value

ArrayList ffkvp = new ArrayList();

// 設(shè)定要上傳的文件

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

}

}

}

總結(jié)

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

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