将 JSP 代码从 Windows 迁移到 Linux 计算机时出错
我编写了一个在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.jsp
where 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要在代码中对磁盘文件系统路径进行硬编码。这只是可移植性和可维护性的问题。
如果是临时文件,请使用
File#createTempFile()
。无论环境如何,它都会在正确的位置自动创建临时文件。不过,您也可以通过 System.getProperty("java.io.tmpdir"); 获取 tmp dir 根位置。
如果应用程序要读取资源,只需将它们放入运行时类路径中或将其路径添加到运行时类路径中即可。然后,您可以通过
Class
或ClassLoader
上的getResource()
和getResourceAsStream()
方法从类路径中获取它们。如果您确实需要在类路径之外有一个固定路径,那么最好在属性文件中定义它,这样您至少可以从应用程序外部控制该路径(因此,无需每次都更改代码)。
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()
.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()
andgetResourceAsStream()
methods onClass
orClassLoader
.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).
看来您的应用程序尝试读取“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.
你说你修改了代码并将其重新部署到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.