????java????url????????????????????struts2??action???????

????1??java????????????url??????????????

 /**
  *
  * @???? ??????
  * @???????? 2013-06-27
  * @??????? 19:28:18
  * @???? —— ??????????????????
  */
 private static void testSimulateFormToPostFile() {
  String localPath = "F:\???????????\DeleteNullDir.java";
  File file = null;// ???????
  URL url = null;// ??????action???
  StringBuffer sb_cookie = null;// ??cookies
  StringBuffer sb_body = null;// ??????
  HttpURLConnection httpUrl = null;// httpЭ????
  OutputStream fos = null;// ?????
  FileInputStream fis = null;// ????????д?????
  BufferedReader br = null;// ??????
  try {
   file = new File(localPath);
   if (!file.exists()) {
    throw new Exception();
   }
   String _url = http://10.49.61.101:9999/finance/ajaxUploadFile.do;
   // Cookie[] cs = request.getCookies();
   // sb_cookie = new StringBuffer();
   // for (Cookie c : cs) {
   // sb_cookie.append(" ");
   // sb_cookie.append(c.getName());
   // sb_cookie.append("=");
   // sb_cookie.append(c.getValue());
   // sb_cookie.append(";");
   // }
   // String cookie = sb_cookie.substring(0?? sb_cookie.length() - 1);//
   // cookie??????????";"
   String boundary = "---------------------------7da2e536604c8";
   url = new URL(_url);
   httpUrl = (HttpURLConnection) url.openConnection();// ????????
   httpUrl.setDoInput(true);// ??????????????????
   httpUrl.setDoOutput(true);// ?????????????????
   httpUrl.setUseCaches(false);// ??????
   httpUrl.setConnectTimeout(30000);// ??????
   httpUrl.setReadTimeout(30000);// ??????
   httpUrl.setRequestMethod("POST");
   httpUrl.setRequestProperty("Content-Length"?? "" + file.length());// ?????С
   httpUrl.addRequestProperty("Charset"?? "UTF-8");
   httpUrl.addRequestProperty("Content-Type"??
     "multipart/form-data;boundary=" + boundary);
   httpUrl.addRequestProperty("Connection"?? "Keep-Alive");// ??????????????????
   // httpUrl.addRequestProperty("Cookie"?? cookie);// ?????????
   fos = httpUrl.getOutputStream();
   // ???httpЭ?飬?????????????????????byte????
   sb_body = new StringBuffer();
   // ????
   sb_body.append("--");
   sb_body.append(boundary);
   sb_body.append(" ");
   // ???????
   sb_body.append("Content-Disposition: form-data;name="upFile";"
     + "filename="" + "upload_data.xlsx" + "" ");
   sb_body.append("Content-Type:application/ms-word ");
   byte[] head = sb_body.toString().getBytes();
   fos.write(head);
   // ???????
   fis = new FileInputStream(file);
   byte[] read = new byte[2048];
   int offset = 0;
   while ((offset = fis.read(read)) != -1) {
    fos.write(read?? 0?? offset);
   }
   fos.write((" --" + boundary + "-- ").getBytes());
   fos.flush();// ????????
   // HTTP???
   br = new BufferedReader(new InputStreamReader(httpUrl
     .getInputStream()));
   String line = null;
   StringBuffer sb = new StringBuffer();
   while ((line = br.readLine()) != null) {
    sb.append(line);
   }
   System.out.println(sb.toString());
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

????2??java?????????????url??????????????

 /**
  *
  * @???? ??????
  * @???????? 2013-06-27
  * @??????? 19:30:05
  * @???? —— ??????????post???????????????
  */
 private static void testPostInStream() {
  try {
   URL url = new URL(
     "http://10.49.61.101:9999/finance/ajaxUploadFileTwo.do?filePath=/c/v/b/&fileName=aaa.xlsx);
   // ????POST???????????????????
   HttpURLConnection conn = (HttpURLConnection) url.openConnection();

   conn.setDoOutput(true);
   conn.setUseCaches(false);
   conn.setRequestMethod("POST");
   conn.setRequestProperty("Content-Type"?? "text/html");
   conn.setRequestProperty("Cache-Control"?? "no-cache");
   conn.setRequestProperty("Charsert"?? "UTF-8");
   // conn.setRequestProperty("upFileFileName"?? "upFileFileName.doc");
   conn.connect();
   conn.setConnectTimeout(10000);

   OutputStream out = conn.getOutputStream();

   File file = new File("F:\???????????NumberFormateUtil.java>");

   DataInputStream in = new DataInputStream(new FileInputStream(file));

   int bytes = 0;
   byte[] buffer = new byte[1024];
   while ((bytes = in.read(buffer)) != -1) {
    out.write(buffer?? 0?? bytes);
   }
   in.close();
   out.flush();
   out.close();

   BufferedReader br = new BufferedReader(new InputStreamReader(conn
     .getInputStream()));
   String line = null;
   StringBuffer sb = new StringBuffer();
   while ((line = br.readLine()) != null) {
    sb.append(line);
   }
   System.out.println(sb.toString());
   conn.disconnect();
   System.out.println("over");
  } catch (Exception e) {
   System.out.println("???????????????" + e);
   e.printStackTrace();
  }
 }