Upload Files to Web Server

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Postby Katharnavas » Wed Jan 09, 2008 8:34 am

rmeph wrote:hi all,
any one tel me a simple file in android how to upload on server............
my file in
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. File f = new File(aDirectory.getAbsolutePath());
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

that upload on server using http protocal.............


Did nt you try the code above just pass your file parameter in place of new File()
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Top

Postby venkat » Wed Jan 09, 2008 8:54 am

Dear Katharnavas, :)
Thank you. i will check it today and i will inform to you if I have any doubt.


Thanks and Regards, :)
Venkat.
Regards,
Venkat.
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Postby rmeph » Wed Jan 09, 2008 1:26 pm

ok..actually i want upload file in android to server without using server side scripting.......is it possible ???how???
rmeph
Senior Developer
Senior Developer
 
Posts: 121
Joined: Mon Dec 10, 2007 1:54 pm
Location: India

Postby Katharnavas » Wed Jan 09, 2008 2:02 pm

rmeph wrote:ok..actually i want upload file in android to server without using server side scripting.......is it possible ???how???


Hope you could find some.. I do remember something related to that while i was making a search for file upload but could not find it again.. It was using a ftp server to upload files.
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby rmeph » Thu Jan 10, 2008 8:04 am

i want .txt file upload on server
I use client side coding

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                         DataOutputStream dos;
  3.  
  4.                         URLConnection con;
  5.  
  6.                         String urlString = new String("http://localhost:8080/webdav/ee/");
  7.  
  8.                         URL url = new URL(urlString);
  9.  
  10.                         con=url.openConnection();
  11.  
  12.                        
  13.  
  14.                         if (con instanceof HttpURLConnection) {
  15.  
  16.                                         System.out.println("This is HttpURLConnection");
  17.  
  18.                                         ((HttpURLConnection)con).setRequestMethod("POST");
  19.  
  20.                                 }
  21.  
  22.                          con.setDoInput(true);
  23.  
  24.                          Log.i(TAG,"setDoInputsetDoInput");
  25.  
  26.                          con.setDoOutput(true);
  27.  
  28.                          Log.i(TAG,"setDoOutputsetDoOutputsetDoOutput");
  29.  
  30.                          con.setRequestProperty("Content-type", "multipart/form-data");
  31.  
  32.                          Log.i(TAG,"after set requestProperty");
  33.  
  34.                           dos = new DataOutputStream(con.getOutputStream());
  35.  
  36.                          Log.i(TAG,"after DataOutputStream ");
  37.  
  38.                          URL myURL = new URL("sample.txt");
  39.  
  40.                          InputStream bis = myURL.openStream();
  41.  
  42.                          InputStreamReader bufIn = new InputStreamReader(bis);
  43.  
  44.                          ByteArrayBuffer baf = new ByteArrayBuffer(50);
  45.  
  46.                          int current = 0;
  47.  
  48.                          while((current = bis.read()) != -1){
  49.  
  50.                                   baf.append((byte)current);
  51.  
  52.                          }
  53.  
  54.                          dos.write(baf.toByteArray(),0,(baf.toByteArray()).length);
  55.  
  56.                          dos.close();
  57.  
  58.                          Log.i(TAG,"in last of method");
  59.  
  60.                     }
  61.  
  62.                     catch (Exception e) {
  63.  
  64.                         e.printStackTrace();
  65.  
  66.                        }
  67.  
  68.                     }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4



in that code mes print "after set requestProperty" after that can not print any mes,not show any exception and cannot show any thing.......that time i do not use server side coding......how to solved that??
rmeph
Senior Developer
Senior Developer
 
Posts: 121
Joined: Mon Dec 10, 2007 1:54 pm
Location: India

Postby Katharnavas » Thu Jan 10, 2008 8:14 am

I dont think the code you use will throw an exception but it wont do anything bcoz you are just opening an url connection and writing some data on the output stream. You are passing some request to server. What the server should do with the request is unknown since you have not implemented the server side coding to handle the data.

Use the php code on the top of the post and do refer that php file in the Url and try to use the code(whole part is needed) and let us know the result.
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Top

