参考资料:
http://lapulande.iteye.com/blog/719581
http://my.oschina.net/gavinjin/blog/48897?p=1
public static String PostFile(String url_post, String filename, String RA_O,String RA_OU,String charset, boolean b_flag, String cookies) throws IOException { String BOUNDARY = "---------------------------27104325129557"; // 定义数据分隔线 String fname = filename URL postUrl = new URL(url_post); HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection(); if ("" != cookies) { connection.setRequestProperty("Cookie", cookies); println("set cookies = [" + cookies + "]") } connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); connection.setRequestMethod("POST"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); connection.setRequestProperty("Charsert", "UTF-8"); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); connection.connect(); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();// 定义最后数据分隔线 // <input type="hidden" name="O" id="O" value="TopCA"/> // <input type="hidden" name="OU" id="OU" value="RA_2k2_0_"/> // <input type="hidden" value="findCert" name="actionType"/> File file = new File(fname); StringBuilder sb = new StringBuilder(); sb.append("--" + BOUNDARY + "\r\n"); sb.append("Content-Disposition: form-data;name=\"certType" + "\"\r\n\r\n"); sb.append("load\r\n"); sb.append("--" + BOUNDARY + "\r\n"); sb.append("Content-Disposition: form-data;name=\"O" + "\"\r\n\r\n"); sb.append(RA_O +"\r\n"); sb.append("--" + BOUNDARY + "\r\n"); sb.append("Content-Disposition: form-data;name=\"OU" + "\"\r\n\r\n"); sb.append(RA_OU+"\r\n"); if(true){ sb.append("--" + BOUNDARY + "\r\n"); sb.append("Content-Disposition: form-data;name=\"file1" + "\";filename=\"\"\r\n"); sb.append("Content-Type:application/octet-stream\r\n\r\n\r\n"); sb.append("--" + BOUNDARY + "\r\n"); sb.append("Content-Disposition: form-data;name=\"file2" + "\";filename=\"\"\r\n"); sb.append("Content-Type:application/octet-stream\r\n\r\n\r\n"); sb.append("--" + BOUNDARY + "\r\n"); sb.append("Content-Disposition: form-data;name=\"file3" + "\";filename=\"\"\r\n"); sb.append("Content-Type:application/octet-stream\r\n\r\n\r\n"); sb.append("--" + BOUNDARY + "\r\n"); sb.append("Content-Disposition: form-data;name=\"file4" + "\";filename=\"\"\r\n"); sb.append("Content-Type:application/octet-stream\r\n\r\n\r\n"); } sb.append("--" + BOUNDARY + "\r\n"); sb.append("Content-Disposition: form-data;name=\"file5" + "\";filename=\"" + file.getName() + "\"\r\n"); sb.append("Content-Type:application/octet-stream\r\n\r\n"); byte[] data = sb.toString().getBytes(); out.write(data); // println("sb = [" + new String(sb) + "]" ) DataInputStream filein = new DataInputStream(new FileInputStream(file)); int bytes = 0; byte[] bufferOut = new byte[1024]; while ((bytes = filein.read(bufferOut)) != -1) { out.write(bufferOut, 0, bytes); } // out.write("\r\n".getBytes()); //多个文件时,二个文件之间加入这个 filein.close(); // DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写道流里面 String body=new String(end_data) // println("body = [" + body + "]") out.writeBytes(body); out.flush(); out.close(); // flush and close BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream(), charset)); if(false){ String line; System.out.println("Contents of post request:"); while ((line = reader.readLine()) != null) { System.out.println(line); } println(" ") } reader.close(); connection.disconnect(); return cookieVal }
使用方法:
String cookie_login = MyHttpUrlConn.Post(URL_LOGIN,"userName=admin&password=12345678","utf-8",false,"");
MyHttpUrlConn.PostFile(Post_upload,USER_FILE_PATH,"mxb","6","utf-8",false,cookie_login);