0
文字列の変換について
以下はJavaScriptからイメージファイルのパスを受け取り「\\」の部分を「/」に変換後imageFile.exists()でそのファイルが存在するかを調べてからファイルを削除するものですが結果がexists()の結果が「false」しか返ってきません。
しかしHTML側で戻り値「imageFilePath」の中身を見てみると「\\」の部分は「/」に変換されています。
また直接JavaScript側でremoveThumbnailImage()を呼び出す時にremoveThumbnailImage("C:/Users/Admin/workspace/website/images/test.jpg")の様に直接パスを指定又はJava側でimageFilePath = "C:/Users/Admin/workspace/website/images/test.jpg"の様にすると
.exists()では「true」を返してきます。
どうすれば解決するでしょうか?
public static String removeThumbnailImage(String imageFilePath){
//// 受け取ったパスの「\\」を「/」に変換するが「false」しか返ってこない
//imageFilePath = imageFilePath.replaceAll("\\\\", "/");
////以下の場合は「true」を返す(\\はバックスラッシュ2つ)
//imageFilePath = "C:\\Users\\Admin\\workspace\\website\\images\\test.jpg";
//imageFilePath = "C:/Users/Admin/workspace/website/images/test.jpg";
//StringBuffer sb = new StringBuffer();
////以下の処理でも結果は「false」
/*StringBuffer sb = new StringBuffer();
for(int i=0;i<imageFilePath.length();i++){
switch(imageFilePath.charAt(i)){
case '\\': sb.append("/"); break;
default : sb.append(imageFilePath.charAt(i));
}//end switch
}//end for*/
/////画像を保存したファイルまでのパスでFileオブジェクト生成
File imageFile = new File(sb.toString());
try{
////イメージファイルが存在するか調べる
if(imageFile.exists()){
////イメージファイルを削除
//imageFile.delete();
return "true";
}else{
return imageFilePath;
}//end if
}catch(Exception e){
e.printStackTrace();
}//end try
}