title: How to send file from php client to java server with sockets
body: <p>I want to transfer socket file for java server from php client.</p>
  
  <p>Java from java is good job.</p>
  
  <p>But php can not transfer file to java server.</p>
  
  <p>I need to convert to php code from java code or 
  I would like to know to transfer socket file for java server from php client by other method.
  Please help me...
  Thank you!!!</p>
  
  <ol>
  <li><p>Java Client Code.</p>
  
  <pre><code>File file = new File(fileName);
  Socket socket = new Socket("localhost", 20225);
  ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
  ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
  
  oos.writeObject(file.getName());
  
  FileInputStream fis = new FileInputStream(file);
  byte [] buffer = new byte[100];
  Integer bytesRead = 0;
  
  while ((bytesRead = fis.read(buffer)) &gt; 0) {
      oos.writeObject(bytesRead);
      oos.writeObject(Arrays.copyOf(buffer, buffer.length));
  }
  
  oos.close();
  ois.close();
  </code></pre></li>
  <li><p>Java Server Code</p>
  
  <pre><code>ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
  ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
  FileOutputStream fos = null;
  byte [] buffer = new byte[BUFFER_SIZE];
  String filename = null;
  Map&lt;String, Object&gt; retMap = new HashMap&lt;String, Object&gt;();
  
  // 1. Read file name.
  Object obj = ois.readObject();
  
  try {
  
      filename = Config.getInstance().getProperties("dispatcher.policy") + obj;
      fos = new FileOutputStream(new File(filename));   
  
      // 2. Read file to the end.
      Integer bytesRead = 0;
      do {
          obj = ois.readObject();
  
          bytesRead = (Integer)obj;
          obj = ois.readObject();
          buffer = (byte[])obj;
  
          // 3. Write data to output file.
          fos.write(buffer, 0, bytesRead);
  
      } while (bytesRead == BUFFER_SIZE);
  
      logger.info("File transfer success : {} ", filename );
  
  
      fos.close();
  
  }catch(Exception e){
  
  }
  oos.writeObject("OK");
  ois.close();
  oos.close();
  </code></pre></li>
  </ol>