永久链接: http://gaojingsong.iteye.com/blog/2414484
预览文章: 【Http文件上传协议解析】
首先是请求头:
最重要的内容是Content-Type,他的内容中是:
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary6TAB8KxvuJTZYfUn
用分号隔开了不同的参数:
第一个参数multipart/form-data是必须的,是指提交的表单中有附件
第二个参数boundary表示分割线,请求体中会用来分割不同的请求参数
至于其他参数,在自己写请求的时候直接模仿传过去即可。
核心代码
package com.example.fileupload.controller;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Set;
public class HttpUrlConnectionDemo {
/**
* @param args
*/
public static void main(String[] args) {
// 上传文件测试
String actionURL ="http://localhost:9090/mgr/upload";
String uploadFile="c:/examples.cfg";
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("email", "demo@123.com");
singleFileUploadWithParameters( actionURL, uploadFile, parameters );
}
/**
* @author Johnson
* @method singleFileUploadWithParameters
* @description 集上传单个文件与传递参数于一体的方法
* @param actionURL 上传文件的URL地址包括URL
* @param fileType 文件类型(枚举类型)
* @param uploadFile 上传文件的路径字符串
* @return String("" if no response get)
* @attention 上传文件name为file(服务器解析)
* */
public static String singleFileUploadWithParameters(String actionURL, String uploadFile,
HashMap<String, String> parameters){
String end = "\r\n";
String twoHyphens = "--";
String boundary = "---------------------------7e0dd540448";
String response = "";
try{
URL url = new URL(actionURL);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
//发送post请求需要下面两行
connection.setDoInput(true);
connection.setDoOutput(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);
//获取请求内容输出流
DataOutputStream ds = new DataOutputStream(connection.getOutputStream());
String fileName = uploadFile.substring(uploadFile.lastIndexOf("/") + 1);
//开始写表单格式内容
//写参数
Set<String> keys = parameters.keySet();
for(String key : keys){
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; name=\"");
ds.write(key.getBytes());
ds.writeBytes("\"" + end);
ds.writeBytes(end);
ds.write(parameters.get(key).getBytes());
ds.writeBytes(end);
}
//写文件
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; " + "name=\"file1\"; " + "filename=\"");
//防止中文乱码
ds.write(fileName.getBytes());
ds.writeBytes("\"" + end);
ds.writeBytes("Content-Type: text/plain" + end);
ds.writeBytes(end);
//根据路径读取文件
FileInputStream fis = new FileInputStream(uploadFile);
byte[] buffer = new byte[1024];
int length = -1;
while((length = fis.read(buffer)) != -1){
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
fis.close();
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
ds.writeBytes(end);
ds.flush();
try{
//获取URL的响应
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
String s = "";
String temp = "";
while((temp = reader.readLine()) != null){
s += temp;
}
response = s;
reader.close();
}catch(IOException e){
e.printStackTrace();
System.out.println("No response get!!!");
}
ds.close();
}catch(IOException e){
e.printStackTrace();
System.out.println("Request failed!");
}
return response;
}
}
结果验证:
相关推荐
主要介绍了JAVA通过HttpURLConnection 上传和下载文件的方法,非常具有实用价值,需要的朋友可以参考下
HttpURLConnection文件下载\httpURLConnection文件下载
基于HttpURLConnection的封装http请求,进行了https校验忽略,对get和post进行了整合,更方便请求,对header以及超时的进行了可配置化处理,对返回也进行了封装,便于请求结果的处理。
Java自带的HttpURLConnection访问接口文件上传, 含普通参数值传递和文件传递
主要介绍了java后台调用HttpURLConnection类模拟浏览器请求实例,该实例可用于接口调用,具有一定的实用价值,需要的朋友可以参考下
使用HttpUrlConnection实现上传文件服务器端代码,引用commons-fileupload-1.2.1.jar、commons-io-1.4.jar
本示例使用HttpUrlConnection实现上传文件,可以将手机上的文件上传到服务器 必须与my_upload工程配合运行,先启动my_upload工程
本文通过实例代码给大家介绍了Android基于HttpUrlConnection类的文件下载功能,非常不错,具有参考借鉴价值,需要的的朋友参考下吧
Java利用HttpURLConnection发送post请求上传文件
基于HttpURLConnection的网络访问 已封装好,一句话访问网络 可采用GET和POST两种方式访问
一个基于Java原生的Http客户端。HttpNet网络请求框架基于HttpUrlConnection
HttpURLConnection调用三方HTTP接口,带有FILE文件参数
NULL 博文链接:https://dreamoftch.iteye.com/blog/1860289
HttpUrlConnection对象模拟POST带JSON字符串的AJAX请求失败! 我的目的是抓取响应结果字符串"301",但抓取的结果是一堆HTML代码,哪位大神看看哪有毛病?
HttpURLConnection实现三方接口HTTP调用调用,带有FILE文件参数
主要介绍了Golang+Android基于HttpURLConnection实现的文件上传功能,结合具体实例形式分析了Android基于HttpURLConnection的客户端结合Go语言服务器端实现文件上传功能的操作技巧,需要的朋友可以参考下
通过HttpURLConnection把本地目录上的资源打包远程到另一台服务器上,有通过过滤器启动Timer,有手工上传的。
HttpUrlConnection下载文件
基于HttpURLConnection的网络访问 已封装好,一句话访问网络 可采用GET和POST两种方式访问
tomcat与web程序结构与Http协议与HttpUrlConnection.pdf