You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "guy.smadja@optic-box.com" <gu...@optic-box.com> on 2015/01/13 19:27:11 UTC

HTTP Server Side: How to Upload File with package httpserver

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