Java - php
16/04/2018 12:08
Hi i want to send a file to php (with post) and i also want to set avariable in the same form. example (&myvar= + myvar).


I have gotten the first piece to work but i dont know how to set thevariable under the same session, any help would be appriciated!
Here is the send file metod (that i want to extend to include settingthe variable)
public void postFile(String exsistingFileName) { try { FileInputStream fileInputStream = new FileInputStream( new File(exsistingFileName) );
// Open a HTTP connection to the URL System.out.println(Connecting to server...);
conn = (HttpsURLConnection) (new URL(urlString)).openConnection();
// Allow Input/Output conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod(POST); conn.setRequestProperty(Connection, Keep-Alive); conn.setRequestProperty(Content-Type,multipart/form-data;boundary=+boundary);
dos = new DataOutputStream(conn.getOutputStream() ); if (!SecureConnection.isSecure(conn)) { System.out.println(Connection not secure!); return; }
System.out.println(Sending query to server...);
dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes(Content-Disposition: form-data;name=datafile; + filename= + exsistingFileName + + lineEnd); dos.writeBytes(lineEnd);
// create a buffer of maximum size bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize];
// read file and write it into form...

bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) { dos.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); }
// send multipart form data necesssary after file data...

dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); dos.flush();
//----------------------- i guess i should set the var before i closethe streams//----------------------- how? i dont know
// close streams fileInputStream.close(); dos.flush(); dos.close(); } catch (MalformedURLException ex) { System.out.println(From ServletCom CLIENT REQUEST:+ex); } catch (IOException ioe) { System.out.println(From ServletCom CLIENT REQUEST:+ioe); }
//read the server response try { BufferedReader br = new BufferedReader(newInputStreamReader(conn.getInputStream())); String str;
while (( str = br.readLine()) != null) { System.out.println(Server response is: +str); System.out.println(); } br.close(); System.out.println(Cosed connnection to server.); } catch (IOException ioex) { System.out.println(From (ServerResponse): +ioex); } }

Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
16/04/2018 12:08 - <allanallansson@spray.se> wrote in message news:1152247213.895409.296870@p79g2000cwp.googlegroups.com...


Take your original code, and change the beginning from:

<oldCode>public void postFile(String exsistingFileName) {try { FileInputStream fileInputStream = new FileInputStream( new File(exsistingFileName) );
// Open a HTTP connection to the URL System.out.println(Connecting to server...);
conn = (HttpsURLConnection) (new URL(urlString)).openConnection();</oldCode>
to
<newCode>public void postFile(String exsistingFileName) {try { FileInputStream fileInputStream = new FileInputStream( new File(exsistingFileName) );
// Open a HTTP connection to the URL System.out.println(Connecting to server...);
conn = (HttpsURLConnection) (new URL(urlString + ?myVar=+myVar+&myVar2=+myVar2)).openConnection();</newCode>
This assumes that urlString didn't contain a question mark already.


- Oliver

Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
16/04/2018 12:08 - If you could explain that with a code snippet i would be most pleased!
Thank you in advance, Allan

Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
16/04/2018 12:08 - <allanallansson@spray.se> wrote in message news:1152181891.972424.318880@q16g2000cwq.googlegroups.com...


You can send arbitrary data via a POST request. It's also possible to send data via both POST and GET simultaneously. Simply POST to a GET url. E.g. POST to http://foo.com/bar?getVariable=value
- Oliver

Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
16/04/2018 12:08 - Thank you for all your help... :)
It works great now!
Oliver Wong skrev:


Source is Usenet: comp.lang.java.help
Sign in to add a comment

eDiscover
Helpforce eDiscover provides technical articles updated each dayHelpforce eDiscover RSS feed contains the latest technical articles in RSS
Click the logo to go back to the main page
Search eDiscover
  
Categories

Click an icon to go to that category

Helpforce eDiscover contains articles about Microsoft Windows Helpforce eDiscover contains articles about Apple products and MacOS Helpforce eDiscover contains articles about Linux and POSIX operating systems Helpforce eDiscover contains articles about Helpforce Helpforce has a large variety of technical information and articles for you to read Helpforce eDiscover contains articles about databases, MYSQL, SQL Server Oracle Helpforce eDiscover contains articles about Java, JVM and the JRE Helpforce eDiscover contains articles about the QNX operating system Helpforce eDiscover contains articles about Oracle Solaris and Open Solaris Helpforce eDiscover contains articles about RISC OS, Acorn and the BBC Micro Helpforce eDiscover contains articles about Amiga and AmigaOS

Type your comment into the box below