Postby venkat » Thu Jan 10, 2008 9:03 am

Hi Katharnavas,
Thank you for you tutorial :) . I am trying to upload text file to local web server. following coding i am using to upload txt file to local web server.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         String exsistingFileName = "samplefile.txt";
  2.  
  3.         String lineEnd = "\r\n";
  4.  
  5.         String twoHyphens = "--";
  6.  
  7.         String boundary = "*****";
  8.  
  9.         String urlString="http://172.28.65.56/upload.php";
  10.  
  11.         HttpURLConnection conn;
  12.  
  13.         DataOutputStream dos;
  14.  
  15.        
  16.  
  17.         try{
  18.  
  19.                
  20.  
  21.             FileInputStream fileInputStream  = openFileInput(exsistingFileName);
  22.  
  23.             InputStreamReader isr = new InputStreamReader(fileInputStream );
  24.  
  25.             char[] inputBuffer = new char[fileInputStream .available()];
  26.  
  27.             isr.read(inputBuffer);
  28.  
  29.             String readString = new String(inputBuffer);
  30.  
  31.             tv.setText("from file"+readString);     // ----------> It is reading text from file.
  32.  
  33.            
  34.  
  35.             URL url = new URL(urlString);
  36.  
  37.  
  38.  
  39.  
  40.  
  41.          // Open a HTTP connection to the URL
  42.  
  43.  
  44.  
  45.          conn = (HttpURLConnection) url.openConnection();
  46.  
  47.  
  48.  
  49.          // Allow Inputs
  50.  
  51.          conn.setDoInput(true);
  52.  
  53.  
  54.  
  55.          // Allow Outputs
  56.  
  57.          conn.setDoOutput(true);
  58.  
  59.  
  60.  
  61.          // Don't use a cached copy.
  62.  
  63.          conn.setUseCaches(false);
  64.  
  65.  
  66.  
  67.          // Use a post method.
  68.  
  69.          conn.setRequestMethod("POST");
  70.  
  71.  
  72.  
  73.          conn.setRequestProperty("Connection", "Keep-Alive");
  74.  
  75.  
  76.  
  77.          conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
  78.  
  79.  
  80.  
  81.          dos = new DataOutputStream( conn.getOutputStream() );
  82.  
  83.  
  84.  
  85.          dos.writeBytes(twoHyphens + boundary + lineEnd);
  86.  
  87.          dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
  88.  
  89.          dos.writeBytes(lineEnd);
  90.  
  91.  
  92.  
  93.    //      Log.e(Tag,"Headers are written");
  94.  
  95.  
  96.  
  97.          // create a buffer of maximum size
  98.  
  99.         int maxBufferSize=1024;
  100.  
  101.         int bytesAvailable = fileInputStream.available();
  102.  
  103.         int bufferSize = Math.min(bytesAvailable, maxBufferSize);
  104.  
  105.         byte[] buffer = new byte[bufferSize];
  106.  
  107.  
  108.  
  109.          // read file and write it into form...
  110.  
  111.  
  112.  
  113.          int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  114.  
  115.  
  116.  
  117.          while (bytesRead > 0)
  118.  
  119.          {
  120.  
  121.          dos.write(buffer, 0, bufferSize);
  122.  
  123.          bytesAvailable = fileInputStream.available();
  124.  
  125.          bufferSize = Math.min(bytesAvailable, maxBufferSize);
  126.  
  127.          bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  128.  
  129.          }
  130.  
  131.  
  132.  
  133.          // send multipart form data necesssary after file data...
  134.  
  135.  
  136.  
  137.          dos.writeBytes(lineEnd);
  138.  
  139.          dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  140.  
  141.  
  142.  
  143.          // close streams
  144.  
  145.       //   Log.e(Tag,"File is written");
  146.  
  147.          fileInputStream.close();
  148.  
  149.          dos.flush();
  150.  
  151.          dos.close();
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.            
  160.  
  161.         }catch(Exception e){tv.setText("this is error"+e.toString());}
Parsed in 0.045 seconds, using GeSHi 1.0.8.4




