2011-12-26 10 views
1

サーブレットを使用してディレクトリ内の複数のファイルを削除したり名前を変更したりする問題があります。サーブレットコードはiamを使用しています。ディレクトリ内の複数のファイルを削除して名前を変更する際の問題

public class SendRedirect extends HttpServlet { 



    RootSipResourceApp app =new RootSipResourceApp(); 

      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {     
         response.setContentType("text/html; charset=UTF-8");       if (strSaveFile != null) 
        app.updateRootFile(strDirectorypath, strappID, appNames); 

       RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp"); 
       dispatcher.forward(request, response); 
      } 



    } 

RootSipResource.javaファイル

public void updateRootFile(String directorypath, String appID, String[] appName) throws IOException { 
     FileInputStream fins=null; 
     try { 
        File[] listOfFiles = fileLists(directorypath); 
      for (int i = 0; i < listOfFiles.length; i++) { 
       synchronized(listOfFiles) { 
          if (listOfFiles[i].isFile()) {          
        rootFiles = listOfFiles[i].getName();           
        if (rootFiles.endsWith(".properties") || rootFiles.endsWith(".PROPERTIES")) { 
         fins = new FileInputStream(directorypath + rootFiles); 
         properties.load(new InputStreamReader(fins, Charset.forName("UTF-8"))); 
         String getAppName = properties.getProperty("root.label." + appID); 
         String propertyStr = "root.label." + appID;           
              String toUtf =new String(appName[i].getBytes("iso-8859-1"), "UTF-8") ; 
         saveFile(fins, getAppName, directorypath + rootFiles, propertyStr,toUtf); 
        }        

       } 
         } 

      } 

     } catch (Exception e) { 
      System.out.println("Exception Inside updateRootFile():: " + e); 
     } 
    } 

    public void saveFile(FileInputStream fIns, String oldAppName, String filePath, String propertyStr, String appName) 
      throws IOException { 
     FileInputStream finStream =null; 
     try {       
        String oldChar = propertyStr + "=" + oldAppName; 
      String newChar = propertyStr + "=" + appName; 
      String strLine; 
      File rootFile = new File(filePath); 
      File copyFile = new File("D:\\root\\root_created.properties"); 

        finStream = new FileInputStream(rootFile); 
        BufferedReader br = new BufferedReader(new InputStreamReader(finStream, "UTF-8")); 
       OutputStreamWriter outStream = new OutputStreamWriter(new FileOutputStream(copyFile), "UTF-8"); 
      while ((strLine = br.readLine()) != null) { 
       strLine = strLine.replace(oldChar, newChar); 
       outStream.write(strLine); 
       outStream.write("\r\n"); 
      } 
      outStream.flush(); 
      outStream.close(); 
      br.close(); 
      fIns.close(); 
        finStream.close();       
       rootFile.delete(); 
      copyFile.renameTo(rootFile);    

     } catch (IOException e) { 
      System.out.println(" Excpetion in save file--*******************---" + e); 
     } 
    } 

私はupdateRootFile内部synchorinizedキーワードを(使用)method..Butはまだそれが動作していません。..

+1

コードをフォーマットして関連コードのみを投稿してください –

+0

これを読んで実際の問題を再考してください:http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-session-variables-and-multithreading/3106909#3106909 – BalusC

答えて

0

ANで実行している場合、このコードが間違って振舞うん複数のリクエストが同時に発行される環境、またはこのメソッドを1つのスレッドで実行すると機能しない環境もあります。それをデバッグしようとした?

あなたの 'listOfFiles'はフィールドではなくメソッド内でローカルに定義された変数なので、どのような場合でも、あなたの 'synchronized'構造は無関係です。何もサーブレットの一部ではなく、むしろ不適切な同期メカニズムの使用です。

関連する問題