日期:2014-05-17 浏览次数:20812 次
public static String beginConnection(String url) throws Exception{ String str = null; HttpURLConnection conn = (HttpURLConnection) new URL(url) .openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setInstanceFollowRedirects(false); conn.connect(); conn.getInputStream(); str = conn.getHeaderField("Set-Cookie"); if(str != null){ String[] session = str.split(";"); str = session[0].substring(session[0].indexOf("=") + 1); } System.out.println("sessionID-->" + str); return str; }
public static String getProgress(String server, String sessionID) { String line = ""; try { URL url = new URL(server); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Cookie", "JSESSIONID=" + sessionID); DataOutputStream out = new DataOutputStream(conn.getOutputStream()); out.write(("folder=").getBytes("UTF-8")); out.flush(); out.close(); BufferedReader reader = new BufferedReader(new InputStreamReader( conn.getInputStream(), "UTF-8")); StringBuffer buffer = new StringBuffer(); while ((line = reader.readLine()) != null) { buffer.append(line); } reader.close(); return buffer.toString(); } catch (Exception e) { e.printStackTrace(); return null; } }
public static String upLoadFile(String server, File file, String sessionID, String folder) { System.out.println("upload sessionid-"+sessionID); String BOUNDARY = "---------7d4a6d158c954336agamacomcn"; // 定义数据分隔线 try { URL url = new URL(server); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Cookie", "JSESSIONID=" + sessionID); conn.setRequestProperty("Content-Type","multipart/form-data; boundary=" + BOUNDARY); conn.setChunkedStreamingMode(BUFFER_SIZE); 。。