My Php Code is here..

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?php
  2.  
  3.  
  4.  
  5. if(count($_FILES)>0)
  6.  
  7. {
  8.  
  9. $target_path = "uploads/";
  10.  
  11. foreach($_FILES as $file)
  12.  
  13. {
  14.  
  15. $target_path = $target_path . basename($file['name']);
  16.  
  17.  
  18.  
  19. if(move_uploaded_file($file['tmp_name'], $target_path)) {
  20.  
  21. chmod($target_path, 0755);
  22.  
  23.  echo "The file ". basename($file['name'])." has been uploaded<br />";
  24.  
  25. } else{
  26.  
  27. echo "There was an error uploading the file, please try again!<br />";
  28.  
  29. }
  30.  
  31. }
  32.  
  33. }
  34.  
  35.  
  36.  
  37.  
  38.  
  39. echo "file name is :".$file['name']."\n";
  40.  
  41. echo "file completed";
  42.  
  43. die();
  44.  
  45. ?>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4



Using above php code, i can upload files from html form.

it's not uploading files from android to local web server :(. Can you tell me where i am making mistake.

Can you attach you full project and php file please..


Thank and Regards,:)
Venkat.
Regards,
Venkat.
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Postby Katharnavas » Thu Jan 10, 2008 11:02 am

This is the php file named upload.php i normally used to upload file to the server. I have test it using both html and android.

Syntax: [ Download ] [ Hide ]
Using php Syntax Highlighting
  1. $target_path = "uploads/";
  2.  
  3.  
  4.  
  5. $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
  6.  
  7.  
  8.  
  9. if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
  10.  
  11.     echo "The file ".  basename( $_FILES['uploadedfile']['name']).
  12.  
  13.     " has been uploaded";
  14.  
  15. } else{
  16.  
  17.     echo "There was an error uploading the file, please try again!";
  18.  
  19. }
Parsed in 0.065 seconds, using GeSHi 1.0.8.4


what is your server response. Do have a folder named uploads in your loacl server. If not so create one bcoz the file is uploaded to that directory only.
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby venkat » Thu Jan 10, 2008 11:27 am

Dear Katharnavas,
I have replaced your php file even though Still i am not getting anything :(.
It's not showing any exception, In uploads folder i have n't find any file. Can you please attach your source
code please.


Thanks and Regards,
Venkat. :)
Regards,
Venkat.
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Postby Katharnavas » Thu Jan 10, 2008 12:03 pm

