0

我编写了一个 java 套接字程序来将证书文件传输到不同的机器,我能够传输文件,但是在打开证书时我收到一个错误提示,比如Invalid public key security object file这可能是什么原因,即使我比较使用winMerge工具传输文件的原始文件(两者相同)

请帮我解决这个问题。

客户

private void transferCarsCertificates()
{
    final File carsCertificatesFolder = new File("D:\\Cordys\\Admin Repository Server\\signedbuild8\\certificates");
    final File[] certFiles = carsCertificatesFolder.listFiles(new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) 
        {
            if(name.lastIndexOf(".")>0)
            {
                int extention = name.lastIndexOf(".");
                if(name.substring(extention).equals(".cer") && !name.contains(ServerId))
                {
                    return true;
                }
            }               
            return false;
        }
    });
    if(certFiles!=null)
    {
        int read = 0;
        int bytesRead = 0;
        byte[] fileBytes;
        InputStream fileInputStream = null;
        for(int i=0;i<certFiles.length; i++)
        {
            final Communicator communicator = new Communicator(Communicator.FILETRANSFER, "CARS_CERT");
            communicator.setFileName(certFiles[i].getName());
            communicator.setFileSize(certFiles[i].length());        
            fileBytes = new byte[(int)certFiles[i].length()];
            try 
            {
                fileInputStream = (InputStream)new FileInputStream(certFiles[i]);

                while (read < fileBytes.length && (bytesRead = fileInputStream.read(fileBytes, read, fileBytes.length - read)) >= 0) 
                {
                    read = read + bytesRead;
                }
                communicator.setFileData(fileBytes);
                client.sendMessage(communicator);
                logger.info(certFiles[i].getName()+" transfered sucessfully");
            } 
            catch (FileNotFoundException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    else
    {
        logger.error("CARS Certificates: "+certFiles+" at location: "+carsCertificatesFolder.getAbsolutePath());
        sendMessage(new Communicator(Communicator.MESSAGE, "FAILED_TO_FIND_THE_CARS_CERTIFICATES"));
        disconnect();
    }
}
private void reciveFiles(final Communicator communicator)
{
    final String message = communicator.getMessage();
    final String destination = message.contains("CARS_CERT") ? destinationDir : destinationDir;
    final File cert = new File(destinationDir+File.separator+communicator.getFileName());
    if(cert.exists())
    {
        cert.setWritable(true);
        cert.delete();
    }
    try 
    {
        cert.createNewFile();
        final OutputStream outPutStream = (OutputStream) new FileOutputStream(cert);
        outPutStream.write(communicator.getFileData(), 0, (int)communicator.getFileSize());
        logger.info(communicator.getFileName()+" is placed successfullt at destination: "+destination);
    }
    catch (IOException ioException)
    {
        logger.error("Destination Dir: "+destinationDir, ioException);
    }       
}

服务器端

 private synchronized void broadcastFiles(String clientId, Communicator communicator)
{
    final String time = simpleDateFormat.format(new Date());
    final Set<Entry<String, ClientThread>> set = listMap.entrySet();
    final Iterator<Entry<String, ClientThread>> iterator = set.iterator();
    while(iterator.hasNext())
    {
        final Entry<String, ClientThread> entry = iterator.next();
        if(entry.getKey()!=clientId)
        {
            final ClientThread clientThread =  entry.getValue();
            if(!clientThread.transferFiles(communicator))
            {
                listMap.remove(clientThread);
                display("Disconnected Client " + clientThread.id + " removed from list.");
            }
            logger.info(time+"File broadcasted: "+communicator.getFileName());
        }
    }
}
4

1 回答 1

0

我可以通过更改代码来解决问题

while (read < fileBytes.length && (bytesRead = fileInputStream.read(fileBytes, read, fileBytes.length - read)) >= 0) 
            {
                read = read + bytesRead;
            }

while (bufferedInputStrem.read(fileBytes, 0, (int)certFiles[i].length()) >= 0) {}
于 2013-07-17T05:54:42.163 回答