0

i need to upload audio file(or any File) in server. i have asp.net server and refer this code but as per my doubt it is code of PHP server uploading.but i need to do in asp.net. 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

Update ::

package com.upload;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
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. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

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

     //String pathToOurFile = "/sdcard/audiometer/shanesh1599870.mp3";
     String pathToOurFile = "http://www.deviantart.com/download/78789749/Gohan_Jr__by_android_1.jpg";
     //String urlServer = "http://asd/MRESC/images/CustomizeActivity/193/";
     upLoad2Server(pathToOurFile);
 }



 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.setRequestMethod("POST");
       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);
       dos.writeBytes(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 = fileInputStream.read(buffer, 0, bufferSize);

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

       // 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();

       Log.i("Upload file to server", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);
       // close streams
       Log.i("Upload file to server", fileName + " File is written");
       fileInputStream.close();
       dos.flush();
       dos.close();
      } catch (MalformedURLException ex) {
       ex.printStackTrace();
       Log.e("Upload file to server", "error: " + 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) {
        Log.i("Huzza", "RES Message: " + line);
       }
       rd.close();
      } 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>
Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133

1 Answers1

0

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. How to upload a file using Java HttpClient library working with PHP - strange problem

  2. HTTP Post multipart file upload in Java ME

  3. Upload image using POST, php and Android

  4. Java Swing File upload with Php on the server

  5. http://www.tizag.com/phpT/fileupload.php

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.

Community
  • 1
  • 1
user370305
  • 108,599
  • 23
  • 164
  • 151