0

I have an applet which records sound and then upload it on the server. Sound recording portion is done, but I am stuck on upload portion. My applet is integrated with vaadin application. I have successfully made request responce cycle with my applet and vaadin server side. But I am unable to send any data over its output stream. My code is :

            connection = ( HttpURLConnection ) new URL( "http://localhost:8080/CheckResponse/" ).openConnection();
            connection.setRequestMethod( "POST" );  
            connection.setDoOutput( true );  
            connection.setDoInput(true);
            connection.addRequestProperty("applet", "sound recording applet");
            //CREATE A WRITER FOR OUTPUT  
            //outWriter = new PrintWriter( connection.getOutputStream() );  
            OutputStreamWriter outWriterS = new   OutputStreamWriter(connection.getOutputStream());
            buff.append( "param1=" );   
            buff.append( URLEncoder.encode( "Param 1 Value", "UTF-8" ) );  
            buff.append( "&" );  
            buff.append( "param2=" );   
            buff.append( URLEncoder.encode( "Param 2 Value", "UTF-8" ) );  

            outWriterS.write(buff.toString());
            outWriterS.flush();
            outWriterS.close();

            //RESPONSE STREAM  
            serverResponse = new BufferedReader( new InputStreamReader( connection.getInputStream() ) );  

            //READ THE RESPOSNE  
            String line2;  
            while ( (line2 = serverResponse.readLine() ) != null )   
            {  
            System.out.println( line2 );  
            }  

On the server side code :

  public void onRequestStart(HttpServletRequest request,
        HttpServletResponse response) {
    // TODO Auto-generated method stub
    try {
        if(request.getHeader("applet")!=null){
            System.out.println("Request started by applet");
        if(request.getHeader("applet").equalsIgnoreCase("sound recording applet")){
            System.out.println("Reading the streams");
        InputStreamReader reader = new InputStreamReader(request.getInputStream());

        BufferedReader buff = new BufferedReader(reader);
        System.out.println("Buffer"+buff);
        System.out.println("Buffer has :"+buff.readLine());
        }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //System.out.println("Request started");

}

the bufferReader.readLine() is returning null. What could be the reason? Thanks

Muhammad Salman Farooq
  • 1,325
  • 9
  • 26
  • 54

2 Answers2

0

You are using bufferReader.readLine() to read the stream from your applet client client yet don't appear to be sending any newline characters. Try using:

outWriterS.write(buff.toString() + "\n");
Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

Without specifically analyzing your code here, I don't think Vaadin very well supports this kind of approach. So maybe the right way would be to have a separate servlet along with the Vaadin application. (Vaadin application is effectively a servlet itself anyway). This way you can use the standard HTTP Request/Response handling.

Furthermore, as you are dealing with potentially big files, you probably use multipart data. See for example How can I make a multipart/form-data POST request using Java?

Community
  • 1
  • 1
eeq
  • 2,108
  • 12
  • 21