Jsp 将文件从Windows客户端上载到Linux tomcat服务器时获取FileNotFoundException
我最近刚刚在linux服务器上运行了一个运行tomcat的web应用程序。在Windows计算机上本地运行应用程序可以让我毫无问题地上传XML文件,但现在它在tomcat服务器上,我不断收到NullPointerException 以下是接收文件的servlet部分:Jsp 将文件从Windows客户端上载到Linux tomcat服务器时获取FileNotFoundException,jsp,tomcat,servlets,file-upload,filenotfoundexception,Jsp,Tomcat,Servlets,File Upload,Filenotfoundexception,我最近刚刚在linux服务器上运行了一个运行tomcat的web应用程序。在Windows计算机上本地运行应用程序可以让我毫无问题地上传XML文件,但现在它在tomcat服务器上,我不断收到NullPointerException 以下是接收文件的servlet部分: protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletExcep
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, ParserConfigurationException, SAXException, ResponseException {
response.setContentType("text/html;charset=UTF-8");
//Create path components to save the file
final Part filePart = request.getPart("XMLFile"); // Retrieves <input type="file" name="XMLFile">
final String path = "/opt/tomcat/bin";
final String fileName = getFileName(filePart);
OutputStream outStream = null;
InputStream fileContent = null;
final PrintWriter out = response.getWriter();
try {
//File xmlFile = new File(path + File.separator + fileName);
File xmlFile = new File(fileName);
outStream = new FileOutputStream(xmlFile);
fileContent = filePart.getInputStream();
...
protectedvoidprocessrequest(HttpServletRequest请求,HttpServletResponse响应)
抛出ServletException、IOException、ParserConfiguration异常、SAXException、ResponseException{
setContentType(“text/html;charset=UTF-8”);
//创建路径组件以保存文件
final Part filePart=request.getPart(“XMLFile”);//检索
最终字符串路径=“/opt/tomcat/bin”;
最终字符串文件名=getFileName(filePart);
OutputStream outStream=null;
InputStream fileContent=null;
最终PrintWriter输出=response.getWriter();
试一试{
//File xmlFile=新文件(路径+File.separator+文件名);
文件xmlFile=新文件(文件名);
outStream=新文件OutputStream(xmlFile);
fileContent=filePart.getInputStream();
...
“File xmlFile=new File(fileName)”行在稍后的catch语句中抛出FileNotFoundException。我尝试了许多不同的可能性,但找不到错误
在一些代码行中,文件路径是“opt/tomcat/bin/(我的xml文件的名称)”,其他时候是“opt/tomcat/bin/opt/tomcat/bin/(我的xml文件的名称)”,但它们都不起作用
你知道错误是什么吗
以下是我的JSP页面上处理文件上载的表单:
<form style="text-align: center" style="position: relative" style="right: auto" style="left: auto" action="upload" method="post" enctype="multipart/form-data">
<h3>Upload your iTunes library</h3>
<input style="margin: 0 auto" name="XMLFile" id="file" type="file"/>
<br>
<input type="submit" value="Submit" formmethod="post" formaction="GetItunesLibrary"/>
</form>
上传你的iTunes库
首先,您不应该将文件存储在/opt/tomcat/bin
中。
使用另一个不需要root访问权限的文件夹。
类似于/home//上传或类似内容。
如果linux上的tomcat设备在您上面指定的同一用户下运行,则不需要设置额外的权限
如果指定一个文件
实例,则要将内容写入..请使用完整路径,如:
/home//uploads/
在您的例子中,您已经为一个可能不存在的物理文件创建了一个文件实例
因此,根据您的代码,使用
File outputFile = new File(path + File.separator + fileName);
if (!outputFile.exists())
outputFile.createNewFile();
其中路径类似于/home//uploads
。
注意:这是假设文件夹上传
存在
在linux上,您应该始终确保您或更好地说tomcat拥有在指定文件夹中读取/写入/创建数据所需的权限您好!我采纳了您的建议,并更改了/home/uploads的路径。但是,错误仍然存在。它一直说在/opt/tomcat/bin/opt/tomcat/bin/中找不到该文件。我已经查看了我对这段代码反复思考了一千遍,现在我不知道我做错了什么。下面是完整的脚本:试试这个:String filePath=request.getRealPath(“/home/uploads”);//注意,这要求文件夹结构是您的_home/home/uploads,然后调用一个两个参数的文件构造函数,因此File xmlFile=new File(filePath,fileName);我不知道为什么,但是调用一个arg construcor对我来说不起作用(我和你一样得到了FileNotFound)。我想你把你网站的用户和你linux服务器中的用户环境搞混了。我说的只是linux用户。如果你启动tomcat,你就在linux的特定用户环境中启动它。(例如,您用来通过ssh连接到linux服务器的用户)。这不是必须的,但在这种情况下,您必须仔细设置文件夹的文件权限。不介意..欢迎:)很高兴您的问题得到了解决。