Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

java to php

 
punsungkin
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys
I want to use java application to do a http upload. the server side is using php. I have tried to change all the http message from the java application exactly same as from a html form. but the php program still cannot recongnize the content and create php varible. when I use html form, the php program works well.
please help me to check.
thanks !
the java application code
import java.net.*;
import java.io.*;
public class urlwrite {
public static void main(String args[]) {
try {
URL myurl=new URL("http://somehost.com/~user/upload.php");
// URL myurl=new URL("http://127.0.0.1:6789");
URLConnection uc=myurl.openConnection();
uc.setAllowUserInteraction(true);
uc.setDoInput(true);
uc.setDoOutput(true);
uc.setRequestProperty("Accept","image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, */*");
uc.setRequestProperty("Accept-Language","en-au");
uc.setRequestProperty("Content-Type","multipart/form-data; boundary=---------------------------7d212137302a");
uc.setRequestProperty("Accept-Encoding","gzip, deflate");
uc.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
uc.connect();
OutputStream out=uc.getOutputStream();
InputStream in=uc.getInputStream();
String a3="---------------------------7d212137302a";
String a4="Content-Disposition: form-data; name=\"testfield\"";
String a5="abc";
String a6="---------------------------7d212137302a";
String a7="Content-Disposition: form-data; name=\"filefield\" filename=\"d:/javawork/test/urlwrite/test1.bmp.bmp\"";
String a8="Content-Type: image/bmp";
String a9="---------------------------7d212137302a--";
byte[] b3=a3.getBytes();
byte[] b4=a4.getBytes();
byte[] b5=a5.getBytes();
byte[] b6=a6.getBytes();
byte[] b7=a7.getBytes();
byte[] b8=a8.getBytes();
byte[] b9=a9.getBytes();
int a;
out.write(b3);
out.write('\r');out.write('\n');
out.write(b4);
out.write('\r');out.write('\n');
out.write('\r');out.write('\n');
out.write(b5);
out.write('\r');out.write('\n');
out.write(b6);
out.write('\r');out.write('\n');
out.write(b7);
out.write('\r');out.write('\n');
out.write(b8);
out.write('\r');out.write('\n');
out.write('\r');out.write('\n');
FileInputStream fi=new FileInputStream(new File("d:/javawork/test/urlwrite/test1.bmp"));
int c;
while((c=fi.read())!=-1)
out.write(c);
out.write('\r');out.write('\n');
out.write(b9);
out.write('\r');out.write('\n');
out.flush();
while((a=in.read())>-1) {
System.out.print((char)a);
}
// for(int i=0;i<=10;i++)
// System.out.println(uc.getHeaderField(i));
in.close();
out.close();
} catch(Exception e) {System.out.println(e);}
}
}
the upload.php code
[?php
if(isset($testfield))
{
echo("something[p]");
}
else {
echo("nothing![p]");
}
if(isset($filefield))
{
echo("Filename:".$filefield_name."[p]");
echo("Filesize:".$filefield_size."[p]");
echo("file found[p]");
}
else {
echo("no file[p]");
}
?]
the html form to compare with
[form enctype="multipart/form-data" action="http://somehost.com/~user/upload.php" method="POST"]
[input type="text" name="testfield"][p]
[input type="file" name="filefield"][p]
[input type="submit"]
[/form]
 
No prison can hold Chairface Chippendale. And on a totally different topic ... my stuff:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!