venkat wrote:Dear Katharnavas,
I have replaced your php file even though Still i am not getting anything :(.
It's not showing any exception, In uploads folder i have n't find any file. Can you please attach your source
code please.


Thanks and Regards,
Venkat. :)


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private void doFileUpload(){
  2.  
  3.  
  4.  
  5.   HttpURLConnection conn = null;
  6.  
  7.   DataOutputStream dos = null;
  8.  
  9.   DataInputStream inStream = null;
  10.  
  11.  
  12.  
  13.  
  14.  
  15.   String exsistingFileName = "/sdcard/t1.mp3";
  16.  
  17.   // Is this the place are you doing something wrong.
  18.  
  19.  
  20.  
  21.   String lineEnd = "\r\n";
  22.  
  23.   String twoHyphens = "--";
  24.  
  25.   String boundary =  "*****";
  26.  
  27.  
  28.  
  29.  
  30.  
  31.   int bytesRead, bytesAvailable, bufferSize;
  32.  
  33.  
  34.  
  35.   byte[] buffer;
  36.  
  37.  
  38.  
  39.   int maxBufferSize = 1*1024*1024;
  40.  
  41.  
  42.  
  43.  
  44.  
  45.   String responseFromServer = "";
  46.  
  47.  
  48.  
  49.   String urlString = "http://192.168.0.2/fp.php";
  50.  
  51.  
  52.  
  53.  
  54.  
  55.   try
  56.  
  57.   {
  58.  
  59.    //------------------ CLIENT REQUEST
  60.  
  61.  
  62.  
  63.   Log.e("MediaPlayer","Inside second Method");
  64.  
  65.  
  66.  
  67.   FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
  68.  
  69.  
  70.  
  71.    // open a URL connection to the Servlet
  72.  
  73.  
  74.  
  75.    URL url = new URL(urlString);
  76.  
  77.  
  78.  
  79.  
  80.  
  81.    // Open a HTTP connection to the URL
  82.  
  83.  
  84.  
  85.    conn = (HttpURLConnection) url.openConnection();
  86.  
  87.  
  88.  
  89.    // Allow Inputs
  90.  
  91.    conn.setDoInput(true);
  92.  
  93.  
  94.  
  95.    // Allow Outputs
  96.  
  97.    conn.setDoOutput(true);
  98.  
  99.  
  100.  
  101.    // Don't use a cached copy.
  102.  
  103.    conn.setUseCaches(false);
  104.  
  105.  
  106.  
  107.    // Use a post method.
  108.  
  109.    conn.setRequestMethod("POST");
  110.  
  111.  
  112.  
  113.    conn.setRequestProperty("Connection", "Keep-Alive");
  114.  
  115.  
  116.  
  117.    conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
  118.  
  119.  
  120.  
  121.    dos = new DataOutputStream( conn.getOutputStream() );
  122.  
  123.  
  124.  
  125.    dos.writeBytes(twoHyphens + boundary + lineEnd);
  126.  
  127.    dos.writeBytes("Content-Disposition: form-data; name="uploadedfile";filename="" + exsistingFileName +""" + lineEnd);
  128.  
  129.    dos.writeBytes(lineEnd);
  130.  
  131.  
  132.  
  133.    Log.e("MediaPlayer","Headers are written");
  134.  
  135.  
  136.  
  137.    // create a buffer of maximum size
  138.  
  139.  
  140.  
  141.    bytesAvailable = fileInputStream.available();
  142.  
  143.    bufferSize = Math.min(bytesAvailable, maxBufferSize);
  144.  
  145.    buffer = new byte[bufferSize];
  146.  
  147.  
  148.  
  149.    // read file and write it into form...
  150.  
  151.  
  152.  
  153.    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  154.  
  155.  
  156.  
  157.    while (bytesRead > 0)
  158.  
  159.    {
  160.  
  161.     dos.write(buffer, 0, bufferSize);
  162.  
  163.     bytesAvailable = fileInputStream.available();
  164.  
  165.     bufferSize = Math.min(bytesAvailable, maxBufferSize);
  166.  
  167.     bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  168.  
  169.    }
  170.  
  171.  
  172.  
  173.    // send multipart form data necesssary after file data...
  174.  
  175.  
  176.  
  177.    dos.writeBytes(lineEnd);
  178.  
  179.    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  180.  
  181.  
  182.  
  183.    // close streams
  184.  
  185.    Log.e("MediaPlayer","File is written");
  186.  
  187.    fileInputStream.close();
  188.  
  189.    dos.flush();
  190.  
  191.    dos.close();
  192.  
  193.  
  194.  
  195.  
  196.  
  197.   }
  198.  
  199.   catch (MalformedURLException ex)
  200.  
  201.   {
  202.  
  203.           Log.e("MediaPlayer", "error: " + ex.getMessage(), ex);
  204.  
  205.   }
  206.  
  207.  
  208.  
  209.   catch (IOException ioe)
  210.  
  211.   {
  212.  
  213.           Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe);
  214.  
  215.   }
  216.  
  217.  
  218.  
  219.  
  220.  
  221.   //------------------ read the SERVER RESPONSE
  222.  
  223.  
  224.  
  225.  
  226.  
  227.   try {
  228.  
  229.            inStream = new DataInputStream ( conn.getInputStream() );
  230.  
  231.            String str;
  232.  
  233.            
  234.  
  235.            while (( str = inStream.readLine()) != null)
  236.  
  237.            {
  238.  
  239.                    Log.e("MediaPlayer","Server Response"+str);
  240.  
  241.            }
  242.  
  243.            inStream.close();
  244.  
  245.  
  246.  
  247.   }
  248.  
  249.   catch (IOException ioex){
  250.  
  251.           Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex);
  252.  
  253.   }
  254.  
  255.  
  256.  
  257. }
