Upload a file using HTTP put in Java
<p>I am writing a desktop app in JAVA for upload a file to a folder on IIS server using HTTP PUT. </p>

<pre><code>URLConnection urlconnection=null;
  try{
   File file = new File("C:/test.txt");
   URL url = new URL("http://192.168.5.27/Test/test.txt");
   urlconnection = url.openConnection();
   urlconnection.setDoOutput(true);
   urlconnection.setDoInput(true);

   if (urlconnection instanceof HttpURLConnection) {
    try {
     ((HttpURLConnection)urlconnection).setRequestMethod("PUT");
     ((HttpURLConnection)urlconnection).setRequestProperty("Content-type", "text/html");
     ((HttpURLConnection)urlconnection).connect();


    } catch (ProtocolException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }


   BufferedOutputStream bos = new BufferedOutputStream(urlconnection
     .getOutputStream());
   BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
     file));
    int i;
    // read byte by byte until end of stream
    while ((i = bis.read()) &gt;0) {
     bos.write(i);
    }
   System.out.println(((HttpURLConnection)urlconnection).getResponseMessage());
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
  try {

   InputStream inputStream;
   int responseCode=((HttpURLConnection)urlconnection).getResponseCode();
   if ((responseCode&gt;= 200) &amp;&amp;(responseCode&lt;=202) ) {
    inputStream = ((HttpURLConnection)urlconnection).getInputStream();
    int j;
    while ((j = inputStream.read()) &gt;0) {
     System.out.println(j);
    }

   } else {
    inputStream = ((HttpURLConnection)urlconnection).getErrorStream();
   }
   ((HttpURLConnection)urlconnection).disconnect();

  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
</code></pre>

<p>This program creates an empty file on the destination folder(Test).The contents are not written to the file.</p>

<p>What is wrong with this program?</p>

<p>Thanks in Advance.</p>
