upload images to php linux server
<p>I want to upload images to a server. I used <a href="http://katharnavas.blogspot.com/2009/10/upload-files-from-mobile.html" rel="nofollow">http://katharnavas.blogspot.com/2009/10/upload-files-from-mobile.html</a> code and other methods, but phone sometimes fail, and analog machine always run well.  Can you help me to analyze the cause of the error and whether I need some setting?  Thank you.</p>

<pre><code>private void upload3()
{

  HttpURLConnection connection = null;
  DataOutputStream outputStream = null;
  DataInputStream inputStream = null;

  String pathToOurFile = fileName;
  Log.e("pathToOurFile",""+pathToOurFile);

  String urlServer = urlStr;
  String lineEnd = "\r\n";
  String twoHyphens = "--";
  String boundary =  "*****";
  Log.e("URL Server",""+urlServer);

  int bytesRead, bytesAvailable, bufferSize;
  byte[] buffer;
  int maxBufferSize = 1*1024*1024;
  Log.e("maxBufferSize",""+maxBufferSize);

  try
  {
      FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );
      Log.e("FIS",""+fileInputStream);
      URL url = new URL(urlServer);
      connection = (HttpURLConnection) url.openConnection();



//  Allow Inputs &amp; Outputs
          connection.setDoInput(true);
          connection.setDoOutput(true);
          connection.setUseCaches(false);

      // Enable POST method
      connection.setRequestMethod("POST");

      connection.setRequestProperty("Connection", "Keep-Alive");
      connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

      outputStream = new DataOutputStream( connection.getOutputStream() );
      outputStream.writeBytes(twoHyphens + boundary + lineEnd);
      outputStream.writeBytes("Content-Disposition: form-data; name=\"fileToUpload\";filename=\"" + pathToOurFile +"\"" + lineEnd);
      outputStream.writeBytes(lineEnd);

      bytesAvailable = fileInputStream.available();
      bufferSize = Math.min(bytesAvailable, maxBufferSize);
      buffer = new byte[bufferSize];

      // Read file
      bytesRead = fileInputStream.read(buffer, 0, bufferSize);

      while (bytesRead &gt; 0)
      {
          outputStream.write(buffer, 0, bufferSize);
          bytesAvailable = fileInputStream.available();
          bufferSize = Math.min(bytesAvailable, maxBufferSize);
          bytesRead = fileInputStream.read(buffer, 0, bufferSize);
      }

      outputStream.writeBytes(lineEnd);
      outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

      // Responses from the server (code and message)
      Log.e("con",String.valueOf(connection.getDoOutput()));


      String   serverResponseMessage = connection.getResponseMessage();

      Log.e("serverResponseMessage",""+serverResponseMessage);
      InputStream is = connection.getInputStream();

        int ch=-1;
         StringBuffer b = new StringBuffer();
        while ((ch = is.read()) != -1) {
            b.append((char) ch);
        }
        Log.d("log", "返回数据-"+b.toString().trim());
      fileInputStream.close();
      outputStream.flush();
      outputStream.close();
  }
  catch (Exception ex)
  {
      Log.e("Exception Handling",""+ex);
  }
}
</code></pre>