Parsed in 0.050 seconds, using GeSHi 1.0.8.4
Last edited by Katharnavas on Thu Jan 10, 2008 12:31 pm, edited 1 time in total.
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby rmeph » Thu Jan 10, 2008 12:14 pm

ok.....i m try simple code for checking connect to server
i use on client code


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  DataOutputStream dos;
  2.  
  3.                         URLConnection con;
  4.  
  5.                          OutputStream out;
  6.  
  7.                         String urlString = new String("http://192.168.0.6/cms_client/nj.php");
  8.  
  9.                         URL url = new URL(urlString);
  10.  
  11.                         con=url.openConnection();
  12.  
  13.                        
  14.  
  15.                         if (con instanceof HttpURLConnection) {
  16.  
  17.                                         System.out.println("This is HttpURLConnection");
  18.  
  19.                                         ((HttpURLConnection)con).setRequestMethod("POST");
  20.  
  21.                                 }
  22.  
  23.  
  24.  
  25.  con.setRequestProperty("Content-Language", "en-US");
  26.  
  27.                         con.setRequestProperty("Content-Type", "multipart/form-data");
  28.  
  29.                         Log.i(TAG,"after setRequestProperty");
  30.  
  31.                         out=con.getOutputStream();
  32.  
  33.                         Log.i(TAG,"after getOutputStream()");
  34.  
  35.                         out.write("nm=enagoyal".getBytes());
  36.  
  37.                         InputStream bis;
  38.  
  39.                         bis=con.getInputStream();
  40.  
  41.                         Log.i(TAG,"444444444444444444");
  42.  
  43.                         byte b[]=new byte[con.getContentLength()];
  44.  
  45.                         ByteArrayBuffer baf = new ByteArrayBuffer(con.getContentLength());
  46.  
  47.                         int index;
  48.  
  49.                         while((index=bis.read())!=-1)
  50.  
  51.                         {
  52.  
  53.                                 baf.append((byte)index);
  54.  
  55.                         }
  56.  
  57.                        String myString = new String(baf.toByteArray());
  58.  
  59.                        
  60.  
  61.                        Log.i(TAG,"myString"+myString);
  62.  
  63.  
  64.  
  65.                        }
  66.  
  67.                     catch (Exception e) {
  68.  
  69.                         e.printStackTrace();
  70.  
  71.                        }
  72.  
  73.                     }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


and server side php file

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?
  2.  
  3. //print_r($_REQUEST);
  4.  
  5. $name=$_REQUEST['nm'];
  6.  
  7. if(!$name)
  8.  
  9. {
  10.  
  11. echo "REQUEST is found";
  12.  
  13. }
  14.  
  15. else
  16.  
  17. {
  18.  
  19. echo  "REQUEST is not found";
  20.  
  21. }
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


but it show Exception

D/dalvkin<570>:Exception java/lang/ClassNotFoundException from PathaClassLoader.java:178 not caught locally
D/dalvkin<570>:NOTE:loadClass 'org/apache/harmony/iuni/util/ExtrenalMessages_en_US' 0x40017d80 threw an Exception
that Exception show after setRequestProperty
how to solved it???
rmeph
Senior Developer
Senior Developer
 
Posts: 121
Joined: Mon Dec 10, 2007 1:54 pm
Location: India

Postby Katharnavas » Thu Jan 10, 2008 12:29 pm

