2016-09-19 33 views
0

我有這樣的代碼:使用Java ByteArrayInputStream的使用文件

public void uploadToFTP(File file) { 

    try { 
     final ByteArrayInputStream stream = new ByteArrayInputStream(FileUtils.readFileToByteArray(file)); 
     String date = dateFormat.format(new Date()); 
     String filename = date.replaceAll(":", "-"); 
     sessionFactory.getSession().write(stream, "dir/" + filename + ".txt"); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

我在這種情況下File我要上傳到一些FTP得到的參數,但每次我這樣做,該文件的問題實際上是空的。當我嘗試例如final ByteArrayInputStream stream = new ByteArrayInputStream("Text here".getBytes());它工作正常,並將信息存儲在文件中,這裏可能是什麼問題,我可能有問題可能將File轉換爲字節或?

+0

你確定輸入的文件是不是空的?順便說一句,你可以使用'final InputStream is = new BufferedInputStream(new FileInputStream(file))'而不需要讀取內存中的整個文件。 – ike3

+0

是的,輸入不是空的,但讓我試試你的建議 – imoteb

回答

0

使用thsi代碼:

public List<ProcessedFile> uploadFTPFilesByCridational(List<ProcessedFile> processedFiles, String sourceDir, 
      String destinationPath, String hostName, String userName, String password, String portNo, String extation, 
      int fileHours, int fileMint) { 
     List<ProcessedFile> processedFilesList = new ArrayList<>(); 
     try { 
      FTPClient ftpClient = new FTPClient(); 

      // client FTP connection 
      ftpClient = connectToFTPClient(hostName, userName, password, portNo); 

      // check if FTP client is connected or not 
      if (ftpClient.isConnected()) { 

       if (processedFiles != null && processedFiles.size() > 0) { 
        for (ProcessedFile processedFile : processedFiles) { 
         FileInputStream inputStream = null; 
         try { 
          File file = new File(sourceDir + "/" + processedFile.getOriginalFileName()); 
          inputStream = new FileInputStream(file); 

          if (!ftpClient.isConnected()) { 
           ftpClient = connectToFTPClient(hostName, userName, password, portNo); 
          } 

          ByteArrayInputStream in = null; 
          try { 
           ftpClient.changeWorkingDirectory(destinationPath); 
           ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
           ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE); 
           ftpClient.enterLocalPassiveMode(); 
           in = new ByteArrayInputStream(FileUtils.readFileToByteArray(file)); 
           ftpClient.storeFile(file.getName(), in); 
          } catch (Exception e) { 
           logger.error(e.getMessage()); 
          } 

          inputStream.close(); 
          in.close(); 

          processedFile.setProcessedStatus(ProcessedStatus.COMPLETED); 
          processedFilesList.add(processedFile); 
         } catch (Exception e) { 
          logger.error(e); 
          processedFile.setProcessedStatus(ProcessedStatus.FAILED); 
          processedFilesList.add(processedFile); 
         } 
        } 
       } 
      } 
      if (ftpClient.isConnected()) { 
       try { 
        ftpClient.logout(); 
        ftpClient.disconnect(); 
       } catch (IOException e) { 
        logger.error(e.getMessage()); 
       } finally { 
        try { 
         ftpClient.disconnect(); 
        } catch (Exception e) { 
         logger.error(e.getMessage()); 
        } 
       } 
      } 
     } catch (Exception e) { 
      logger.error("FTP not connected exception: " + e); 
     } 
     return processedFilesList; 
    }