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

4

2 回答 2

2

这不是它的工作原理。您刚刚打开URLConnection并写入了输出流。这样你就假设像一个套接字连接,但在这里我们需要更多的一个HttpUrlConnection,然后是一个请求参数和一个多部分请求。

谷歌搜索

谷歌找到了很多解决方案,但为了答案的完整性,我在下面添加一个:

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

于 2013-10-02T20:06:38.593 回答
0

您想将文件从服务器上传到用户桌面吗?

出于明显的安全原因,我怀疑这是否会被允许。

为什么不直接从浏览器调用 servlet?并“另存为”文件?

这是一个关于如何从 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
    }
....
    }
于 2013-10-02T14:52:51.210 回答