当前位置: 动力学知识库 > 问答 > 编程问答 >

java : upload files to HTTP server using POST, server code issue?

问题描述:

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

网友答案:

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 ! *

网友答案:

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

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

网友答案:

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).

分享给朋友:
您可能感兴趣的文章:
随机阅读: