gpt4 book ai didi

java - 在 Servlet 中删除和重命名多个文件的问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:35:19 26 4
gpt4 key购买 nike

我必须删除并重命名 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);
}

最佳答案

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

您可以使 saveFile()updateRootFile() 同步以防止被多个线程访问。

关于java - 在 Servlet 中删除和重命名多个文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8600260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com