| Author |
Message |
Katharnavas Senior Developer

Joined: 04 Dec 2007 Posts: 100 Location: India
|
Posted: Wed Jan 09, 2008 8:34 am Post subject: |
|
|
| rmeph wrote: | hi all,
any one tel me a simple file in android how to upload on server............
my file in
| Java: | File f = new File(aDirectory.getAbsolutePath()); |
that upload on server using http protocal............. |
Did nt you try the code above just pass your file parameter in place of new File() |
|
| Back to top |
|
 |
|
|
 |
venkat Senior Developer

Joined: 27 Nov 2007 Posts: 152 Location: India
|
Posted: Wed Jan 09, 2008 8:54 am Post subject: |
|
|
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. |
|
| Back to top |
|
 |
rmeph Senior Developer

Joined: 10 Dec 2007 Posts: 120 Location: India
|
Posted: Wed Jan 09, 2008 1:26 pm Post subject: |
|
|
| ok..actually i want upload file in android to server without using server side scripting.......is it possible ???how??? |
|
| Back to top |
|
 |
Katharnavas Senior Developer

Joined: 04 Dec 2007 Posts: 100 Location: India
|
Posted: Wed Jan 09, 2008 2:02 pm Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
rmeph Senior Developer

Joined: 10 Dec 2007 Posts: 120 Location: India
|
Posted: Thu Jan 10, 2008 8:04 am Post subject: |
|
|
i want .txt file upload on server
I use client side coding
| Java: |
DataOutputStream dos;
URLConnection con;
String urlString = new String("http://localhost:8080/webdav/ee/");
URL url = new URL(urlString);
con=url.openConnection();
if (con instanceof HttpURLConnection) {
System.out.println("This is HttpURLConnection");
((HttpURLConnection)con).setRequestMethod("POST");
}
con.setDoInput(true);
Log.i(TAG,"setDoInputsetDoInput");
con.setDoOutput(true);
Log.i(TAG,"setDoOutputsetDoOutputsetDoOutput");
con.setRequestProperty("Content-type", "multipart/form-data");
Log.i(TAG,"after set requestProperty");
dos = new DataOutputStream(con.getOutputStream());
Log.i(TAG,"after DataOutputStream ");
URL myURL = new URL("sample.txt");
InputStream bis = myURL.openStream();
InputStreamReader bufIn = new InputStreamReader(bis);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}
dos.write(baf.toByteArray(),0,(baf.toByteArray()).length);
dos.close();
Log.i(TAG,"in last of method");
}
catch (Exception e) {
e.printStackTrace();
}
} |
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?? |
|
| Back to top |
|
 |
Katharnavas Senior Developer

Joined: 04 Dec 2007 Posts: 100 Location: India
|
Posted: Thu Jan 10, 2008 8:14 am Post subject: |
|
|
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. |
|
| Back to top |
|
 |
|
|
 |
venkat Senior Developer

Joined: 27 Nov 2007 Posts: 152 Location: India
|
Posted: Thu Jan 10, 2008 9:03 am Post subject: |
|
|
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.
| Java: | String exsistingFileName = "samplefile.txt";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
String urlString="http://172.28.65.56/upload.php";
HttpURLConnection conn;
DataOutputStream dos;
try{
FileInputStream fileInputStream = openFileInput(exsistingFileName);
InputStreamReader isr = new InputStreamReader(fileInputStream );
char[] inputBuffer = new char[fileInputStream .available()];
isr.read(inputBuffer);
String readString = new String(inputBuffer);
tv.setText("from file"+readString); // ----------> It is reading text from file.
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("Content-Type", "multipart/form-data;boundary="+boundary);
dos = new DataOutputStream( conn.getOutputStream() );
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
dos.writeBytes(lineEnd);
// Log.e(Tag,"Headers are written");
// create a buffer of maximum size
int maxBufferSize=1024;
int bytesAvailable = fileInputStream.available();
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
// read file and write it into form...
int 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(Tag,"File is written");
fileInputStream.close();
dos.flush();
dos.close();
}catch(Exception e){tv.setText("this is error"+e.toString());} |
My Php Code is here..
| XML: | <?php
if(count($_FILES)>0)
{
$target_path = "uploads/";
foreach($_FILES as $file)
{
$target_path = $target_path . basename($file['name']);
if(move_uploaded_file($file['tmp_name'], $target_path)) {
chmod($target_path, 0755);
echo "The file ". basename($file['name'])." has been uploaded<br />";
} else{
echo "There was an error uploading the file, please try again!<br />";
}
}
}
echo "file name is :".$file['name']."\n";
echo "file completed";
die();
?> |
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. |
|
| Back to top |
|
 |
Katharnavas Senior Developer

Joined: 04 Dec 2007 Posts: 100 Location: India
|
Posted: Thu Jan 10, 2008 11:02 am Post subject: |
|
|
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.
| php: | $target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
} |
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. |
|
| Back to top |
|
 |
venkat Senior Developer

Joined: 27 Nov 2007 Posts: 152 Location: India
|
Posted: Thu Jan 10, 2008 11:27 am Post subject: |
|
|
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. |
|
| Back to top |
|
 |
Katharnavas Senior Developer

Joined: 04 Dec 2007 Posts: 100 Location: India
|
Posted: Thu Jan 10, 2008 12:03 pm Post subject: |
|
|
| 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.  |
| Java: | private void doFileUpload(){
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String exsistingFileName = "/sdcard/t1.mp3";
// Is this the place are you doing something wrong.
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
String responseFromServer = "";
String urlString = "http://192.168.0.2/fp.php";
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("Content-Type", "multipart/form-data;boundary="+boundary);
dos = new DataOutputStream( conn.getOutputStream() );
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
dos.writeBytes(lineEnd);
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 (IOException 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 (IOException ioex){
Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex);
}
} |
Last edited by Katharnavas on Thu Jan 10, 2008 12:31 pm; edited 1 time in total |
|
| Back to top |
|
 |
