証明書ファイルを別のマシンに転送するための 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());
}
}
}