明白了要做什么事情,接下来就是用java代码来构建这样的StringBuffer了。 下面给出代码


public static String postFileFromBitmap2(String urlstr, HashMap hmap) {   dout("postURLContent:" + urlstr);   String fileKey = "image0";   String rs = ""; 
 
 
String temp = "temp.jpg";


Bitmap2File(bitmap, temp, U.ctx_for_getResString); 
  String exsistingFileName = 
U.ctx_for_getResString.getFilesDir() + "/" + temp;     String lineEnd = "\r\n";    
String twoHyphens = "--"; 
 
  String boundary = 
"---------------------------11538186919912";   try {  
 
 
File f = new File(exsistingFileName);     FileInputStream fileInputStream = new FileInputStream(f);     if (!f.exists())      
return rs; 
 
 
 
URL url = new URL(urlstr); 
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();     conn.setDoInput(true);     conn.setDoOutput(true);     conn.setUseCaches(false);  
 
 
conn.setRequestMethod("POST");     conn.setRequestProperty("Connection", 
"Keep-Alive"); 
    conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);     //兼容嘀咕的用户账户密码信息必须通过 AUTH_HTTP 方式提供  
 
 
if(hmap.containsKey("AUTH_HTTP_ENCODING")){ 
    conn.setRequestProperty("Authorization", "Basic " + hmap.get("AUTH_HTTP_ENCODING"));    
 
} 
 
    DataOutputStream dos = new DataOutputStream(conn.getOutputStream());






dos.writeBytes(twoHyphens + boundary + lineEnd); 
          dos.writeBytes("Content-Disposition: form-data; 
name=\"content\""+ lineEnd); 
          dos.writeBytes(lineEnd + 
URLEncoder.encode(""+hmap.get("content"), "UTF-8")+ lineEnd);    
 
 
dos.writeBytes(twoHyphens + boundary + lineEnd); 
          dos.writeBytes("Content-Disposition: form-data; 
name=\"source\""+ lineEnd); 
          dos.writeBytes(lineEnd + 
URLEncoder.encode(""+hmap.get("source"), "UTF-8")+ lineEnd);    
 
 
dos.writeBytes(twoHyphens + boundary + lineEnd); 
          dos.writeBytes("Content-Disposition: form-data; 
name=\"uploadImg\""+ lineEnd); 
          dos.writeBytes(lineEnd + 
URLEncoder.encode("xiexiezhichi", "UTF-8")+ lineEnd);    
        dos.writeBytes(twoHyphens + boundary + lineEnd); 
    dos.writeBytes("Content-Disposition: 
form-data; name=\"" + fileKey + "\";filename=\"" + exsistingFileName+ "\"" + lineEnd); 
    dos.writeBytes("Content-Type: image/jpeg"+lineEnd);    
 
dos.writeBytes(lineEnd); 
 
    int bytesAvailable = fileInputStream.available();  
 
 
byte[] buffer = new byte[bytesAvailable];     int bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);     while (bytesRead > 0) {  
 
 
 
dos.write(buffer, 0, bytesAvailable); 
    bytesAvailable = fileInputStream.available(); 
    bytesRead = 
fileInputStream.read(buffer, 0, bytesAvailable);  
 
 
}



dos.writeBytes(lineEnd); 
    dos.writeBytes(boundary + lineEnd);     fileInputStream.close(); 
   
 
 
dos.writeBytes(twoHyphens + boundary + lineEnd); 
    dos.writeBytes("Content-Disposition: 
form-data; name=\"image1\";filename=\"\"" + lineEnd);     dos.writeBytes("Content-Type: application/octet-stream"+lineEnd);     dos.writeBytes(lineEnd); 
   
 
dos.writeBytes(twoHyphens + boundary + lineEnd); 
 
    dos.writeBytes("Content-Disposition: form-data; name=\"image2\";filename=\"\"" + lineEnd);     dos.writeBytes("Content-Type: application/octet-stream"+lineEnd);  
 
 
dos.writeBytes(lineEnd); 
    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);     dos.flush();    
 
dos.close();  
   
 
 
InputStream in = conn.getInputStream(); 
    BufferedReader br = new BufferedReader(new InputStreamReader(in));     StringBuffer sb2 = new StringBuffer();     String tmp = null; 
    while ((tmp = br.readLine()) != null) {     sb2.append(tmp); 
    } 
    br.close();     in.close(); 
    return sb2.toString();   } catch (IOException e) { 
    e.printStackTrace(); 
 
 
}