rmeph Senior Developer

Joined: 10 Dec 2007 Posts: 120 Location: India
|
Posted: Thu Jan 10, 2008 12:14 pm Post subject: |
|
|
ok.....i m try simple code for checking connect to server
i use on client code
| Java: | DataOutputStream dos;
URLConnection con;
OutputStream out;
String urlString = new String("http://192.168.0.6/cms_client/nj.php");
URL url = new URL(urlString);
con=url.openConnection();
if (con instanceof HttpURLConnection) {
System.out.println("This is HttpURLConnection");
((HttpURLConnection)con).setRequestMethod("POST");
}
con.setRequestProperty("Content-Language", "en-US");
con.setRequestProperty("Content-Type", "multipart/form-data");
Log.i(TAG,"after setRequestProperty");
out=con.getOutputStream();
Log.i(TAG,"after getOutputStream()");
out.write("nm=enagoyal".getBytes());
InputStream bis;
bis=con.getInputStream();
Log.i(TAG,"444444444444444444");
byte b[]=new byte[con.getContentLength()];
ByteArrayBuffer baf = new ByteArrayBuffer(con.getContentLength());
int index;
while((index=bis.read())!=-1)
{
baf.append((byte)index);
}
String myString = new String(baf.toByteArray());
Log.i(TAG,"myString"+myString);
}
catch (Exception e) {
e.printStackTrace();
}
} |
and server side php file
| XML: | <?
//print_r($_REQUEST);
$name=$_REQUEST['nm'];
if(!$name)
{
echo "REQUEST is found";
}
else
{
echo "REQUEST is not found";
}
|
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??? |
|
| Back to top |
|
 |
Katharnavas Senior Developer

Joined: 04 Dec 2007 Posts: 100 Location: India
|
Posted: Thu Jan 10, 2008 12:29 pm Post subject: |
|
|
| rmeph wrote: | ok.....i m try simple code for checking connect to server
i use on client code
| Java: | DataOutputStream dos;
URLConnection con;
OutputStream out;
String urlString = new String("http://192.168.0.6/cms_client/nj.php");
URL url = new URL(urlString);
con=url.openConnection();
if (con instanceof HttpURLConnection) {
System.out.println("This is HttpURLConnection");
((HttpURLConnection)con).setRequestMethod("POST");
}
con.setRequestProperty("Content-Language", "en-US");
con.setRequestProperty("Content-Type", "multipart/form-data");
Log.i(TAG,"after setRequestProperty");
out=con.getOutputStream();
Log.i(TAG,"after getOutputStream()");
out.write("nm=enagoyal".getBytes());
InputStream bis;
bis=con.getInputStream();
Log.i(TAG,"444444444444444444");
byte b[]=new byte[con.getContentLength()];
ByteArrayBuffer baf = new ByteArrayBuffer(con.getContentLength());
int index;
while((index=bis.read())!=-1)
{
baf.append((byte)index);
}
String myString = new String(baf.toByteArray());
Log.i(TAG,"myString"+myString);
}
catch (Exception e) {
e.printStackTrace();
}
} |
and server side php file
| XML: | <?
//print_r($_REQUEST);
$name=$_REQUEST['nm'];
if(!$name)
{
echo "REQUEST is found";
}
else
{
echo "REQUEST is not found";
}
|
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,
| Java: | try {
// Construct data
String c = "hai this is test";
String data = URLEncoder.encode("nm", "UTF-8") + "=" + URLEncoder.encode(c, "UTF-8");
// Send data
URL url = new URL("http://192.168.0.6/cms_client/nj.php");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
showAlert("Dialoge Box", "Message: " + line,"OK", false);
}
wr.close();
rd.close();
} catch (Exception e) {
} |
|
|
| Back to top |
|
 |
rmeph Senior Developer

Joined: 10 Dec 2007 Posts: 120 Location: India
|
Posted: Thu Jan 10, 2008 12:40 pm Post subject: |
|
|
thanks a lots...........it's working  |
|
| Back to top |
|
 |
Katharnavas Senior Developer

Joined: 04 Dec 2007 Posts: 100 Location: India
|
Posted: Thu Jan 10, 2008 1:03 pm Post subject: |
|
|
| rmeph wrote: | thanks a lots...........it's working  |
Its nice to see that it works for you ? |
|
| Back to top |
|
 |
rmeph Senior Developer

Joined: 10 Dec 2007 Posts: 120 Location: India
|
Posted: Thu Jan 10, 2008 1:19 pm Post subject: |
|
|
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
| Java: | HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String exsistingFileName =data/download/r.txt;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
String responseFromServer = "";
String urlString = "http://192.168.0.6/r&D/test.php";
try
{
Log.e("fileupload","Start");
FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
URL url = new URL(urlString);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
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=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
dos.writeBytes(lineEnd);
Log.e("fileupload","after writeByte");
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
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);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
fileInputStream.close();
dos.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
showAlert("Dialoge Box", "Message: " + line,"OK", false);
}
dos.close();
rd.close();
}
catch (MalformedURLException ex)
{
Log.e("MediaPlayer", "error: " + ex.getMessage(), ex);
}
catch (IOException ioe)
{
Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe);
}
} |
and server side
| XML: | <?
$file=$_FILES['name'];
if($file['size'] >= 0)
{
$fm=move_uploaded_file($_FILES['name']['tmp_name'],'test.txt');
if(!$fm)
{
die('File Not Moved');
}
else
{
echo "File Copied";
}
}
?> |
How to solved it??? |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
© 2007, Android Development Community
All rights reserved.
Powered by phpBB.
|