rmeph wrote:ok.....i m try simple code for checking connect to server
i use on client code


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  DataOutputStream dos;
  2.                         URLConnection con;
  3.                          OutputStream out;
  4.                         String urlString = new String("http://192.168.0.6/cms_client/nj.php");
  5.                         URL url = new URL(urlString);
  6.                         con=url.openConnection();
  7.                        
  8.                         if (con instanceof HttpURLConnection) {
  9.                                         System.out.println("This is HttpURLConnection");
  10.                                         ((HttpURLConnection)con).setRequestMethod("POST");
  11.                                 }
  12.  
  13.  con.setRequestProperty("Content-Language", "en-US");
  14.                         con.setRequestProperty("Content-Type", "multipart/form-data");
  15.                         Log.i(TAG,"after setRequestProperty");
  16.                         out=con.getOutputStream();
  17.                         Log.i(TAG,"after getOutputStream()");
  18.                         out.write("nm=enagoyal".getBytes());
  19.                         InputStream bis;
  20.                         bis=con.getInputStream();
  21.                         Log.i(TAG,"444444444444444444");
  22.                         byte b[]=new byte[con.getContentLength()];
  23.                         ByteArrayBuffer baf = new ByteArrayBuffer(con.getContentLength());
  24.                         int index;
  25.                         while((index=bis.read())!=-1)
  26.                         {
  27.                                 baf.append((byte)index);
  28.                         }
  29.                        String myString = new String(baf.toByteArray());
  30.                        
  31.                        Log.i(TAG,"myString"+myString);
  32.  
  33.                        }
  34.                     catch (Exception e) {
  35.                         e.printStackTrace();
  36.                        }
  37.                     }
Parsed in 0.044 seconds, using GeSHi 1.0.8.4


and server side php file

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?
  2. //print_r($_REQUEST);
  3. $name=$_REQUEST['nm'];
  4. if(!$name)
  5. {
  6. echo "REQUEST is found";
  7. }
  8. else
  9. {
  10. echo  "REQUEST is not found";
  11. }
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


but it show Exception

D/dalvkin<570>:Exception java/lang/ClassNotFoundException from PathaClassLoader.java:178 not caught locally
D/dalvkin<570>:NOTE:loadClass 'org/apache/harmony/iuni/util/ExtrenalMessages_en_US' 0x40017d80 threw an Exception
that Exception show after setRequestProperty
how to solved it???



hi try this,
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. try {
  2.  
  3.                 // Construct data
  4.  
  5.                         String c = "hai this is test";
  6.  
  7.                 String data = URLEncoder.encode("nm", "UTF-8") + "=" + URLEncoder.encode(c, "UTF-8");
  8.  
  9.            
  10.  
  11.                 // Send data
  12.  
  13.                 URL url = new URL("http://192.168.0.6/cms_client/nj.php");
  14.  
  15.                 URLConnection conn = url.openConnection();
  16.  
  17.                 conn.setDoOutput(true);
  18.  
  19.                 OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
  20.  
  21.                 wr.write(data);
  22.  
  23.                 wr.flush();
  24.  
  25.            
  26.  
  27.                 // Get the response
  28.  
  29.                 BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  30.  
  31.                 String line;
  32.  
  33.                 while ((line = rd.readLine()) != null) {                
  34.  
  35.                         showAlert("Dialoge Box", "Message: " + line,"OK", false);
  36.  
  37.                 }
  38.  
  39.                 wr.close();
  40.  
  41.                
  42.  
  43.                rd.close();
  44.  
  45.                
  46.  
  47.             } catch (Exception e) {
  48.  
  49.             }
Parsed in 0.042 seconds, using GeSHi 1.0.8.4
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby rmeph » Thu Jan 10, 2008 12:40 pm

thanks a lots...........it's working :D
rmeph
Senior Developer
Senior Developer
 
Posts: 121
Joined: Mon Dec 10, 2007 1:54 pm
Location: India

Postby Katharnavas » Thu Jan 10, 2008 1:03 pm

rmeph wrote:thanks a lots...........it's working :D


Its nice to see that it works for you ?
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby rmeph » Thu Jan 10, 2008 1:19 pm

when i upload txt file then show same exception

D/dalvkin<570>:Exception java/lang/ClassNotFoundException from PathaClassLoader.java:178 not caught locally
D/dalvkin<570>:NOTE:loadClass 'org/apache/harmony/iuni/util/ExtrenalMessages_en_US' 0x40017d80 threw an exception

