1

I am new to java and I have to transfer file from Android application to server. I took help from the link

Uploading files to HTTP server using POST. Android SDK

<?php
$target_path  = "./";
$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!";
}
?>

Its PHP code and works perfectly fine but I have to implement the server side code in Java not in PHP.

I googled and find the code from link enter link description here

InputStream in = request.getInputStream();
        BufferedReader r = new BufferedReader(new InputStreamReader(in));
        StringBuffer buf = new StringBuffer();
        String line;
        while ((line = r.readLine())!=null) {
        buf.append(line);
        }
        String s = buf.toString();

I am new to java so donot know how to write this code. I have NetBeans netbeans-7.1.1-ml-javaee installed.

Can somebody tell me that if this code is correct and how to put it in file or which type of file. I have created project but do not know how to put this code in file.

Edits:

Andriod code is working fine ... I want to develop Server code to get and save file

3 Answers 3

5

i hope this code can help u

try {
         HttpClient client = new DefaultHttpClient();
     HttpPost post = new HttpPost(Image_url);
     MultipartEntity mpEntity = new MultipartEntity();
     File file = new File(selectedImagePath);
     ContentBody cbFile = new FileBody(file, "image/jpeg");         
     mpEntity.addPart("photo", cbFile);
     mpEntity.addPart("user_id", new StringBody(SmallyTaxiTabbar.unique_ID));
     mpEntity.addPart("password", new StringBody(SmallyTaxiTabbar.password));
     post.setEntity(mpEntity);
     HttpResponse response1 = client.execute(post);
     HttpEntity resEntity = response1.getEntity();
     String Response=EntityUtils.toString(resEntity);
     Log.d("PICTUREServer Response", Response);
     JSONArray jsonarray = new JSONArray("["+Response+"]");
     JSONObject jsonobject = jsonarray.getJSONObject(0);
     alert=(jsonobject.getString("alert"));
     client.getConnectionManager().shutdown();
    } 
     catch (Exception e) {
                Log.e("TAGPost", e.toString());     
    }

where SmallyTaxiTabbar.unique_ID, password is parameter value

*i Hope this code can help u ! *

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, it works like charm! Note Download httpcomponents-client-4.1-bin.zip from hc.apache.org/downloads.cgi and add httpclient-4.1.jar, httpcore-4.1.jar and httpmime-4.1.jar to your project.
1

check if this library helps you. I have created wrappers for several http methods..

https://github.com/nareshsvs/android-libraries

Comments

0

The Apache Commons FileUpload library should help you process the content of the POST request in Java from a server point of view. It can be integrated with Servlets if required.

In particular, it will process the multipart/form-data content for you (which you would otherwise have to do manually if you were reading the request's InputStream directly line by line).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.