appache read and write stream

package com.cn.sevlet;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.UnknownServiceException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.content.Context;

public class ClientBuilder {
private static final String CHARSET = "utf-8";
@SuppressWarnings("null")
public static String request(Context context, String uri,
UnamParameters param, String httpMethod) throws UnsupportedEncodingException, IllegalStateException, IOException {
String result = null;
HttpClient client = new DefaultHttpClient();


if (httpMethod.equalsIgnoreCase("get")) {
BufferedReader reader = null;
HttpGet get = new HttpGet(uri);
HttpResponse response = client.execute(get);
reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), CHARSET));//地址

StringBuffer sb = new StringBuffer();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
result = sb.toString();
return result;
} 
else if (httpMethod.equalsIgnoreCase("post")) {
final String MULTIPART_FORM_DATA = "multipart/form-data";
final String BOUNDARY = "infovillage";//分隔符
final String TWO_HYPHENS = "--";
final String ENDLINE = System.getProperty("line.separator");

Map<String, String> table = new HashMap<String, String>();
table.put("product", param.getProduct());
table.put("price", param.getPrice());
table.put("unit", param.getUnit());
table.put("name", param.getName());
table.put("telephone", param.getTelephone());
table.put("address", param.getAddress());
table.put("description", param.getDescription());

StringBuffer sb = new StringBuffer();
Set<Map.Entry<String, String>> set = table.entrySet();
/*文件内容为二进制流的形式*/
for (Map.Entry<String, String> entry : set) {
sb.append(TWO_HYPHENS + BOUNDARY + ENDLINE);
sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + ENDLINE);
sb.append(ENDLINE);
sb.append(entry.getValue() + ENDLINE);
}
sb.append(TWO_HYPHENS + BOUNDARY + ENDLINE);
sb.append("Content-Disposition: form-data; name=\"picture\"; filename=\"uploadImage.jpg\"" + ENDLINE);
sb.append("Content-Type: image/jpeg" + ENDLINE);
sb.append(ENDLINE);
sb.append(param.getPicture());
sb.append(ENDLINE);
sb.append(TWO_HYPHENS + BOUNDARY + TWO_HYPHENS +ENDLINE);

HttpPost post = new HttpPost(uri);
post.addHeader("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY);
DataOutputStream output=null;
try{
output.writeBytes(sb.toString());
}catch(IOException e){
e.printStackTrace();
}
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
swapStream.writeTo(output); 
byte []bytes= swapStream.toByteArray();
InputStream in = new ByteArrayInputStream(bytes); 
output.flush();


post.setEntity(new InputStreamEntity(in, bytes.length));
// System.out.println(sb.toString());
HttpResponse response = client.execute(post);

BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer strbuf = new StringBuffer();
String line = null;
while((line = reader.readLine()) != null) {
strbuf.append(line);
}
result = strbuf.toString();
reader.close();
}
return result;
}
public static InputStream getFile(Context context, String uri,
UnamParameters param, String httpMethod)
throws UnknownServiceException {
InputStream is = null;
if (httpMethod.equalsIgnoreCase("GET")) {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(uri);
try {
HttpResponse response = client.execute(get);
is = response.getEntity().getContent();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return is;
}
 

猜你喜欢

转载自xiaxingwork.iteye.com/blog/1627595