3

実際に私は次のコードを持っています:

String f = LauncherFrame.class.getProtectionDomain().getCodeSource().getLocation().getPath(); // path to launcher
java.lang.System.out.println(f);

String launcherHash = "";

try{
  MessageDigest md5  = MessageDigest.getInstance("MD5");
  launcherHash = calculateHash(md5, f);
}catch (Exception e) {
  java.lang.System.out.println(e){
  return;
}

calculateHash関数:

public static String calculateHash(MessageDigest algorithm,String fileName) throws Exception{
      FileInputStream    fis = new FileInputStream(fileName);
      BufferedInputStream bis = new BufferedInputStream(fis);
      DigestInputStream  dis = new DigestInputStream(bis, algorithm);

      while (dis.read() != -1);
            byte[] hash = algorithm.digest();

      return byteArray2Hex(hash);
}

.jarファイルのパスにキリル文字が含まれていない場合は、unix/windowsで正常に機能します。しかし、それがある場合、次の例外が発生します。

java.io.FileNotFoundException: 
C:\Users\%d0%90%d1%80%d1%82%d1%83%d1%80\Documents\NetBeansProjects\artcraft-client\build\classes (Can't find file)

どうすれば修正できますか?

4

1 に答える 1

2

これはメモリからのものであるため、構文が少しずれている可能性がありますが、URLから直接ストリームを開くために組み込みのURLサポートを使用するのがおそらく最善です。

URL url = LauncherFrame.class.getProtectionDomain().getCodeSource().getLocation();

calculateHash次に、ファイル名の代わりにURLを渡し、URLのopenStreamメソッドを使用してコンテンツを含むストリームを取得します。

InputStream is = url.openStream();
BufferedInputStream bis = new BufferedInputStream(is);
...
于 2012-09-06T21:00:22.460 に答える