3
我必须删除并重命名doPost中的文件,但是在执行某些文件时删除了一些不属于的文件。当我在java中运行相同的代码时,操作成功执行。这里是我用于删除目录内文件的代码。下面的代码也是我在servlet中使用的代码。在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);
}
怎么可能。 – chinchu 2011-12-22 07:15:01
如何在这些方法中使用sysnchorized – chinchu 2011-12-22 07:16:12
我在函数saveFile()和updateRootFile()中使用了synchoronized关键字,但它仍然不起作用 – chinchu 2011-12-22 07:37:26