2015-12-05 4 views
1

Я пытаюсь зашифровать файл, а затем загрузить в Dropbox. Ниже приведен код:Загрузите документ в 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(); 

    } 
} 

Я получаю следующее сообщение об ошибке:

Exception in thread "main" java.lang.IllegalStateException: You said you were going to upload 12 bytes, but you wrote 16 bytes to the Uploader's 'body' stream.

ответ

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); 
+0

Привет, Маркс ...... Большое спасибо за вашу помощь. Я получил ошибку .... –

-1
DbxEntry.File uploadedFile = dbxClient.uploadFile("/" + fileName, DbxWriteMode.add(), fileName.length(), fis); 

fileName.length() не является правильным, оно должно быть file length.

Смежные вопросы