Upload Files to Web Server

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

Postby raquibulbari » Thu Jan 24, 2008 5:34 pm

Here is the code that worked for me
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4.  
  5.  
  6. FileInputStream fileInputStream = null;
  7.  
  8.     void thirdTry(){
  9.  
  10.         String exsistingFileName = "asdf.png";
  11.  
  12.  
  13.  
  14.         String lineEnd = "\r\n";
  15.  
  16.         String twoHyphens = "--";
  17.  
  18.         String boundary = "*****";
  19.  
  20.         String Tag="3rd";
  21.  
  22.         try
  23.  
  24.         {
  25.  
  26.         //------------------ CLIENT REQUEST
  27.  
  28.  
  29.  
  30.         Log.e(Tag,"Inside second Method");
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.         // Open a HTTP connection to the URL
  39.  
  40.  
  41.  
  42.        //connectURL is a URL object
  43.  
  44.         HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
  45.  
  46.  
  47.  
  48.         // Allow Inputs
  49.  
  50.         conn.setDoInput(true);
  51.  
  52.  
  53.  
  54.         // Allow Outputs
  55.  
  56.         conn.setDoOutput(true);
  57.  
  58.  
  59.  
  60.         // Don't use a cached copy.
  61.  
  62.         conn.setUseCaches(false);
  63.  
  64.  
  65.  
  66.         // Use a post method.
  67.  
  68.         conn.setRequestMethod("POST");
  69.  
  70.  
  71.  
  72.         conn.setRequestProperty("Connection", "Keep-Alive");
  73.  
  74.  
  75.  
  76.         conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
  77.  
  78.  
  79.  
  80.         DataOutputStream dos = new DataOutputStream( conn.getOutputStream() );
  81.  
  82.  
  83.  
  84.         dos.writeBytes(twoHyphens + boundary + lineEnd);
  85.  
  86.         dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
  87.  
  88.         dos.writeBytes(lineEnd);
  89.  
  90.        
  91.  
  92.        
  93.  
  94.  
  95.  
  96.  
  97.  
  98.         Log.e(Tag,"Headers are written");
  99.  
  100.  
  101.  
  102.         // create a buffer of maximum size
  103.  
  104.  
  105.  
  106.         int bytesAvailable = fileInputStream.available();
  107.  
  108.         int maxBufferSize = 1024;
  109.  
  110.         int bufferSize = Math.min(bytesAvailable, maxBufferSize);
  111.  
  112.         byte[] buffer = new byte[bufferSize];
  113.  
  114.  
  115.  
  116.         // read file and write it into form...
  117.  
  118.  
  119.  
  120.         int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  121.  
  122.  
  123.  
  124.         while (bytesRead > 0)
  125.  
  126.         {
  127.  
  128.                 dos.write(buffer, 0, bufferSize);
  129.  
  130.                 bytesAvailable = fileInputStream.available();
  131.  
  132.                 bufferSize = Math.min(bytesAvailable, maxBufferSize);
  133.  
  134.                 bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  135.  
  136.         }
  137.  
  138.  
  139.  
  140.         // send multipart form data necesssary after file data...
  141.  
  142.  
  143.  
  144.         dos.writeBytes(lineEnd);
  145.  
  146.        
  147.  
  148.        
  149.  
  150.        
  151.  
  152.        
  153.  
  154.        
  155.  
  156.         dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  157.  
  158.  
  159.  
  160.         // close streams
  161.  
  162.         Log.e(Tag,"File is written");
  163.  
  164.         fileInputStream.close();
  165.  
  166.         dos.flush();
  167.  
  168.        
  169.  
  170.         InputStream is = conn.getInputStream();
  171.  
  172.                 //      retrieve the response from server
  173.  
  174.                 int ch;
  175.  
  176.  
  177.  
  178.                 StringBuffer b =new StringBuffer();
  179.  
  180.                 while( ( ch = is.read() ) != -1 ){
  181.  
  182.                         b.append( (char)ch );
  183.  
  184.         }
  185.  
  186.                 String s=b.toString();         
  187.  
  188.                 Log.i("Fuck response",s);
  189.  
  190.         dos.close();
  191.  
  192.  
  193.  
  194.  
  195.  
  196.         }
  197.  
  198.         catch (MalformedURLException ex)
  199.  
  200.         {
  201.  
  202.         Log.e(Tag, "error: " + ex.getMessage(), ex);
  203.  
  204.         }
  205.  
  206.  
  207.  
  208.         catch (IOException ioe)
  209.  
  210.         {
  211.  
  212.         Log.e(Tag, "error: " + ioe.getMessage(), ioe);
  213.  
  214.         }
  215.  
  216.     }
  217.  
  218.  
Parsed in 0.042 seconds, using GeSHi 1.0.8.4


This code worked for me, php code is same as posted before :D
Shimugool
User avatar
raquibulbari
Developer
Developer
 
Posts: 25
Joined: Sun Dec 16, 2007 6:31 am
Location: dhaka,bangladesh

Top

Postby Katharnavas » Fri Jan 25, 2008 5:03 am

raquibulbari wrote:Katharnavas thanks man , your code came in great help for me, the whole day i was trying to upload image, the code was about to be same like you , at least your code worked, thanks man, i was just getting frustrated on me

:D


Its nice to hear that you got ur problem fixed..
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby cyberrob » Mon Feb 04, 2008 7:48 am

Hey guys, I try codes you post above, Seems everything ok but...

I got a Java.io.FileNotFoundException

I try
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String exsistingFileName = "ning";
  2.  
  3. fileInputStream = openFileInput(exsistingFileName);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

exception happened at line2, the one I assign path to fileInputStream
Same result when I use
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. fileInputStream = new FileInputStream(new File(exsistingFileName))
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


So I'm wondering where should I point the file path to ??
I put the .png file in /res/drawble/
and include it by using getString(R.drawable.abc.png),which also give me the same exception.

Hope someone could help me out here! THX a LOT!
User avatar
cyberrob
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Dec 13, 2007 3:01 am

Postby plusminus » Mon Feb 04, 2008 10:22 am

Hello cyberrob,

:idea: raquibulbari probably had the file manually pushed to the emulator :!:
you cannot use getString(R.drawable.abc.png);, because it is only capable of grabbing String-Resources and not the Names of other resources. Also you can not access images you placed under "/res/drawable/xyz.png", by their filenames, because they got compiled into your application.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby cyberrob » Tue Feb 12, 2008 3:33 am

Oh!that's why I cant do it that way,ok! I try manually push file into it. Thanks! :D

plusminus wrote:Hello cyberrob,

:idea: raquibulbari probably had the file manually pushed to the emulator :!:
you cannot use getString(R.drawable.abc.png);, because it is only capable of grabbing String-Resources and not the Names of other resources. Also you can not access images you placed under "/res/drawable/xyz.png", by their filenames, because they got compiled into your application.

Regards,
plusminus
User avatar
cyberrob
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Dec 13, 2007 3:01 am

Postby cyberrob » Thu Feb 14, 2008 9:10 am

Finally I got everything WORK!!
I found posts above miss to assign FileInputStream a value, so I assign the file I wanna upload like this:
Code: Select all
fileInputStream = new FileInputStream(exsistingFileName);


in try-catch block, so it all work out! :lol:
Next Step try to show the php echo msg on "showalert"
Thank you guys!!

plusminus wrote:Hello cyberrob,

:idea: raquibulbari probably had the file manually pushed to the emulator :!:
you cannot use getString(R.drawable.abc.png);, because it is only capable of grabbing String-Resources and not the Names of other resources. Also you can not access images you placed under "/res/drawable/xyz.png", by their filenames, because they got compiled into your application.

Regards,
plusminus
User avatar
cyberrob
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Dec 13, 2007 3:01 am

Top

Postby inter » Wed Feb 27, 2008 3:17 pm

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


I use this code in server side .But it not work .It response "There was an error uploading the side ,please try again!!"

The client side is same above.
Can you help me. Thanks.
inter
Junior Developer
Junior Developer
 
Posts: 23
Joined: Thu Feb 21, 2008 10:13 am

Postby inter » Wed Feb 27, 2008 3:26 pm

I'm sory .I have just done it
Thank a lot!!!!
inter
Junior Developer
Junior Developer
 
Posts: 23
Joined: Thu Feb 21, 2008 10:13 am

wre to place image file,while uploding to server

Postby ekambresh » Mon Mar 17, 2008 11:31 am

please tell me wre to plce the image file while uploading to the server....if possible send me the tutorial





tnx
ekambresh
ekambresh
ekambresh
Junior Developer
Junior Developer
 
Posts: 22
Joined: Fri Feb 08, 2008 8:54 am
Location: Bangalore

Postby venkat » Mon Mar 17, 2008 2:39 pm

