Java HttpUrlConnection form-data方式提交数据

网络请求连接代码如下:

public static String openUrl(String url, RequestParameter parameter) throws Exception {
	URL urls = new URL(url);
	HttpURLConnection connection = null;
	OutputStream outputStream = null;
	String rs = null;
	try {
		connection = (HttpURLConnection) urls.openConnection();
		connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=----footfoodapplicationrequestnetwork");
		connection.setDoOutput(true);
		connection.setDoInput(true);
		connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
		connection.setRequestProperty("Accept", "*/*");
		connection.setRequestProperty("Range", "bytes="+"");
		connection.setConnectTimeout(8000);
		connection.setReadTimeout(20000);
		connection.setRequestMethod("POST");
		
		StringBuffer buffer = new StringBuffer();
		int len = 0;
		if(parameter != null)
			len = parameter.size();
		
		for(int i = 0; i < len; i++) {
			buffer.append("------footfoodapplicationrequestnetwork\r\n");
			buffer.append("Content-Disposition: form-data; name=\"");
			buffer.append(parameter.getKey(i));
			buffer.append("\"\r\n\r\n");
			buffer.append(parameter.getValue(i));
			buffer.append("\r\n");
		}
		if(parameter != null)
			buffer.append("------footfoodapplicationrequestnetwork--\r\n");
		outputStream = connection.getOutputStream();
		outputStream.write(buffer.toString().getBytes());
		try {
			connection.connect();
			if(connection.getResponseCode() == 200) {
				rs = getWebSource(connection.getInputStream());
			}
		}
		catch (Exception e) {
			rs = null;
		}
		
		return rs;
	}
	finally {
		try {
			outputStream.close();
		}
		catch (Exception e) {
		}
		outputStream = null;
		
		if(connection != null)
			connection.disconnect();
		connection = null;
	}
}

提交参数有一下注意:

1、Content-Type参数必须写明。格式为:multipart/form-data; boundary=----footfoodapplicationrequestnetwork,前面四条短线后面自定义字符串,只要不与其他字符串冲突就行。

2、form-data参数格式。

buffer.append("------footfoodapplicationrequestnetwork\r\n");
buffer.append("Content-Disposition: form-data; name=\"");
buffer.append(parameter.getKey(i));
buffer.append("\"\r\n\r\n");
buffer.append(parameter.getValue(i));
buffer.append("\r\n");

参数说明:

第一行为form-data的请求头,也就是boundary前面再加两个短线,貌似必须要加上\r\n。

第二行为form-data的参数名。

3、form-data结束标志。格式为:------footfoodapplicationrequestnetwork--\r\n

在boundary前加两个短线,在后加两个短线和\r\n。

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,针对你的问题,我可以给你提供一些思路和代码示例。 首先,你需要使用`HttpURLConnection`来实现文件上传。以下是一个上传文件的示例代码: ```java URL url = new URL(uploadUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); outputStream.writeBytes("--" + boundary + "\r\n"); outputStream.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + fileName + "\"" + "\r\n"); outputStream.writeBytes("Content-Type: " + mimeType + "\r\n"); outputStream.writeBytes("\r\n"); outputStream.write(fileBytes); outputStream.writeBytes("\r\n"); outputStream.writeBytes("--" + boundary + "--" + "\r\n"); outputStream.flush(); outputStream.close(); ``` 在上面的代码中,`uploadUrl`是上传文件的接口地址,`boundary`是分隔符,`fileName`是文件名,`mimeType`是文件类型,`fileBytes`是文件的二进制数据。 接下来,你需要在Spring MVC中编写文件上传接口。以下是一个接收文件的示例代码: ```java @RequestMapping(value = "/upload", method = RequestMethod.POST) public ModelAndView upload(@RequestParam("file") MultipartFile file) throws IOException { String fileName = file.getOriginalFilename(); byte[] fileBytes = file.getBytes(); // do something with the file bytes return new ModelAndView("upload_success"); } ``` 在上面的代码中,`@RequestParam("file")`注解表示接收名为`file`的文件参数,并且使用`MultipartFile`类型来接收文件数据。`getOriginalFilename()`方法获取文件名,`getBytes()`方法获取文件二进制数据。 希望以上代码能够帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值