将文件从applet上传到servlet有问题

时间:2013-10-02 14:40:11

标签: java servlets file-upload browser applet

我正在开发一个记录语音和上传到servlet的applet。

以下是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

以下是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);
        }
    }

我用keytool创建了一个证书。我已经签署了applet的JAR。 我已将applet添加到jsp文件中,它正在运行,并具有所有权限(我尝试使用applet在桌面上保存文件)

更新:问题是文件没有发送,当我尝试调试servlet时,applet不会调用它。 请帮忙

2 个答案:

答案 0 :(得分:2)

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

Google Search

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

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

答案 1 :(得分: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
    }
....
    }