java模擬複雜表單post請求

java模擬複雜表單post請求
能支持文件上傳

/**
	 * 支持複雜表單,比如文件上傳
	 * @param formParam
	 * @return
	 * @throws Exception
	 */
	public static String postWithForm(FormParam formParam) throws Exception {
		String url = formParam.getUrl();
		String charset = "UTF-8";
		String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
		String CRLF = "\r\n"; // Line separator required by multipart/form-data.

		URLConnection connection = new URL(url).openConnection();
		connection.setDoOutput(true);
		connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
		try (
				OutputStream output = connection.getOutputStream();
				PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
			) {
			// make body param
			Map<String, String> bodyParam = formParam.getBodyParam();
			if (null != bodyParam) {
				for (String p : bodyParam.keySet()) {
					writer.append("--" + boundary).append(CRLF);
					writer.append("Content-Disposition: form-data; name=\"" + p + "\"").append(CRLF);
					writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
					writer.append(CRLF).append(bodyParam.get(p)).append(CRLF).flush();
				}
			}
			// Send file.
			Map<String, File> fileParam = formParam.getFileParam();
			if (null != fileParam) {
				for (String fileName : fileParam.keySet()) {
					writer.append("--" + boundary).append(CRLF);
					writer.append("Content-Disposition: form-data; name=\"" + fileName + "\"; filename=\""
							+ fileParam.get(fileName).getName() + "\"").append(CRLF);
					writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(fileName)).append(CRLF);
					writer.append("Content-Transfer-Encoding: binary").append(CRLF);
					writer.append(CRLF).flush();
					Files.copy(fileParam.get(fileName).toPath(), output);
					output.flush(); // Important before continuing with writer!
					writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.
				}
			}
			// End of multipart/form-data.
			writer.append("--" + boundary + "--").append(CRLF).flush();
		}
		HttpURLConnection conn = (HttpURLConnection) connection;
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		int len;
		byte[] buffer = new byte[1024];
		while ((len = conn.getInputStream().read(buffer)) != -1) {
			bout.write(buffer, 0, len);
		}
		String result = new String(bout.toByteArray(), "utf-8");
		return result;
	}

FormParam封裝類:

package net.riking.core.utils;

import java.io.File;
import java.util.Map;

public class FormParam {
	private String url;
//	private String auth;
//	/**
//	 * http請求頭裏的參數
//	 */
//	private Map<String, String> headerParam;
	/**
	 * 常規參數
	 */
	private Map<String, String> bodyParam;
	/**
	 * 待上傳的文件參數 filename和file
	 */
	private Map<String, File> fileParam;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

//	public String getAuth() {
//		return auth;
//	}
//
//	public void setAuth(String auth) {
//		this.auth = auth;
//	}
//
//	public Map<String, String> getHeaderParam() {
//		return headerParam;
//	}
//
//	public void setHeaderParam(Map<String, String> headerParam) {
//		this.headerParam = headerParam;
//	}

	public Map<String, String> getBodyParam() {
		return bodyParam;
	}

	public void setBodyParam(Map<String, String> bodyParam) {
		this.bodyParam = bodyParam;
	}

	public Map<String, File> getFileParam() {
		return fileParam;
	}

	public void setFileParam(Map<String, File> fileParam) {
		this.fileParam = fileParam;
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章