模拟客户端IE上传文件


  URL preUrl = null;
  URLConnection uc = null;
  try {
   preUrl = new URL(http://127.0.0.1/seeyon/fileUpload+"&senderLoginName="+"zy");
   uc = preUrl.openConnection();
   HttpURLConnection hc = (HttpURLConnection) uc;
   hc.setDoOutput(true);
   hc.setUseCaches(false);
   hc.setRequestProperty("contentType", "charset=utf-8"); 
   hc.setRequestMethod("POST");
   BufferedInputStream  input=new BufferedInputStream(new FileInputStream("c:/test.doc"));
   String BOUNDARY = "---------------------------7d4a6d158c9"; // 分隔符
   String fileName="test.doc";
   StringBuffer sb = new StringBuffer();
   sb.append("--");
   sb.append(BOUNDARY);
   sb.append("\r\n");
   sb.append("Content-Disposition: form-data; \r\n name=\"1\"; filename=\""+fileName+"\"\r\n");
   sb.append("Content-Type: application/msword\r\n\r\n");
   
   hc.setRequestProperty("Content-Type",
     "multipart/form-data;boundary=" + "---------------------------7d4a6d158c9");
   byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
   DataOutputStream dos = new DataOutputStream(hc.getOutputStream());
   dos.write(sb.toString().getBytes("utf-8"));   
   int cc=0;
   while((cc=input.read())!=-1)
   {
    dos.write(cc);
   }
   dos.write(end_data);
   dos.flush();
   dos.close();
   FileOutputStream file=new FileOutputStream("c:/1.txt");
   InputStream is = hc.getInputStream();
   int ch;
   while ((ch = is.read()) != -1) {
    file.write(ch);
   }
   if (is != null)
    is.close();
  }
  catch (MalformedURLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 

你可能感兴趣的:(C++,c,IE,C#,dos)