Multipart/form-data方式でtextやファイルをアップロードし、マイクロブログの写真のように


/**
*actionUrl--- 
*params---text 
*files---- 
*username--- 
*passwd--- 
*/
public static String postFile(String actionUrl, Map<String, String> params, Map<String, File> files,
			String username,String passwd) throws IOException
    {
//		String root = Environment.getExternalStorageDirectory().toString();
//		File file1 = new File(root+"/test.txt");
//		file1.createNewFile();
//		FileOutputStream fos = new FileOutputStream(file1,true);
        String BOUNDARY = "---------------------------" + System.currentTimeMillis();// 
        String PREFIX = "--"; // 
    	String LINEND = "\r
"; // String MULTIPART_FROM_DATA = "multipart/form-data";// String CHARSET = "UTF-8";// URL uri = new URL(actionUrl); HttpURLConnection conn = (HttpURLConnection) uri.openConnection(); conn.setReadTimeout(5 * 1000); // conn.setDoInput(true);// conn.setDoOutput(true);// conn.setUseCaches(false); // // conn.setRequestProperty("Authorization", "Basic " + Base64.encodeBytes((username + ":" + passwd).getBytes()));// conn.setRequestMethod("POST"); conn.setRequestProperty("connection", "keep-alive"); conn.setRequestProperty("Charsert", "UTF-8"); conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY); // StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : params.entrySet()) { sb.append(PREFIX); sb.append(BOUNDARY); sb.append(LINEND); sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINEND); sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND); sb.append("Content-Transfer-Encoding: 8bit" + LINEND); sb.append(LINEND); sb.append(entry.getValue()); sb.append(LINEND); } DataOutputStream outStream = new DataOutputStream(conn.getOutputStream()); outStream.write(sb.toString().getBytes()); // fos.write(sb.toString().getBytes()); InputStream in = null; // if (files != null) { for (Map.Entry<String, File> file : files.entrySet()) { StringBuilder sb1 = new StringBuilder(); sb1.append(PREFIX); sb1.append(BOUNDARY); sb1.append(LINEND); // name post filename sb1.append("Content-Disposition: form-data; name=\""+file.getKey()+"\"; filename=\"" + file.getValue() + "\"" + LINEND); sb1.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINEND); sb1.append(LINEND); outStream.write(sb1.toString().getBytes()); // fos.write(sb1.toString().getBytes()); InputStream is = new FileInputStream(file.getValue()); // byte[] buffer = new byte[1024]; // int len = 0; // while ((len = is.read(buffer)) != -1) // { // outStream.write(buffer, 0, len); //// fos.write(buffer, 0, len); // } int bytesAvailable; while ((bytesAvailable = is.available()) > 0) { int bufferSize = Math.min(bytesAvailable, 4096); byte[] buffer = new byte[bufferSize]; int bytesRead = is.read(buffer, 0, bufferSize); outStream.write(buffer, 0, bytesRead); } is.close(); outStream.write(LINEND.getBytes()); // fos.write(LINEND.getBytes()); } // } byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes(); outStream.write(end_data); // fos.write(end_data); outStream.flush(); // fos.flush(); // fos.close(); outStream.close(); // StringBuilder sb2 = null; int res = conn.getResponseCode(); if (res == 200) { in = conn.getInputStream(); int ch; sb2 = new StringBuilder(); while ((ch = in.read()) != -1) { sb2.append((char) ch); } // Util.print("sb222-->"+sb2); } else{ return "error"; } // outStream.close(); conn.disconnect(); return sb2.toString(); }