删除和重命名Servlet中的多个文件时出现问题

时间:2011-12-22 06:40:13

标签: java servlets file-io

我必须删除并重命名doPost中的文件。但执行某些文件时正在删除一些不是。当我在java中运行相同的代码时,操作成功执行。我用于删除目录中的文件的代码。我在servlet中使用相同的代码。

public static void updateRootFile(String directorypath, String appID, String[] appName) throws IOException {
    try {

        FileInputStream fin = null;
        File[] listOfFiles=fileLists(directorypath);
        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                rootFiles = listOfFiles[i].getName();
                if (rootFiles.endsWith(".properties") || rootFiles.endsWith(".PROPERTIES")) {
                    fin = new FileInputStream(directorypath + rootFiles);
                    properties.load(new InputStreamReader(fin, Charset.forName("UTF-8")));
                    String getAppName = properties.getProperty("root.label." + appID);
                    String propertyStr = "root.label." + appID;
                    saveFile(fin, getAppName, directorypath + rootFiles, propertyStr, appName[i]);
                }
            }

        }

    } catch (Exception e) {
        System.out.println("expn-" + e);

    }

}

public static void saveFile(FileInputStream fins, String oldAppName, String filePath, String propertyStr, String appName)
        throws IOException {
    String oldChar = propertyStr + "=" + oldAppName;
    String newChar = propertyStr + "=" + appName;
    String strLine;
    File f1 = new File(filePath);
    File f2 = new File("C:\\Equinox\\RootSipResource\\root\\root_created.properties");      
    BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(f1), "UTF-8"));
    OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(f2), "UTF-8");
    while ((strLine = br.readLine()) != null) {
        strLine = strLine.replace(oldChar, newChar);
        out.write(strLine);
        out.write("\r\n");
    }
    out.flush();
    out.close();
    br.close();
    fins.close();
}

Servlet代码:

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import root.sip.RootSipResourceApp;

public class SendRedirect extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/html; charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
        String strDirectorypath = (String) request.getParameter("txtfileBrowse");
        request.setAttribute("directorypath", strDirectorypath);
        String strappID = request.getParameter("txtAppID");
        String[] appNames = {strEn, strAr, strBg, strCs, strDa, strDe, strEl, strEs, strFi, strFr, strHe, strHr, strHu, strIt,strLw, strJa, strKo, strNl, strNo, strPl, strPt, strRo, strRu, strSk, strSl, strSv, strTr, strZh, strZh_TW };

        RootSipResourceApp.updateRootFile(strDirectorypath, strappID, appNames);
        System.out.println("after................");
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
        dispatcher.forward(request, response);
}

1 个答案:

答案 0 :(得分:2)

可能是您的静态方法被多个Servlet线程一次访问。

您可以同步saveFile()updateRootFile()以防止多个线程加入。