2

I've been trying to develop a desktop java app which can upload an image from local hard disk to my website and get a URL of a page dynamically created in PHP for that image. I came across HttpClient which can send a POST request, but has a lot of deprecated classes and I haven't seen how to get some information back from server besides HTTP status codes. Second approach which I am not familiar with and would really like to avoid is rebuilding a server side in JAVA.

Which would be the simplest solution for uploading a file via upload form (which is processed in PHP) and getting back some information like new URL of image to the JAVA application?

2
  • Actually, no matters which tecnology you use on server side, so you will exchange bytes. You can use Retrofit in your client to send files easily to your server. Commented Feb 4, 2016 at 16:17
  • It seems like a straightforward library. Is there any detailed docs about getting the response from server such as URL of uploaded image and etc Commented Feb 4, 2016 at 16:36

1 Answer 1

3

you can read the response of the POST request pretty easily (Assuming you have already uploaded the file to the server in a POST - if not check out How to upload a file using Java HttpClient library working with PHP, but i copied the critical lines here):

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
File file = new File("c:/TRASH/zaba_1.jpg");
FileEntity reqEntity = new FileEntity(file, "binary/octet-stream");
reqEntity.setContentType("binary/octet-stream");
post.setEntity(reqEntity);
HttpResponse response = httpclient.execute(post);
HttpEntity entity = response.getEntity();
Log.d("INFO", "rcode:" + response.getStatusLine().toString());
if (response.getStatusLine().getStatusCode() - 200 >= 100)
     throw new Exception("bad status code: " + response.getStatusLine().getStatusCode());
String responseString = EntityUtils.toString(entity);

oh btw this is using

 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.util.EntityUtils;
Sign up to request clarification or add additional context in comments.

7 Comments

Could you please explain little more detailed what is stored in dict ?
In your situation you should already be uploading a image in a POST, so everything up to HttpResponse response = httpclient.execute(post); you should already have. After that you can use my code to read the response sent by the server to parse the URL returned.
I added some code related to uploading the file and a link
Thank you for the answer it is more clear now. But one more thing is bugging me. On the server side I should somehow put the relevant information I need from the server in the JSON object and that should be available in the dict variable, correct?
no, on server side you should put the url that you want to client to go to in the response. Client side it will then be available in responseString
|

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.