我开发了一个用于以加密形式上载文件的已签名小程序。 我从jsp调用的这个小程序工作正常,但我的问题是: 我可以从jsp调用该小程序以这种方式返回jsp中的加密文件,并将该文件传递给服务器端? 我可以在applet或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>
您可以设置一个接受文件内容流并返回文件内容流的servlet。 – 2012-07-30 13:39:42