问题描述
- jsp上传文件,为什么一直报servlet异常,在别个电脑上面却可以正确运行
-
<%@ page contentType=""text/html; charset=GBK""%><%@ page import=""java.io.*""%><%@ page import=""java.util.*""%><%@ page import=""javax.servlet.*""%><%@ page import=""javax.servlet.http.*""%><% request.setCharacterEncoding(""utf-8""); %><!DOCTYPE html PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"" ""http://www.w3.org/TR/html4/loose.dtd""><html><head><title>Insert title here</title></head><body> <% 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(0realPath.lastIndexOf(""\"")); rootPath=realPath+""\upload\""; //rootPath = ""D:\jsp666""; //创建文件的保存目录 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(""<P>上传的文件字节数不可以超过"" + MAX_SIZE + ""</p>""); return; } byte dataBytes[] = new byte[formDataLength]; //保存上传文件的数据 int byteRead = 0; int totalBytesRead = 0; while (totalBytesRead < formDataLength) { //上传的数据保存在byte数组 byteRead = in.read(dataBytes totalBytesRead formDataLength); totalBytesRead += byteRead; } String file = new String(dataBytes); //根据byte数组创建字符串 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; //取得文件数据的结束的位置 //out.print(fileName); File checkFile = new File(fileName); //检查上载文件是否存在 if (checkFile.exists()) { out.println(""<p>"" + saveFile + ""文件已经存在.</p>""); } File fileDir = new File(rootPath);//检查上载文件的目录是否存在 if (!fileDir.exists()) { fileDir.mkdirs(); } fileOut = new FileOutputStream(fileName); //创建文件的写出类 fileOut.write(dataBytes startPos (endPos - startPos)); //保存文件的数据 fileOut.close(); out.println(""<P><font color=red size=5>"" + saveFile + ""文件成功上传.</font></p>""); } else { String content = request.getContentType(); out.println(""<p>上传的数据类型不是是multipart/form-data</p>""); } } catch (Exception ex) { ex.printStackTrace(); //throw new ServletException(ex.getMessage()); } %></body></html>
解决方案
什么异常把异常代码贴出来.
解决方案二:
这个要具体看是什么异常才行,,,
解决方案三:
五月 25 2016 4:44:14 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [jsp] in context with path [/sy6] threw exception [javax.servlet.ServletException] with root cause
javax.servlet.ServletException
at org.apache.jsp.uploadfile1_jsp._jspService(uploadfile1_jsp.java:183)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
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)