Hi ekambresh, u can upload images also. please push images manually to files folder using "DDMS" tool .


here how you can push images into "files" folder.

window --> show view --> File Explorer than, navigate to data/data/[your package structre]/files then,

select that folder, then , there is two small phone icons. first one is used to pull files from that folder to your system and another one is used to push files into that folder.

use second icon to push files from your system to "files" folder.

I hope it will help. :)
Regards,
Venkat.
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

path

Postby rohan » Wed Mar 19, 2008 2:35 pm

venkat wrote:Hi ekambresh, u can upload images also. please push images manually to files folder using "DDMS" tool .


here how you can push images into "files" folder.

window --> show view --> File Explorer than, navigate to data/data/[your package structre]/files then,

select that folder, then , there is two small phone icons. first one is used to pull files from that folder to your system and another one is used to push files into that folder.

use second icon to push files from your system to "files" folder.

I hope it will help. :)



hi venkat,
I am new to android.
I did not get the path that you have given "window --> show view --> File Explorer than, navigate to data/data/[your package structre]/files" . how do I go to the above path? and also what is "DDMS" tool .

thanx in advance
rohan
Developer
Developer
 
Posts: 34
Joined: Wed Mar 19, 2008 2:26 pm

Postby mehta » Sun Mar 30, 2008 1:45 am

In the examples above, the path of the file to be uploaded is hardcoded. Is there a way to let the user choose the file path dynamically?
mehta
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Mar 30, 2008 1:41 am

Postby marielisacr » Mon Jul 14, 2008 2:18 pm

Hi, am trying to upoad a file to a server,

the problem is that I need to send two differents parameters to the server, one is the file and the another is a text.

I am trying this code:

public void doFileUpload(String exsistingFileName, String xml){

HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;


//String exsistingFileName = "/sdcard/img2.jpg";
// Is this the place are you doing something wrong.

String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "-----------------------------29772313742745";


int bytesRead, bytesAvailable, bufferSize;

byte[] buffer;

int maxBufferSize = 1*1024*1024;


String responseFromServer = "";

String urlString = Constants.WEB_ADDRESS+"upload.do";


try
{
//------------------ CLIENT REQUEST

Log.e("MediaPlayer","Inside second Method");

FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );

// open a URL connection to the Servlet

URL url = new URL(urlString);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Cookie", "JSESSIONID="+PlayList.getSessionId());
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

dos = new DataOutputStream( conn.getOutputStream() );

dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"xml\""+lineEnd+URLEncoder.encode(xml,"UTF-8") + lineEnd);
dos.writeBytes(boundary + lineEnd);
//dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"fileFile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
dos.writeBytes(lineEnd);

Log.e("MediaPlayer","xml ");

Log.e("MediaPlayer","Headers are written");

// create a buffer of maximum size

bytesAvailable = fileInputStream.available();
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);

// close streams
Log.e("MediaPlayer","File is written");
fileInputStream.close();
dos.flush();
dos.close();


}
catch (MalformedURLException ex)
{
Log.e("MediaPlayer", "error: " + ex.getMessage(), ex);
}

catch (Exception ioe)
{
Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe);
}


//------------------ read the SERVER RESPONSE


try {
inStream = new DataInputStream ( conn.getInputStream() );
String str;

while (( str = inStream.readLine()) != null)
{
Log.e("MediaPlayer","Server Response"+str);
}
inStream.close();

}
catch (Exception ioex){
Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex);
}

}

but it doesn't work, it only recognizes one of the parameters. I got an error on the server's side that the file is null and the text variable had everything, including the file.

I hope someone can help me.

Thanks.
marielisacr
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed May 21, 2008 10:04 pm

Postby marielisacr » Mon Jul 14, 2008 2:20 pm

