甚至在Exception之后上传文件(FileNotFoundException(拒绝访问))

时间:2015-02-27 14:21:51

标签: java java-ee servlets upload filenotfoundexception

我创建了一个servlet,用于上传用户从表单中选择的文件。

HTML表单摘要是:

    <form method="POST" enctype="multipart/form-data" action="UploadFile">
<div class="form-group">
    <label for="inputModuleName">Module Name</label>
    <input type="text" class="form-control" id="inputModuleName" placeholder="Module Name">
</div>
<div class="form-group">
    <label for="exampleInputFile">File input</label>
    <input type="file" id="exampleInputFile" name="fileName">
    <p class="help-block">Only .docx & .pdf files</p>
</div>
    <div class="form-group">
    <label for="inputComment">Comments</label>
    <textarea class="form-control" rows="4" placeholder="Write here..."></textarea>
</div>
<button name="uploadButton" class="btn btn-default">Upload</button>

注意:此处只有文件类型输入相关,因为它是测试页面。目前没有其他输入字段被处理。

和Servlet代码是:

    import java.io.*;
    import java.sql.*;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.annotation.MultipartConfig;
    import javax.servlet.*;
    import javax.servlet.http.*;

    @MultipartConfig(fileSizeThreshold=1024*1024*2, maxFileSize=1024*1024*5)
    public class UploadFile extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    PrintWriter out = response.getWriter();

    String appPath = request.getServletContext().getRealPath("/");
    String saveDir = getServletContext().getInitParameter("uploaddir");
    String savePath = appPath + saveDir;

    File fileSaveDir = new File(savePath);

    try{
        for(Part part : request.getParts()){
            String fileName = extractFileName(part);
            part.write(savePath + File.separator + fileName);
        }
        out.println("File uploaded!!!");
    }catch(Exception e){
        e.printStackTrace();
    out.println("Error: Can't Upload");
    }
}

private String extractFileName(Part part) {
    String contentDisp = part.getHeader("content-disposition");
    String[] items = contentDisp.split(";");
    for (String s : items) {
        if (s.trim().startsWith("filename")) {
            return s.substring(s.indexOf("=") + 2, s.length()-1);
        }
    }
    return "";
}
}

当我提交表单时,它总是抛出异常,我在控制台中获得以下输出,但文件上传到文件夹:

java.io.IOException: java.io.FileNotFoundException: C:\tomcat\webapps\proto1\filesupload (Access is denied)
    at org.apache.catalina.core.ApplicationPart.write(ApplicationPart.java:121)
    at UploadFile.doPost(UploadFile.java:30)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2466)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2455)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: java.io.FileNotFoundException: C:\tomcat\webapps\proto1\filesupload (Access is denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
    at org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.write(DiskFileItem.java:377)
    at org.apache.catalina.core.ApplicationPart.write(ApplicationPart.java:119)
    ... 24 more

如何解决此问题?

需要考虑的一些要点:

  • 我正在使用Windows 8.1操作系统和Apache Tomcat 7服务器。
  • saveFileDir.canRead()saveFileDir.canWrite()都返回true
  • 从文件夹的安全设置编辑权限,以完全修改&#39;。

1 个答案:

答案 0 :(得分:0)

尝试使用File.separator而不是在您的路径中放置斜杠... 这确保了使用OS相关的路径分隔符。

说这个多余但仍然尝试使用双向后斜线..“\\”。

这是我读到的链接。

Read here