public void doPost( HttpServletRequest request, HttpServletResponse response )
{
DiskFileUpload diskFileUpload = new DiskFileUpload(); // 允许文件最大长度
diskFileUpload.setSizeMax( 100*1024*1024 ); // 设置内存缓冲大小
diskFileUpload.setSizeThreshold( 4096 ); // 设置临时目录
diskFileUpload.setRepositoryPath( "c:/tmp" );
List fileItems = diskFileUpload.parseRequest( request );
Iterator iter = fileItems.iterator(); for( ; iter.hasNext(); )
{
FileItem fileItem = (FileItem) iter.next();
if( fileItem.isFormField() ) { // 当前是一个表单项
out.println( "form field : " + fileItem.getFieldName() + ", " + fileItem.getString() );
} else {
// 当前是一个上传的文件
String fileName = fileItem.getName();
fileItem.write( new File("c:/uploads/"+fileName) );
}
}}
POST /upload_file/UploadFile HTTP/1.1
Accept: text/plain, */*
Accept-Language: zh-cn
Host: 192.168.29.65:80
Content-Type:multipart/form-data;boundary=---------------------------7d33a816d302b6
User-Agent: Mozilla/4.0 (compatible; OpenOffice.org)
Content-Length: 424
Connection: Keep-Alive -----------------------------7d33a816d302b6
Content-Disposition:form-data;
name="userfile1";
filename="E:/s"Content-Type:
application/octet-stream abbXXXccc
-----------------------------7d33a816d302b6
Content-Disposition: form-data;
name="text1" foo
-----------------------------7d33a816d302b6
Content-Disposition: form-data;
name="password1" bar
-----------------------------7d33a816d302b6--
POST http://127.0.0.1/login.do HTTP/1.0
Accept: image/gif, image/jpeg, image/pjpeg, */*
Accept-Language: en-us,zh-cn;q=0.5
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Content-Length: 28
/r/n
username=admin&password=1234
hc.setRequestMethod(HttpConnection.POST);
byte[] data = "username=admin&password=1234".getBytes();
并计算正文长度,填入Content-Type和Content-Length:
hc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
hc.setRequestProperty("Content-Length", String.valueOf(data.length));
然后打开OutputStream将正文写入:
OutputStream output = hc.openOutputStream();
output.write(data);
hc.setRequestProperty("Content-Type", "multipart/form-data; boundary=ABCD");
--ABCD
Content-Disposition: form-data; name="title"
/r/n
Today
--ABCD
Content-Disposition: form-data; name="1.txt"; filename="C:/1.txt"
Content-Type: text/plain
/r/n
<这里是1.txt文件的内容>
--ABCD--
/r/n
String[] props = ... // 字段名
String[] values = ... // 字段值
byte[] file = ... // 文件内容
String BOUNDARY = "---------------------------7d4a6d158c9"; // 分隔符
StringBuffer sb = new StringBuffer();
// 发送每个字段:
for(int i=0; i
sb = sb.append("--");
sb = sb.append(BOUNDARY);
sb = sb.append("/r/n");
sb = sb.append("Content-Disposition: form-data; name=/""+ props[i] + "/"/r/n/r/n");
sb = sb.append(URLEncoder.encode(values[i]));
sb = sb.append("/r/n");
}
// 发送文件:
sb = sb.append("--");
sb = sb.append(BOUNDARY);
sb = sb.append("/r/n");
sb = sb.append("Content-Disposition: form-data; name=/"1/"; filename=/"1.txt/"/r/n");
sb = sb.append("Content-Type: application/octet-stream/r/n/r/n");
byte[] data = sb.toString().getBytes();
byte[] end_data = ("/r/n--" + BOUNDARY + "--/r/n").getBytes();
// 设置HTTP头:
hc.setRequestProperty("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY);
hc.setRequestProperty("Content-Length", String.valueOf(data.length + file.length + end_data.length));
// 输出:
output = hc.openOutputStream();
output.write(data);
output.write(file);
output.write(end_data);
// 读取服务器响应:
// TODO...