1

I am working on an applet that records voice and uploads to a servlet.

Here is the code of the upload thread in the applet

 class uploadThread extends Thread {

    @Override
    public void run() {
        try {
            //Preparing the file to send
            AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
            File file = File.createTempFile("uploded", ".wav");

            byte audio[] = out.toByteArray();
            InputStream input = new ByteArrayInputStream(audio);
            final AudioFormat format = getFormat();
            final AudioInputStream ais = new AudioInputStream(input, format, audio.length / format.getFrameSize());
            AudioSystem.write(ais, fileType, file);

            //uploading to servlet

        FileInputStream in = new FileInputStream(fileToSend);
        byte[] buf = new byte[1024];
        int bytesread = 0;

        String toservlet = "http://localhost:8080/Servlet/upload";

        URL servleturl = new URL(toservlet);
        URLConnection servletconnection = servleturl.openConnection();
        servletconnection.setDoInput(true);
        servletconnection.setDoOutput(true);
        servletconnection.setUseCaches(false);
        servletconnection.setDefaultUseCaches(false);

        DataOutputStream out = new DataOutputStream(servletconnection.getOutputStream());

        while ((bytesread = in.read(buf)) > -1) {
            out.write(buf, 0, bytesread);
        }

        out.flush();
        out.close();

        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Error during upload");
        }
    }
}//End of inner class uploadThread

Here is the code of the grab file method in the servlet:

    java.io.DataInputStream dis = null;
    try {
        int fileLength = Integer.valueOf(request.getParameter("fileLength"));
        String fileName = request.getParameter("fileName");

        dis = new java.io.DataInputStream(request.getInputStream());
        byte[] buffer = new byte[fileLength];

        dis.readFully(buffer);
        dis.close();
        File cibleServeur = new File("/Users/nebrass/Desktop/" + fileName);
        FileOutputStream fos = new FileOutputStream(cibleServeur);
        fos.write(buffer);
        fos.close();
    } catch (IOException ex) {
        Logger.getLogger(UploadServlet.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            dis.close();
        } catch (Exception ex) {
            Logger.getLogger(UploadServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

I have created a certificate with the keytool. And i have signed the JAR of the applet. I have added the applet to the jsp file and it is working, and have the all permissions (I tried to save a file on a desktop using the applet)

Update: The problem is that the file is not sent, and when i try to debug the servlet, it is not invoked by the the applet. Please help

Jason Bourne
  • 756
  • 1
  • 14
  • 34
  • Related: http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests/ – BalusC Oct 02 '13 at 20:29

2 Answers2

2

That's not how it works. You've just opened a URLConnection and wrote to the output stream. That way you're assuming something like a socket connection, but here we need more of a HttpUrlConnection and then a request-parameter and a multi-part request.

Google Search

Google found lots of solutions, but for the completeness of the answer, I'm adding one below :

https://stackoverflow.com/a/11826317/566092

Community
  • 1
  • 1
coding_idiot
  • 13,526
  • 10
  • 65
  • 116
0

You want up upload a file from the server to the user desktop?

I doubt this will be allowed, for obvious security reasons.

Why don't you just call the servlet directly from the browser? And "save as" the file?

Here is an exemple on how to send a file (any type) from a servlet.

 protected void doPost(
    ...
        response.setContentType("your type "); // example: image/jpeg, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/octet-stream
                    response.setHeader("Content-Disposition","attachment; filename=\"your_filename\"");
                    File uploadedFile = new File("/your_file_folde/your_file_name");
                    if (uploadedFile.exists()){
                        FileUtils.copyFile(uploadedFile, response.getOutputStream());
                    }
    else { // Error message
    }
....
    }
Cedric Simon
  • 4,571
  • 4
  • 40
  • 52
  • The upload is from the applet to the servlet. The save on disk is an other method of the applet and it is working perfectly – Jason Bourne Oct 02 '13 at 15:05