I am looking for a Java applet to read a file from client machine and creat a POST request for PHP server uploading.
PHP script on server should receive the file as normal file upload in FORM submit.
I am using the following code. The file contents are passed to PHP script
but they are not correctly converted to an image.
//uploadURL will be a url of PHP script like
// http://www.example.com/uploadfile.php
URL url = new URL(uploadURL);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
InputStream is = new FileInputStream("C://img.jpg");
OutputStream os = con.getOutputStream();
byte[] b1 = new byte[10000000];
int n;
while((n = is.read(b1)) != -1) {
os.write("hello" , 0, 5);
test += b1;
}
con.connect();
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…