我正在尝试加密文件,然后上传到Dropbox。 以下是代码:
private void encryptFile(String FileName) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException, DbxException {
// TODO Auto-generated method stub
String key = "Mary has one cat";
// char[] hex = encodeHex(SecKey.getEncoded());
//String key = String.valueOf(hex);
System.out.println(key);
Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
FileInputStream inputStream = new FileInputStream(FileName);
byte[] inputBytes = new byte[(int) FileName.length()];
inputStream.read(inputBytes);
byte[] outputBytes = cipher.doFinal(inputBytes);
FileOutputStream outputStream = new FileOutputStream(FileName);
outputStream.write(outputBytes);
inputStream.close();
outputStream.close();
uploadToDropbox(FileName);
}
public void uploadToDropbox(String fileName) throws DbxException,
IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
FileInputStream fis = new FileInputStream(fileName);
try {
DbxEntry.File uploadedFile = dbxClient.uploadFile("/" + fileName,
DbxWriteMode.add(), fileName.length(), fis);
String sharedUrl = dbxClient.createShareableUrl("/" + fis);
System.out.println("Uploaded: " + uploadedFile.toString() + " URL "
+ sharedUrl);
} finally {
fis.close();
}
}
我收到以下错误:
线程“main”中的异常java.lang.IllegalStateException:你说 你要上传12个字节,但你写了16个字节 上传者的“身体”流。
答案 0 :(得分:0)
在这一行中,您传递的第三个参数应该是您上传的数据的长度,而不是您传递文件名的长度。
DbxEntry.File uploadedFile = dbxClient.uploadFile("/" + fileName,
DbxWriteMode.add(), fileName.length(), fis);
尝试这样的事情:
DbxEntry.File uploadedFile = dbxClient.uploadFile("/" + fileName,
DbxWriteMode.add(), new File(fileName).length(), fis);
答案 1 :(得分:-1)
DbxEntry.File uploadedFile = dbxClient.uploadFile("/" + fileName, DbxWriteMode.add(), fileName.length(), fis);
fileName.length()
不正确,应为file length
。