我已经开发了一个签名的applet,用于以加密形式上传文件。
我从jsp调用此小程序,效果很好,但是我的问题是:
我可以从jsp调用该applet吗,它将返回jsp中的加密文件并将该文件传递到服务器端?
我可以在applet或jsp中为该加密文件创建多部分文件并将其发送到服务器吗?
我正在运行的Applet看起来像:
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()方法,您可以在其中检索文件。如您所说,您的小程序应该经过签名才能执行此操作。