这个程序需要传输采用HTTP multipart/form-data的方式,使用http专为文件上传建立的rfc1867协议(协议详细介绍http://www.ietf.org/rfc/rfc1867.txt)
package com.xxx.getguestfile.sample;import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;public class TestUpload {
public final static void main(String[] args) throws Exception {
// 使用http协议,按照rfc1867协议标准发送文件
File guest = new File("c:\\test.txt");   //旅客身份信息文本文件
File photo = new File("c:\\photo1.jpg"); //旅客照片 String BOUNDARY = "---------------------------198152288819156"; // 分隔符
// 旅客身份信息文本文件上传的http请求头
StringBuffer sbGuest = new StringBuffer();
sbGuest = sbGuest.append("--");
sbGuest = sbGuest.append(BOUNDARY);
sbGuest = sbGuest.append("\r\n");
sbGuest = sbGuest.append("Content-Disposition: form-data; name=\""
+ "guest" + "\"; filename=\"" + guest.getAbsolutePath()
+ "\"\r\n");
sbGuest = sbGuest.append("Content-Type: text/plain\r\n\r\n");
byte[] start_data_guest = sbGuest.toString().getBytes(); // 旅客照片上传的http请求头
StringBuffer sbPhoto = new StringBuffer();
sbPhoto = sbPhoto.append("--");
sbPhoto = sbPhoto.append(BOUNDARY);
sbPhoto = sbPhoto.append("\r\n");
sbPhoto = sbPhoto.append("Content-Disposition: form-data; name=\""
+ "photo" + "\"; filename=\"" + photo.getAbsolutePath()
+ "\"\r\n");
sbPhoto = sbPhoto
.append("Content-Type: image/jpg\r\n\r\n");
byte[] start_data_photo = sbPhoto.toString().getBytes(); // http请求结尾
byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes(); // java的http相关类库,建 立http链接
URL url = null;
HttpURLConnection connection = null;
url = new URL(
"http://xxx/getguestfile/service/acceptfile/upfile");  //上传文件处理服务地址
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");     //使用post发送 connection.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY);
connection.setRequestProperty("Content-Length", String
.valueOf(start_data_guest.length+start_data_photo.length + guest.length()+photo.length()
+ end_data.length*2)); //计算报文长度
connection.setUseCaches(false);
connection.connect(); // 发送参数
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
out.write(start_data_guest);
out.write(getFile(guest));  //文件的字节数组
out.write(end_data); out.write(start_data_photo);
out.write(getFile(photo));   
out.write(end_data); out.flush();
out.close();

if (connection.getResponseCode() == 200) { InputStream u = connection.getInputStream();// 获取http连接的返回内容
BufferedReader in = new BufferedReader(new InputStreamReader(u));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line);
}
System.out.println("返回值" + buffer.toString());  //返回ok表示上传成功
} else {
System.out.println("连接失败");
}

connection.disconnect();
} private static byte[] getFile(File file) throws Exception {
FileInputStream da = new FileInputStream(file);
byte[] content = new byte[(int) file.length()];
if (file.length() > 0) {
da.read(content);
}
return content; }
}