problema en la carga de datos de android

i need to upload audio file(or any File) in server. i have server and refer this code but as per my doubt it is code of PHP server uploading.but i need to do in so what is the changes to apply ?

one more thing is url liook like this :: http://xyz/MRESC/images/CustomizeActivity/193/ so its not store in Database it store in directory

Actualizar ::

package com.upload;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class HttpFileUploader extends Activity {
 /** Called when the activity is first created. */
 public void onCreate(Bundle savedInstanceState) {

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

     //String pathToOurFile = "/sdcard/audiometer/shanesh1599870.mp3";
     String pathToOurFile = "";
     //String urlServer = "http://asd/MRESC/images/CustomizeActivity/193/";

 public static int upLoad2Server(String sourceFileUri) {
      String upLoadServerUri = "http://xyz/MRESC/images/CustomizeActivity/193/";
      // String [] string = sourceFileUri;
      String fileName = sourceFileUri;

      HttpURLConnection conn = null;
      DataOutputStream dos = null;
      DataInputStream inStream = null;
      String lineEnd = "\r\n";
      String twoHyphens = "--";
      String boundary = "*****";
      int bytesRead, bytesAvailable, bufferSize;
      byte[] buffer;
      int maxBufferSize = 1 * 1024 * 1024;
      String responseFromServer = "";

      File sourceFile = new File(sourceFileUri);
      if (!sourceFile.isFile()) {
       return 0;
      int serverResponseCode = 0;
    try { // open a URL connection to the Servlet
       FileInputStream fileInputStream = new FileInputStream(sourceFile);
       URL url = new URL(upLoadServerUri);
       conn = (HttpURLConnection) url.openConnection(); // Open a HTTP  connection to  the URL
       conn.setDoInput(true); // Allow Inputs
       conn.setDoOutput(true); // Allow Outputs
       conn.setUseCaches(false); // Don't use a Cached Copy
       conn.setRequestProperty("Connection", "Keep-Alive");
       conn.setRequestProperty("ENCTYPE", "multipart/form-data");
       conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
       conn.setRequestProperty("uploaded_file", fileName);
       dos = new DataOutputStream(conn.getOutputStream());

       dos.writeBytes(twoHyphens + boundary + lineEnd);
       dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ fileName + "\"" + lineEnd);

       bytesAvailable = fileInputStream.available(); // create a buffer of  maximum size
       Log.i("Huzza", "Initial .available : " + bytesAvailable);

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

       // read file and write it into form...
       bytesRead =, 0, bufferSize);

       while (bytesRead > 0) {
        dos.write(buffer, 0, bufferSize);
         bytesAvailable = fileInputStream.available();
         bufferSize = Math.min(bytesAvailable, maxBufferSize);
        bytesRead =, 0, bufferSize);

       // send multipart form data necesssary after file data...
       dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

       // Responses from the server (code and message)
       serverResponseCode = conn.getResponseCode();
       String serverResponseMessage = conn.getResponseMessage();

       Log.i("Upload file to server", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);
       // close streams
       Log.i("Upload file to server", fileName + " File is written");
      } catch (MalformedURLException ex) {
       Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
      } catch (Exception e) {
    //this block will give the response of upload link
      try {
       BufferedReader rd = new BufferedReader(new InputStreamReader(conn
       String line;
       while ((line = rd.readLine()) != null) {
        Log.i("Huzza", "RES Message: " + line);
      } catch (IOException ioex) {
       Log.e("Huzza", "error: " + ioex.getMessage(), ioex);
      return serverResponseCode;  // like 200 (Ok)

     } // end upLoad2Server


Permission ::

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

preguntado el 08 de noviembre de 11 a las 12:11

Look at my edited answer. Hope this will help you. -

1 Respuestas

To upload a file on server I don't think so for asp .net and PHP server have a different code on android.

Just check your server side script. For android both are treated as a server (either a asp .net or PHP). 

I don't know PHP or asp .net but take a look at these examples,

  1. Cómo cargar un archivo usando la biblioteca Java HttpClient trabajando con PHP - problema extraño

  2. Carga de archivos de varias partes HTTP Post en Java ME

  3. Cargar imagen usando POST, php y Android

  4. Java Swing File upload with Php on the server


In these, there are some example for java swing or java ME but I think just use the logic for upload file from java and then take look at how they handle on server side with php script.

contestado el 23 de mayo de 17 a las 14:05

can you elaborate that how can i check server side script? - Nikunj Patel

one more thing is that i dont know what parameter should i pass in all setRequestProperty content. see my code. - Nikunj Patel

one more thing is url liook like this :: http://xyz/MRESC/images/CustomizeActivity/193/ so its not store in Database it store in directory. - Nikunj Patel

In my case I put image in MultipartEntity set as a filebody and then in URL give the server's php script's url path. - user370305

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.