0

I have a java applet which I am using to send a file back to my server with - on the server end I want to receive this on a php page.

Below is the java code which is doing the sending, on the php side of things I have checked the global arrays and I have the data passed by the URL, but not the file data. I have really searched and scratched on this one so any help appreciated.

String strURL = sendToURL + "?ACTION=POST&LEN=" + imgBytes.length + "&Fname=picture.png";
    try{
        URL urlServlet = new URL(strURL);
        URLConnection sCon = urlServlet.openConnection();
        sCon.setDoInput(true);
        sCon.setDoOutput(true);
        if (sCon.getAllowUserInteraction()) {
            sCon.setAllowUserInteraction(true);
        }
        sCon.setUseCaches(false);
        sCon.setDefaultUseCaches(false);
        sCon.setRequestProperty("Content-Type", "text/html");
        sCon.setRequestProperty("Connection", "Keep-Alive");
        sCon.setConnectTimeout(transferTimeout);
        sCon.setReadTimeout(transferTimeout);
        DataOutputStream out = new DataOutputStream(sCon.getOutputStream());

        int index = 0;
        size = 1024;
        do {
            if (index + size > imgBytes.length) {
                size = imgBytes.length - index;
            }
            out.write(imgBytes, index, size);
            index += size;
        } while (index < imgBytes.length);

        out.write(imgBytes);
        out.flush();
        out.close();

SOLVED - as so often happens one posts a question after days of battling and mere minutes later a solution presents.

I got thinking after the comment about using SOAP that I remembered using cURL for transferring XML data once before. a few searches later and I came across a much simpler and very elegant solution.

http://www.lornajane.net/posts/2008/Accessing-Incoming-PUT-Data-from-PHP

basically you can access the PUT data in php by using

file_get_contents("php://input")

so now it works awesomely

2
  • i don't think php is able to do what you want Jul 20, 2011 at 19:20
  • That's what I was worried about - what I was considering is to somehow encode the file and then send it as part of the URL. Unfortunately I am very new to java so am not sure how to accomplish that either.
    – l0ft13
    Jul 20, 2011 at 19:22

1 Answer 1

0

i used a lot of times Soap Messages to get Data From PHP to Java that works fine

So Use PHP as Webservice and Communicate via SOAP

Setup a WSDL File

Generate Java Stubs and Skeletons http://download.oracle.com/javase/6/docs/technotes/tools/share/wsimport.html

Load WSDL into a php skript http://www.php.net/manual/de/book.soap.php

   $soapClient = new SoapClient("blahblah.wsdl"); 

And do your logic in the php

Then use the Java Stubs to call the server and transmit your data

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.