2016-09-19 43 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; 
    }