hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "guy.smadja@optic-box.com" <guy.sma...@optic-box.com>
Subject HTTP Server Side: How to Upload File with package httpserver
Date Tue, 13 Jan 2015 18:27:11 GMT
Hello

I have an applet that upload a file choosen by the user:
/**********************************************/
                                               URL url = new URL("http://192.168.0.1/put");
                                               conn = (HttpURLConnection) url.openConnection();
                                               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());
                                               dos.writeBytes(twoHyphens + boundary + lineEnd);
                                               dos.writeBytes(
                                                               "Content-Disposition: form-data;
name=\"" + parameterName + "\";"
                                                                              + " filename=\""
                                                                              + fileName
                                                                              + "\""
                                                                              + 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);

                                               if(extraParams != null) {
                                                               for(Enumeration enum = extraParams.keys();
enum.hasMoreElements(); ) {
                                                                              String param
= (String)enum.nextElement();
                                                                              String value
= extraParams.getProperty(param);
                                                                              dos.writeBytes(twoHyphens
+ boundary + lineEnd);
                                                                              dos.writeBytes(
                                                                                         
    "Content-Disposition: form-data; name=\"" + param + "\";" + lineEnd + lineEnd + value
+ lineEnd);
                                                               }
                                               }
                                               dos.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
                                               // close streams
                                               fileInputStream.close();
                                               dos.flush();
                                               dos.close();

                                               int httpStatus = conn.getResponseCode();
/********************************/

On the server side
/********************************/
    server = HttpServer.create(new InetSocketAddress(port), 0);
    server.createContext("/put", new PutHandler());
/********************************/

How should I implement PutHandler to store the file on the server ?

Thanks,

Guy Smadja
Opticemarket.com
http://www.optic-box.com
Tel : 01 76 71 08 32


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message