0

我一直在本地测试一个 java servlet,但我不知道这里发生了什么。该文件已正确上传到目的地,但在正确写入文件后出现“访问被拒绝”错误:

Apr 08, 2016 10:12:39 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [FileUploadServlet] in context with path [/FileUpload] threw exception
java.io.IOException: java.io.FileNotFoundException: C:\temp (Access is denied)
at org.apache.catalina.core.ApplicationPart.write(ApplicationPart.java:121)
at FileUploadServlet.doPost(FileUploadServlet.java:63)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.FileNotFoundException: C:\temp (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:171)
at org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.write(DiskFileItem.java:402)
at org.apache.catalina.core.ApplicationPart.write(ApplicationPart.java:119)
... 22 more

小服务程序代码:

/**
* Servlet implementation class FileUploadServlet
*/
@WebServlet(asyncSupported = true, urlPatterns = { "/FileUploadServlet" })
@MultipartConfig(location="c:/temp/temp")

public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * Default constructor. 
 */
public FileUploadServlet() {
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response. getWriter();

    out.print("<!DOCTYPE html>");
    out.println("<head><title>File Upload</title><meta http-equiv='Content-Type' content='text/html charset=UTF-8'>");
    out.println("</head><body><form method='POST' action='./FileUploadServlet' enctype='multipart/form-data' >");
    out.println("File:<input type='file' name='file' id='file' /> <br/>");
    out.println("Destination:<input type='text' value='/tmp' name='destination'/>");
    out.println("</br><input type='submit' value='Upload' name='upload' id='upload' />");
    out.println("</form></body></html>");
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    for (Part part : request.getParts()) {
        String fileName = extractFileName(part);
        File f = new File(fileName);
        part.write("C:" + File.separator + "temp" + File.separator + f.getName());
    }
}

/**
 * Extracts file name from HTTP header content-disposition
 */
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 "";
}
}

编辑:在跟踪中给出问题的行是

part.write("C:" + File.separator + "temp" + File.separator + f.getName());

编辑 2:终于让它工作了(看起来文件在发送的文件的一个部分内)。编码:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {      
    File uploads = new File("C:/temp");
    Part filePart = request.getPart("file");
    String fileName = extractFileName(filePart);
    File f = new File(fileName);
    File file = new File(uploads, f.getName());
    try (InputStream input = filePart.getInputStream()) {
        Files.copy(input, file.toPath());
    } catch (Exception e){
        response.setContentType("text/html");
        PrintWriter out = response. getWriter();
        out.print("<body>Failed to upload file <br>" + e.getMessage());
    }
}
4

2 回答 2

0

严重:servlet [FileUploadServlet] 的 Servlet.service() 在路径 [/FileUpload] 的上下文中抛出异常 java.io.IOException: java.io.FileNotFoundException: C:\temp(访问被拒绝)

这表示执行此 java 部署的用户(在本例中为运行应用服务器的用户)没有访问该文件夹的权限。解决方法是将文件放在运行它的用户创建的文件夹中,或授予该文件夹相应的权限。

于 2016-04-08T09:08:39.767 回答
0

终于让它工作了(看起来文件在发送的文件的一个部分中)。编码:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {      
    File uploads = new File("C:/temp");
    Part filePart = request.getPart("file");
    String fileName = extractFileName(filePart);
    File f = new File(fileName);
    File file = new File(uploads, f.getName());
    try (InputStream input = filePart.getInputStream()) {
        Files.copy(input, file.toPath());
    } catch (Exception e){
        response.setContentType("text/html");
        PrintWriter out = response. getWriter();
        out.print("<body>Failed to upload file <br>" + e.getMessage());
    }
}
于 2018-11-25T19:21:32.847 回答