I am uploading an excel file to the tomcat server. Which is saving inside my eclipse directory D:\workspace_Eclipse\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\StatusPortal\Job_doc\abc.xls
我正在将一个excel文件上传到tomcat服务器。哪个保存在我的eclipse目录D:\ workspace_Eclipse \ .metadata \ .plugins \ org.eclipse.wst.server.core \ tmp1 \ wtpwebapps \ StatusPortal \ Job_doc \ abc.xls
When ever i am accessing this file its giving me file not found Exception \Job_doc\abc.xls
. Its could not able to find the path which is i am giving while accessing the file like \Job_doc\abc.xls
当我访问这个文件时,它给我的文件找不到Exception \ Job_doc \ abc.xls。它无法找到我在访问文件时给出的路径,如\ Job_doc \ abc.xls
I am giving the path \Job_doc\abc.xls
while accessing.
我在访问时给出路径\ Job_doc \ abc.xls。
2 个解决方案
#1
0
This is because you are using a relative path. Eclipse will use the current working directory to be a temp location for deploying the webapp. So the file is uploaded to the folder relative to this path (This happens when you start the app from eclipse Run On Server
. Define your paths as static constants(May be you can use absolute paths for testing). After testing you can use the relative paths on production deployment.
这是因为您使用的是相对路径。 Eclipse将使用当前工作目录作为部署webapp的临时位置。因此,文件被上传到相对于此路径的文件夹(当您从eclipse Run On Server启动应用程序时会发生这种情况。将路径定义为静态常量(可能您可以使用绝对路径进行测试)。测试后您可以使用生产部署的相对路径。
Still, you can do alternate way. Dont use the integrated tomcat server of Eclipse. Use a standalone server, use the descriptor file to link the webapp in workspace to tomcat. After the save, just reload the app in tomcat manager and try.
不过,你可以采取其他方式。不要使用Eclipse的集成tomcat服务器。使用独立服务器,使用描述符文件将工作空间中的webapp链接到tomcat。保存后,只需在tomcat管理器中重新加载应用程序并尝试。
#2
0
Try reading your file using ClassLoader
as below:
尝试使用ClassLoader读取文件,如下所示:
InputStream inputStream =
getClass().getClassLoader().getResourceAsStream("/Job_doc/abc.xls");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream ));
If you want to get the File
object, then try as below:
如果要获取File对象,请尝试以下操作:
URI uri = getClass().getClassLoader().getResource("/Job_doc/abc.xls").toURI();
File file = new File(uri);
#1
0
This is because you are using a relative path. Eclipse will use the current working directory to be a temp location for deploying the webapp. So the file is uploaded to the folder relative to this path (This happens when you start the app from eclipse Run On Server
. Define your paths as static constants(May be you can use absolute paths for testing). After testing you can use the relative paths on production deployment.
这是因为您使用的是相对路径。 Eclipse将使用当前工作目录作为部署webapp的临时位置。因此,文件被上传到相对于此路径的文件夹(当您从eclipse Run On Server启动应用程序时会发生这种情况。将路径定义为静态常量(可能您可以使用绝对路径进行测试)。测试后您可以使用生产部署的相对路径。
Still, you can do alternate way. Dont use the integrated tomcat server of Eclipse. Use a standalone server, use the descriptor file to link the webapp in workspace to tomcat. After the save, just reload the app in tomcat manager and try.
不过,你可以采取其他方式。不要使用Eclipse的集成tomcat服务器。使用独立服务器,使用描述符文件将工作空间中的webapp链接到tomcat。保存后,只需在tomcat管理器中重新加载应用程序并尝试。
#2
0
Try reading your file using ClassLoader
as below:
尝试使用ClassLoader读取文件,如下所示:
InputStream inputStream =
getClass().getClassLoader().getResourceAsStream("/Job_doc/abc.xls");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream ));
If you want to get the File
object, then try as below:
如果要获取File对象,请尝试以下操作:
URI uri = getClass().getClassLoader().getResource("/Job_doc/abc.xls").toURI();
File file = new File(uri);