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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Milorad
  • 155
  • 14
  • 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. – Héctor Feb 04 '16 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 – Milorad Feb 04 '16 at 16:36

1 Answers1

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;
Community
  • 1
  • 1
Aaron
  • 1,390
  • 1
  • 16
  • 30
  • Could you please explain little more detailed what is stored in dict ? – Milorad Feb 04 '16 at 16:34
  • 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. – Aaron Feb 04 '16 at 16:37
  • I added some code related to uploading the file and a link – Aaron Feb 04 '16 at 16:43
  • 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? – Milorad Feb 04 '16 at 16:58
  • 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` – Aaron Feb 04 '16 at 17:14
  • Thanks I will try out. Is there any solution for passing arguments such as title, not just redirect URL's? – Milorad Feb 04 '16 at 18:03
  • you can pass whatever you want, in my original example, i passed several items (strings, ints, w/e) from server to client as json, then parsed the response client side into a json object – Aaron Feb 04 '16 at 18:07