0

我将文件上传到服务器并从服务器读取文件,当我在服务器中打开文件夹时,我看到了上传的文件,但是当我尝试访问文件进行处理时,它给了我以下异常:

java.io.FileNotFoundException: D:\Servers\apache-tomcat-7.0.27\webapps\acct\accesscarddata (Access is denied)

更新 :

上传文件的代码:

protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession(true);
        for (Part part : request.getParts()) {
            InputStream is = request.getPart(part.getName()).getInputStream();
            int i = is.available();
            byte[] b = new byte[i];
            is.read(b);
            String fileName = getFileName(part);
            if(session.getAttribute("accesscardDatafileNname")!=null)
                session.setAttribute("accesscardDatafileNname", fileName);
            String uploadDir = getServletContext().getRealPath("/accesscarddata/");
            FileOutputStream os = new FileOutputStream(uploadDir + "/"+ fileName);
            os.write(b);
            is.close();
        }
    }

这就是我调用该方法的方式:doGet(request, response);

这就是我尝试读取文件的方式:

@SuppressWarnings({ "rawtypes", "unchecked" })
    protected void readFile(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String fileName = "", employeeAttendanceFilePath = "";
        String _uploadDir = getServletContext().getRealPath("/accesscarddata/");
        HttpSession session = request.getSession(true);
        if (session.getAttribute("accesscardDatafileNname") != null)
            fileName = (String) session.getAttribute("accesscardDatafileNname");

        employeeAttendanceFilePath = _uploadDir + "/" + fileName;
        List sheetData = new ArrayList();
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(employeeAttendanceFilePath);
            HSSFWorkbook workbook = new HSSFWorkbook(fis);
            HSSFSheet sheet = workbook.getSheetAt(0);// gets the first sheet on workbook
            Iterator rows = sheet.rowIterator();
            while (rows.hasNext()) {
                HSSFRow row = (HSSFRow) rows.next();
                Iterator cells = row.cellIterator();
                // count=count+1;
                List data = new ArrayList();
                while (cells.hasNext()) {
                    HSSFCell cell = (HSSFCell) cells.next();
                    data.add(cell);
                }
                sheetData.add(data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
        session.setAttribute("sheetData", sheetData);

    }

以下是堆栈跟踪:

java.io.FileNotFoundException: D:\Servers\apache-tomcat-7.0.27\webapps\acct\accesscarddata (Access i
s denied)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:120)
        at java.io.FileInputStream.<init>(FileInputStream.java:79)
        at com.slingmedia.notifier.servlet.LeaveReportServlet.readFile(LeaveReportServlet.java:236)
        at com.slingmedia.notifier.servlet.LeaveReportServlet.doPost(LeaveReportServlet.java:159)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.j
ava:305)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999
)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.jav
a:565)
        at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1812)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:662)

我想这是访问权限的问题,但不知道是相同的问题还是其他问题。

请为我提供必要的输入来解决此问题。

谢谢并恭祝安康。

4

1 回答 1

1

您正在阅读文件夹accesscarddata而不是accesscardDatafileNname

if(session.getAttribute("accesscardDatafileNname")!=null)
            session.setAttribute("accesscardDatafileNname", fileName);

这将永远不会使用文件名设置属性。

于 2012-07-24T12:06:24.817 回答