将 JSP 代码从 Windows 迁移到 Linux 计算机时出错

发布于 2024-09-30 18:31:10 字数 2082 浏览 5 评论 0原文

我编写了一个在Windows系统的Tomcat5.5服务器上运行的JSP代码。

我必须将所有 JSP 代码复制到 Linux 系统,当我执行相同操作时,出现如下错误。

 javax.servlet.ServletException: c:\tmp is not a directory
 Readcsv.init(Readcsv.java:36)
 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
 org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:875)


org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
 org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
 org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
 org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
 java.lang.Thread.run(Thread.java:636)

我在没有c:\tmp目录的windows系统中修改了一段java代码,并重新启动了tomcat服务器,该工具工作正常。

当我将windows修改后的java代码替换到linux系统时,仍然出现同样的错误。

注意:我使用 url http://192.168.0.85:8080/CNA/uploadcsv.jsp 从 Windows 访问 linux 服务器,其中 85 是 linux 的系统编号。

linux版本是否也需要重新启动tomcat之类的东西?如果是这样怎么做?

更新

这是我在代码中使用 c:\tmp 位置的地方。

public class Readcsv extends HttpServlet {
private static final String TMP_DIR_PATH = "c:\tmp";
private File tmpDir;
private static final String DESTINATION_DIR_PATH ="/files";
private File destinationDir;

public void init(ServletConfig config) throws ServletException {
    super.init(config);
    tmpDir = new File(TMP_DIR_PATH);
    if(!tmpDir.isDirectory()) {
        throw new ServletException(TMP_DIR_PATH + " is not a directory");
    }
    String realPath = getServletContext().getRealPath(DESTINATION_DIR_PATH);
    destinationDir = new File(realPath);
    if(!destinationDir.isDirectory()) {
        throw new ServletException(DESTINATION_DIR_PATH+" is not a directory");
    }

}

如何找到临时路径的替代品?目标路径工作正常。 我完全复制了这个 示例 中的代码

I worked on a JSP code that is runnning on a Tomcat5.5 server in windows system .

I had to copy all the JSP code to a linux system and when I did the same I got an error stating below.

 javax.servlet.ServletException: c:\tmp is not a directory
 Readcsv.init(Readcsv.java:36)
 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
 org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:875)


org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
 org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
 org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
 org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
 java.lang.Thread.run(Thread.java:636)

I modified a java code in the windows system without that c:\tmp directory and restarted the tomcat server and the tool worked fine.

When I replaced the modified java code of windows to the linux system, I still get the same error.

Note: Am accessing the linux server from windows using the url http://192.168.0.85:8080/CNA/uploadcsv.jspwhere 85 is the system number of linux.

Is there anything like tomcat has to be restarted for the linux version too? If so how to do the same?

UPDATE

This is where I have used the c:\tmp location in my code.

public class Readcsv extends HttpServlet {
private static final String TMP_DIR_PATH = "c:\tmp";
private File tmpDir;
private static final String DESTINATION_DIR_PATH ="/files";
private File destinationDir;

public void init(ServletConfig config) throws ServletException {
    super.init(config);
    tmpDir = new File(TMP_DIR_PATH);
    if(!tmpDir.isDirectory()) {
        throw new ServletException(TMP_DIR_PATH + " is not a directory");
    }
    String realPath = getServletContext().getRealPath(DESTINATION_DIR_PATH);
    destinationDir = new File(realPath);
    if(!destinationDir.isDirectory()) {
        throw new ServletException(DESTINATION_DIR_PATH+" is not a directory");
    }

}

How can I find the replacement for the temp path? The destination path works fine.
I exactly copied the code from this example

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尝蛊 2024-10-07 18:31:10

不要在代码中对磁盘文件系统路径进行硬编码。这只是可移植性和可维护性的问题。

如果是临时文件,请使用 File#createTempFile()

File tempfile = File.createTempFile("name", ".ext");

无论环境如何,它都会在正确的位置自动创建临时文件。不过,您也可以通过 System.getProperty("java.io.tmpdir"); 获取 tmp dir 根位置。


如果应用程序要读取资源,只需将它们放入运行时类路径中或将其路径添加到运行时类路径中即可。然后,您可以通过 ClassClassLoader 上的 getResource()getResourceAsStream() 方法从类路径中获取它们。

InputStream input = getClass().getResourceAsStream("file.properties");

如果您确实需要在类路径之外有一个固定路径,那么最好在属性文件中定义它,这样您至少可以从应用程序外部控制该路径(因此,无需每次都更改代码)。

String path = properties.getProperty("my.file.path");

Don't hardcode disk file system paths in your code. That's only portability and maintainability trouble.

In case of temporary files, rather make use of File#createTempFile().

File tempfile = File.createTempFile("name", ".ext");

It will automatically create the temp file at the right location, regardless of the environment. You can however also obtain the tmp dir root location by System.getProperty("java.io.tmpdir");.


In case of resources which are to be read by your application, just put them in the runtime classpath or add their path to the runtime classpath. Then you can just obtain them from the classpath by getResource() and getResourceAsStream() methods on Class or ClassLoader.

InputStream input = getClass().getResourceAsStream("file.properties");

If you really need to have a fixed path outside the classpath, then rather define it in a properties file so that you at least have any control over the path from outside the application (so, without the need to change the code everytime).

String path = properties.getProperty("my.file.path");
半世蒼涼 2024-10-07 18:31:10

看来您的应用程序尝试读取“C:\tmp”下的 csv 文件,该文件在您的 Linux 系统上不存在。

it seems that your application try to read a csv file under "C:\tmp" which doesn't exist on your linux system.

£噩梦荏苒 2024-10-07 18:31:10

你说你修改了代码并将其重新部署到Tomcat。

您可能只需要重新启动 Tomcat 即可让它获取新代码。在那之前,它将运行旧代码,并且您将得到相同的错误。

如何重新启动 Tomcat 取决于您运行的 Linux 发行版以及安装 Tomcat 的方式。

You said you modified the code and redeployed it to Tomcat.

You probably just need to restart Tomcat to get it to pick up the new code. Until then, it will be running the old code and you will get the same error.

How you restart Tomcat depends on which Linux distribution you are running and how you installed Tomcat.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文