2 dryyoureyes dryyoureyes 于 2016.05.07 07:36 提问

使用jsp的文件操作上传文件出现“文件拒绝访问”的问题,

uploadfile.jsp代码如下:

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



上传文件


选择要上传的文件








uploadfile1.jsp代码如下:

<%@ page contentType="text/html; charset=gbk" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



upFile


<%
int MAX_SIZE=102400*102400;
String rootPath;
DataInputStream in = null;
FileOutputStream fileOut = null;
String remoteAddr = request.getRemoteAddr();
String serverName = request.getServerName();
String realPath = request.getRealPath("/");
realPath = realPath.substring(0,realPath.lastIndexOf("\"));
rootPath = realPath + "\upload\";
out.println("上传文件保存目录为" + rootPath);
String contentType = request.getContentType();
try{
if(contentType.indexOf("multipart/form-data")>=0) {
in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
if(formDataLength>MAX_SIZE) {
out.println("

上传的文件字节不可以超过" + MAX_SIZE + "

");
return;
}
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes,totalBytesRead,formDataLength);
totalBytesRead+=byteRead;
}
String file = new String(dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"")+10);
saveFile = saveFile.substring(0,saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\")+1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex+1,contentType.length());
String fileName = rootPath = saveFile;
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n",pos)+1;
pos = file.indexOf("\n",pos)+1;
pos = file.indexOf("\n",pos)+1;
int boundaryLocation = file.indexOf(boundary,pos)-4;
int startPos = ((file.substring(0,pos)).getBytes()).length;
int endPos = ((file.substring(0,boundaryLocation)).getBytes()).length;
File checkFile = new File(fileName);
if(checkFile.exists()) {
out.println("

" + saveFile + "文件已经存在

");
}
File fileDir = new File(rootPath);
if(!fileDir.exists()) {
fileDir.mkdirs();
}
fileOut = new FileOutputStream(fileName);
fileOut.write(dataBytes,startPos,(endPos-startPos));
fileOut.close();
out.println("

"+ saveFile + "文件上传成功.

")
; }
else{
String content = request.getContentType();
out.println("

上传的数据文件是不是 multipart/form-data

");
}
} catch(Exception e) {
//throw new ServletException(ex.getMessage());

}
%>
继续上传文件

运行后出现的错误信息如下:

五月 07, 2016 7:34:38 上午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [jsp] in context with path [] threw exception [javax.servlet.ServletException: 上传文件发生拒绝访问错误.txt (拒绝访问。)] with root cause
javax.servlet.ServletException: 上传文件发生拒绝访问错误.txt (拒绝访问。)
at org.apache.jsp.lizi.wenjian1.uploadfile1_jsp._jspService(uploadfile1_jsp.java:161)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
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.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!