Note that there are some explanatory texts on larger screens.

plurals
  1. POUpload and POST file to PHP page with Java
    text
    copied!<p>I need a way to upload a file and POST it into php page...</p> <p>My php page is:</p> <pre><code>&lt;?php $maxsize = 10485760; $array_estensioni_ammesse=array('.tmp'); $uploaddir = 'uploads/'; if (is_uploaded_file($_FILES['file']['tmp_name'])) { if($_FILES['file']['size'] &lt;= $maxsize) { $estensione = strtolower(substr($_FILES['file']['name'], strrpos($_FILES['file']['name'], "."), strlen($_FILES['file']['name'])-strrpos($_FILES['file']['name'], "."))); if(!in_array($estensione, $array_estensioni_ammesse)) { echo "File is not valid!\n"; } else { $uploadfile = $uploaddir . basename($_FILES['file']['name']); echo "File ". $_FILES['file']['name'] ." uploaded successfully.\n"; if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully moved.\n"; } else print_r($_FILES); } } else echo "File is not valid!\n"; } else { echo "Upload Failed!!!"; print_r($_FILES); } ?&gt; </code></pre> <p>and i use this java code in my desktop application:</p> <pre><code>HttpURLConnection httpUrlConnection = (HttpURLConnection)new URL("http://www.mypage.org/upload.php").openConnection(); httpUrlConnection.setDoOutput(true); httpUrlConnection.setRequestMethod("POST"); OutputStream os = httpUrlConnection.getOutputStream(); Thread.sleep(1000); BufferedInputStream fis = new BufferedInputStream(new FileInputStream("tmpfile.tmp")); for (int i = 0; i &lt; totalByte; i++) { os.write(fis.read()); byteTrasferred = i + 1; } os.close(); BufferedReader in = new BufferedReader( new InputStreamReader( httpUrlConnection.getInputStream())); String s = null; while ((s = in.readLine()) != null) { System.out.println(s); } in.close(); fis.close(); </code></pre> <p>But I receive always the "Upload Failed!!!" message.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload