package com.zzh.http.sender;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;

import com.zzh.http.HttpException;
import com.zzh.http.Request;
import com.zzh.http.Response;

public class FilePostSender extends PostSender {

	public static String SEPARATOR = "\r\n";
	
	public FilePostSender(Request request) {
		super(request);
	}
	
	@Override
	public Response send() throws HttpException {
		try {
			String boundary = "---------------------------[Nutz]7d91571440efc";
			openConnection();
			setupRequestHeader();
			conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);
			setupDoInputOutputFlag();
			Map<String, ?> params = request.getParams();
			DataOutputStream outs = new DataOutputStream(conn.getOutputStream());		
			if (null != params && params.size() > 0) {
				for (Iterator<String> it = params.keySet().iterator(); it.hasNext();) {
					outs.writeBytes("--" + boundary + SEPARATOR);
					String key = it.next();
					File f = new File(params.get(key).toString());
					if(f.exists()){
						outs.writeBytes("content-disposition:	form-data;	name=\""+ key + "\";	filename=\""+ params.get(key) +"\"\r\n\r\n");
				        InputStream is = new FileInputStream(f);  			        	
				        byte[] buffer = new byte[is.available()];
					    while(true){  
					    	synchronized(buffer){  
					    		int amountRead = is.read(buffer);  
					            if(amountRead == -1){  
					            	break;  
					            } 
					            outs.write(buffer,0,amountRead);
					            outs.writeBytes("\r\n");
					        }
					    }
				     is.close();

					}else{
						outs.writeBytes("content-disposition:	form-data;	name=\"" + key + "\"\r\n\r\n"); 
						outs.writeBytes(params.get(key) + "\r\n");
					}			
				}
				outs.writeBytes("--" + boundary + "--"+ SEPARATOR);
				outs.flush();
				outs.close();
			}		

			return createResponse(getResponseHeader());

		} catch (Exception e) {
			throw new HttpException(request.getUrl().toString(), e);
		}
	}


}

@@ -280,16 +280,52 @@
 esponse;
+%0Aimport com.zzh.http.Request.METHOD;
 %0A%0Apublic
@@ -1284,24 +1284,95 @@
 ey) +%22%5C%22%5Cr%5Cn
+%22);%0A%09%09%09%09%09%09outs.writeBytes(%22Content-Type:   application/octet-stream%5Cr%5Cn
 %5Cr%5Cn%22);%0A%09%09%09%09

Update FilePostSender.java.