I have to delete and rename the files in doPost.but while executing some of the files are deleting some othes are not. When the same code i run in java the operation is succesfully carried out.here is the code i used for deleating files inside a directory.The same below code is i used in 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 code:
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);
}
It could be that your static methods are being accessed by multiple Servlet Threads at once.
You can make the
saveFile()andupdateRootFile()synchronized to prevent being accesed by multiple threads.