我将文件上传到服务器并从服务器读取文件,当我在服务器中打开文件夹时,我看到上传的文件但是当我尝试访问要处理的文件时,它给出了以下异常:
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)
我认为这是访问权限的问题,但不知道是否存在相同或其他问题。
请向我提供解决此问题的必要信息。
谢谢和问候。
答案 0 :(得分:1)
您正在阅读文件夹accesscarddata
,而不是accesscardDatafileNname
if(session.getAttribute("accesscardDatafileNname")!=null)
session.setAttribute("accesscardDatafileNname", fileName);
这永远不会使用文件名设置属性。