Sorry, here is the code more clear

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void doFileUpload(String exsistingFileName, String xml){
  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/img2.jpg";
  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 =  "-----------------------------29772313742745";
  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 = Constants.WEB_ADDRESS+"upload.do";
  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.                         // Open a HTTP connection to the URL
  78.  
  79.                         conn = (HttpURLConnection) url.openConnection();
  80.  
  81.                         // Allow Inputs
  82.  
  83.                         conn.setDoInput(true);
  84.  
  85.                         // Allow Outputs
  86.  
  87.                         conn.setDoOutput(true);
  88.  
  89.                         // Don't use a cached copy.
  90.  
  91.                         conn.setUseCaches(false);
  92.  
  93.                         // Use a post method.
  94.  
  95.                         conn.setRequestMethod("POST");
  96.  
  97.                         conn.setRequestProperty("Connection", "Keep-Alive");
  98.  
  99.                         conn.setRequestProperty("Cookie", "JSESSIONID="+PlayList.getSessionId());
  100.  
  101.                         conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
  102.  
  103.  
  104.  
  105.                         dos = new DataOutputStream( conn.getOutputStream() );
  106.  
  107.  
  108.  
  109.                         dos.writeBytes(twoHyphens + boundary + lineEnd);
  110.  
  111.                         dos.writeBytes("Content-Disposition: form-data; name=\"xml\""+lineEnd+URLEncoder.encode(xml,"UTF-8") + lineEnd);
  112.  
  113.                         dos.writeBytes(boundary + lineEnd);
  114.  
  115.                         //dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  116.  
  117.                         dos.writeBytes("Content-Disposition: form-data; name=\"fileFile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
  118.  
  119.                         dos.writeBytes(lineEnd);
  120.  
  121.  
  122.  
  123.                         Log.e("MediaPlayer","xml ");
  124.  
  125.  
  126.  
  127.                         Log.e("MediaPlayer","Headers are written");
  128.  
  129.  
  130.  
  131.                         // create a buffer of maximum size
  132.  
  133.  
  134.  
  135.                         bytesAvailable = fileInputStream.available();
  136.  
  137.                         bufferSize = Math.min(bytesAvailable, maxBufferSize);
  138.  
  139.                         buffer = new byte[bufferSize];
  140.  
  141.  
  142.  
  143.                         // read file and write it into form...
  144.  
  145.  
  146.  
  147.                         bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  148.  
  149.  
  150.  
  151.                         while (bytesRead > 0)
  152.  
  153.                         {
  154.  
  155.                                 dos.write(buffer, 0, bufferSize);
  156.  
  157.                                 bytesAvailable = fileInputStream.available();
  158.  
  159.                                 bufferSize = Math.min(bytesAvailable, maxBufferSize);
  160.  
  161.                                 bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  162.  
  163.                         }
  164.  
  165.  
  166.  
  167.                         // send multipart form data necesssary after file data...
  168.  
  169.  
  170.  
  171.                         dos.writeBytes(lineEnd);
  172.  
  173.                         dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  174.  
  175.  
  176.  
  177.                         // close streams
  178.  
  179.                         Log.e("MediaPlayer","File is written");
  180.  
  181.                         fileInputStream.close();
  182.  
  183.                         dos.flush();
  184.  
  185.                         dos.close();
  186.  
  187.  
  188.  
  189.  
  190.  
  191.                 }
  192.  
  193.                 catch (MalformedURLException ex)
  194.  
  195.                 {
  196.  
  197.                         Log.e("MediaPlayer", "error: " + ex.getMessage(), ex);
  198.  
  199.                 }
  200.  
  201.  
  202.  
  203.                 catch (Exception ioe)
  204.  
  205.                 {
  206.  
  207.                         Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe);
  208.  
  209.                 }
  210.  
  211.  
  212.  
  213.  
  214.  
  215.                 //------------------ read the SERVER RESPONSE
  216.  
  217.  
  218.  
  219.  
  220.  
  221.                 try {
  222.  
  223.                         inStream = new DataInputStream ( conn.getInputStream() );
  224.  
  225.                         String str;
  226.  
  227.  
  228.  
  229.                         while (( str = inStream.readLine()) != null)
  230.  
  231.                         {
  232.  
  233.                                 Log.e("MediaPlayer","Server Response"+str);
  234.  
  235.                         }
  236.  
  237.                         inStream.close();
  238.  
  239.  
  240.  
  241.                 }
  242.  
  243.                 catch (Exception ioex){
  244.  
  245.                         Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex);
  246.  
  247.                 }
  248.  
  249.  
  250.  
  251.         }
Parsed in 0.052 seconds, using GeSHi 1.0.8.4
marielisacr
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed May 21, 2008 10:04 pm

how to get Data to server in java

Postby Niketa » Tue Oct 07, 2008 12:30 pm

hi,

Can any one tell what if the server is the java.

Actually i want to retrieve file in a servlet so what should i do.

I mean to say if i want to send data in servlet then how can i get it. I have success fully send data to server but

how can i retrieve it at server in java code.



Thank you
Niketa
Developer
Developer
 
Posts: 36
Joined: Wed Sep 17, 2008 12:37 pm

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 8 guests