问题
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
回答1:
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
来源:https://stackoverflow.com/questions/6767273/receive-output-from-java-dataoutputstream-in-a-php-page