i client side code is

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. HttpURLConnection conn = null;
  2.  
  3.                          DataOutputStream dos = null;
  4.  
  5.                          DataInputStream inStream = null;
  6.  
  7.                          String exsistingFileName =data/download/r.txt;
  8.  
  9.                          String lineEnd = "\r\n";
  10.  
  11.                          String twoHyphens = "--";
  12.  
  13.                          String boundary = "*****";
  14.  
  15.                          int bytesRead, bytesAvailable, bufferSize;
  16.  
  17.                          byte[] buffer;
  18.  
  19.                          int maxBufferSize = 1*1024*1024;
  20.  
  21.                          String responseFromServer = "";
  22.  
  23.                          String urlString = "http://192.168.0.6/r&D/test.php";
  24.  
  25.                          
  26.  
  27.                          try
  28.  
  29.                          {
  30.  
  31.                          Log.e("fileupload","Start");
  32.  
  33.                          FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
  34.  
  35.                          URL url = new URL(urlString);
  36.  
  37.                          conn = (HttpURLConnection) url.openConnection();
  38.  
  39.                          conn.setDoInput(true);
  40.  
  41.                          conn.setDoOutput(true);
  42.  
  43.                          conn.setUseCaches(false);
  44.  
  45.                          conn.setRequestMethod("POST");
  46.  
  47.                          conn.setRequestProperty("Connection", "Keep-Alive");
  48.  
  49.                          conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
  50.  
  51.                          dos = new DataOutputStream( conn.getOutputStream() );
  52.  
  53.                          dos.writeBytes(twoHyphens + boundary + lineEnd);
  54.  
  55.                          dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
  56.  
  57.                          dos.writeBytes(lineEnd);
  58.  
  59.                          Log.e("fileupload","after writeByte");
  60.  
  61.                          bytesAvailable = fileInputStream.available();
  62.  
  63.                          bufferSize = Math.min(bytesAvailable, maxBufferSize);
  64.  
  65.                          buffer = new byte[bufferSize];
  66.  
  67.                          bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  68.  
  69.                          while (bytesRead > 0)
  70.  
  71.                          {
  72.  
  73.                          dos.write(buffer, 0, bufferSize);
  74.  
  75.                          bytesAvailable = fileInputStream.available();
  76.  
  77.                          bufferSize = Math.min(bytesAvailable, maxBufferSize);
  78.  
  79.                          bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  80.  
  81.                          }
  82.  
  83.                          dos.writeBytes(lineEnd);
  84.  
  85.                          dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  86.  
  87.                          fileInputStream.close();
  88.  
  89.                          dos.flush();
  90.  
  91.                          BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  92.  
  93.                          String line;
  94.  
  95.                          while ((line = rd.readLine()) != null) {
  96.  
  97.                          showAlert("Dialoge Box", "Message: " + line,"OK", false);
  98.  
  99.                          }
  100.  
  101.                          dos.close();
  102.  
  103.                          rd.close();
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.                          }
  112.  
  113.                          catch (MalformedURLException ex)
  114.  
  115.                          {
  116.  
  117.                          Log.e("MediaPlayer", "error: " + ex.getMessage(), ex);
  118.  
  119.                          }
  120.  
  121.  
  122.  
  123.                          catch (IOException ioe)
  124.  
  125.                          {
  126.  
  127.                          Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe);
  128.  
  129.                          }
  130.  
  131.  
  132.  
  133.            }
Parsed in 0.052 seconds, using GeSHi 1.0.8.4

and server side

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?
  2.  
  3. $file=$_FILES['name'];
  4.  
  5. if($file['size'] >= 0)
  6.  
  7. {
  8.  
  9.         $fm=move_uploaded_file($_FILES['name']['tmp_name'],'test.txt');
  10.  
  11.         if(!$fm)
  12.  
  13.         {
  14.  
  15.         die('File Not Moved');
  16.  
  17.         }
  18.  
  19.         else
  20.  
  21.         {
  22.  
  23.                 echo "File Copied";
  24.  
  25.         }
  26.  
  27. }
  28.  
  29. ?>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


How to solved it???
rmeph
Senior Developer
Senior Developer
 
Posts: 121
Joined: Mon Dec 10, 2007 1:54 pm
Location: India

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: Yahoo [Bot] and 7 guests