java - 如何从applet返回文件到jsp

标签 java javascript jsp applet

我开发了一个签名的小程序,用于以加密形式上传文件。 我从 jsp 调用的这个小程序运行良好,但我的问题是: 我可以从 jsp 调用该小程序,以便在 jsp 中返回加密文件并将该文件传递到服务器端吗? 我可以在 applet 或 jsp 中为该加密文件创建多部分文件并将其发送到服务器吗?

我正在运行的小程序看起来像:

public static void encryptDecryptFile(String srcFileName,
            String destFileName, Key key, int cipherMode) throws Exception {
        OutputStream outputWriter = null;
        InputStream inputReader = null;     
        try {                   
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");         
            byte[] buf = cipherMode == Cipher.ENCRYPT_MODE ? new byte[100]
                    : new byte[128];
            int bufl;
            cipher.init(cipherMode, key);           
            outputWriter = new FileOutputStream(destFileName);
            inputReader = new FileInputStream(srcFileName);
            while ((bufl = inputReader.read(buf)) != -1) {          
                byte[] encText = null;
                if (cipherMode == Cipher.ENCRYPT_MODE)
                    encText = encrypt(copyBytes(buf, bufl), (PublicKey) key);
                else
                    encText = decrypt(copyBytes(buf, bufl), (PrivateKey) key);              
                outputWriter.write(encText);
            }           
        } catch (Exception e) {e.printStackTrace();
            throw e;
        } finally {
            try {
                if (outputWriter != null)
                    outputWriter.close();
                if (inputReader != null)
                    inputReader.close();
            } catch (Exception e) {
            }
        }
    }

我的调用 jsp 看起来像:

<applet id="upload" name="upload" code="TestApplet.class" archive="Encrypt.jar" width="360" height="350"></applet>

最佳答案

最简单的方法是使用 HttpClient Apache Commons图书馆。您必须在小程序中执行以下操作:

    public void sendFile throws IOException {
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod("http://yourserverip:8080/yourServlet");

        File f = new File(destFileName);
       
        postMethod.setRequestBody(new FileInputStream(f));
        postMethod.setRequestHeader("Content-type",
            "text/xml; charset=ISO-8859-1");

        client.executeMethod(postMethod);
        postMethod.releaseConnection();
    }

这将触发您的 servlet doPost() 方法,您可以在其中检索文件。正如您所说,您的小程序应该经过签名才能执行此操作。

关于java - 如何从applet返回文件到jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11722392/

相关文章:

java - 无法在聚合中使用过滤器bsons : Can't find a codec for class com. mongodb.client.model.Filters$AndFilter

java - 在 Eclipse 插件中包含和访问二进制(非 Java)文件

javascript - 选择 span 标签的类名

jsp - 如何设置(点)。使用jsp在范围内分隔变量

jsp - 如何在 Struts 2 操作类中检索复选框值?

java - 如何将从两个表中检索的数据存储到 java/servlet/jsp 中的另一个表中

java - 进程无法访问文件,因为该文件正在被另一个进程使用

java - 如何在带有 android 6.0.1 的 2013 nexus 7 上将 sockethandler 添加到 logcat?

javascript - 重定向视频完成网页

javascript - 我如何对重复的数组元素求和