title: How to upload .mp3 file and image to http server?
body: <p>My code for uploading image to server is :</p>
  <pre><code>String userIdParameter = String.valueOf(userId);
      String fileName = &quot;temporary_holder.jpg&quot;;
      HttpURLConnection conn = null;
      DataOutputStream dos = null;
  
      String lineEnd = &quot;\r\n&quot;;
      String twoHyphens = &quot;--&quot;;
      String boundary = &quot;*****&quot;;
      int bytesRead, bytesAvailable, bufferSize;
      byte[] buffer;
      int maxBufferSize = 1 * 1024 * 1024;
  
      String sourceFileUri = HomeScreen.get_path();
      String upLoadServerUri = &quot;http://10.120.10.87:8080/WebImage/UploadImage&quot;;
  
      File sourceFile = new File(sourceFileUri);
      if (!sourceFile.isFile()) {
          Log.e(&quot;Huzza&quot;, &quot;Source File Does not exist&quot;);
          return;
      }
      int serverResponseCode = 0;
      try {
  
          // open a URL connection to the Servlet
          FileInputStream fileInputStream = new FileInputStream(sourceFile);
  
          // ------------------ CLIENT REQUEST
          URL url = new URL(upLoadServerUri);
  
          // Open a HTTP connection to the URL
          conn = (HttpURLConnection) url.openConnection();
          conn.setDoInput(true); // Allow Inputs
          conn.setDoOutput(true); // Allow Outputs
          conn.setUseCaches(false); // Don't use a Cached Copy
  
          // Use a post method.
          conn.setRequestMethod(&quot;POST&quot;);
          conn.setRequestProperty(&quot;Connection&quot;, &quot;Keep-Alive&quot;);
  
          conn.setRequestProperty(&quot;ENCTYPE&quot;, &quot;multipart/form-data&quot;);
          conn.setRequestProperty(&quot;Content-Type&quot;,
              &quot;multipart/form-data;boundary=&quot; + boundary);
          conn.setRequestProperty(&quot;file_name&quot;, fileName);
          conn.setRequestProperty(&quot;file_name_audio&quot;, fileName);
          conn.setRequestProperty(&quot;X-myapp-param1&quot;, userIdParameter);
  
          // conn.setFixedLengthStreamingMode(1024);
          // conn.setChunkedStreamingMode(1);
  
          dos = new DataOutputStream(conn.getOutputStream());
  
          dos.writeBytes(twoHyphens + boundary + lineEnd);
  
          dos.writeBytes(&quot;Content-Disposition: form-data; name=\&quot;file_name\&quot;;filename=\&quot;&quot;
              + fileName + &quot;\&quot;&quot; + lineEnd);
  
          dos.writeBytes(lineEnd);
          
          
              // create a buffer of maximum size
          bytesAvailable = fileInputStream.available();
  
          int streamSize = (int) sourceFile.length();
          bufferSize = streamSize / 10;
  
          System.out.println(&quot;streamSize&quot; + streamSize);
  
          buffer = new byte[streamSize];
  
          // read file and write it into form...
          bytesRead = fileInputStream.read(buffer, 0, bufferSize);
          int count = 0;
          while (bytesRead &gt; 0) {
          progress = (int) (count);
          displayNotification();
          Thread.sleep(500);
          
          dos.write(buffer, 0, bufferSize);
          bytesAvailable = fileInputStream.available();
          // bufferSize = Math.min(bytesAvailable, maxBufferSize);
          bytesRead = fileInputStream.read(buffer, 0, bufferSize);
          count += 10;
  
          }
  
          // send multipart form data necesssary after file data...
          dos.writeBytes(lineEnd);
          dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  
          // Responses from the server (code and message)
          serverResponseCode = conn.getResponseCode();
          String serverResponseMessage = conn.getResponseMessage();
  
          System.out.println(&quot;Upload file to serverHTTP Response is : &quot;
              + serverResponseMessage + &quot;: &quot; + serverResponseCode);
          // close streams
          System.out.println(&quot;Upload file to server&quot; + fileName
              + &quot; File is written&quot;);
          fileInputStream.close();
          dos.flush();
          dos.close();
      } catch (MalformedURLException ex) {
          ex.printStackTrace();
          Log.e(&quot;Upload file to server&quot;, &quot;error: &quot; + ex.getMessage(), ex);
      } catch (Exception e) {
          e.printStackTrace();
      }
      // this block will give the response of upload link
      try {
          BufferedReader rd = new BufferedReader(new InputStreamReader(
              conn.getInputStream()));
          String line;
          while ((line = rd.readLine()) != null) {
          System.out.println(&quot;RESULT Message: &quot; + line);
          }
          rd.close();
      } catch (IOException ioex) {
          Log.e(&quot;Huzza&quot;, &quot;error: &quot; + ioex.getMessage(), ioex);
      }
      return; // like 200 (Ok)
  
      
  </code></pre>
  <p>Uploading image to server works fine. I need to upload both mp3 file and image to the server.</p>