大家好,这是我的一个文件上传的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">
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>文件上传界面</title>
</head><body>
<%
int MAX_SIZE=102400;
String rootPath;
DataInputStream in=null;
FileOutPutStream fileOut=null;
String remoteAddr=request.getRemoteAddr();
String serverName=request.getServerName();
String realPath=request.getRealPath(serverName);
realPath=realPath.substring(0,realPath.lastIndexOf("\\"));
rootPath=realPath+"\\unload\\";
Out.println("上传文件保存目录为"+rootPath);
String contentType=request.getContentType();
try
{
if(contentType.indexOf("multipart/forem-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)
{
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.lastIndex0f("=");
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+"文件已经存在.</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)
{
throw new ServletException(ex.getMessage());
}
%>
<a href="shangchuan.jsp">继续上传文件</a>
</body>
</html>
出错信息如下:
type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 19 in the jsp file: /file.jsp
FileOutPutStream cannot be resolved to a type
16: int MAX_SIZE=102400;
17: String rootPath;
18: DataInputStream in=null;
19: FileOutPutStream fileOut=null;
20: String remoteAddr=request.getRemoteAddr();
21: String serverName=request.getServerName();
22: String realPath=request.getRealPath(serverName);
An error occurred at line: 15 in the jsp file: /file.jsp
Out cannot be resolved
12: </head>
13:
14: <body>
15: <%
16: int MAX_SIZE=102400;
17: String rootPath;
18: DataInputStream in=null;
An error occurred at line: 15 in the jsp file: /file.jsp
Out cannot be resolved
12: </head>
13:
14: <body>
15: <%
16: int MAX_SIZE=102400;
17: String rootPath;
18: DataInputStream in=null;
An error occurred at line: 15 in the jsp file: /file.jsp
Syntax error on token "Invalid Character", ) expected
12: </head>
13:
14: <body>
15: <%
16: int MAX_SIZE=102400;
17: String rootPath;
18: DataInputStream in=null;
An error occurred at line: 50 in the jsp file: /file.jsp
The method lastIndex0f(String) is undefined for the type String
47: String saveFile=file.substring(file.indexOf("filename=\"")+10);
48: saveFile=saveFile.substring(0,saveFile.indexOf("\n"));
49: saveFile=saveFile.substring(saveFile.lastIndexOf("\\")+1,saveFile.indexOf("\""));
50: int lastIndex=contentType.lastIndex0f("=");
51: String boundary=contentType.substring(lastIndex+1,contentType.length());
52: String fileName=rootPath+saveFile;
53: int pos;
An error occurred at line: 70 in the jsp file: /file.jsp
FileOut cannot be resolved
67: {
68: fileDir.mkdirs();
69: }
70: FileOut=new FileOutputstream(fileName);
71: fileOut.write(dataBytes,startPos,(endPos-startPos));
72: fileOut.close();
73: Out.println("<p><font color=red size=5>"+saveFile+"文件成功上传!</font></p>");
An error occurred at line: 70 in the jsp file: /file.jsp
FileOutputstream cannot be resolved to a type
67: {
68: fileDir.mkdirs();
69: }
70: FileOut=new FileOutputstream(fileName);
71: fileOut.write(dataBytes,startPos,(endPos-startPos));
72: fileOut.close();
73: Out.println("<p><font color=red size=5>"+saveFile+"文件成功上传!</font></p>");
An error occurred at line: 15 in the jsp file: /file.jsp
Out cannot be resolved
12: </head>
13:
14: <body>
15: <%
16: int MAX_SIZE=102400;
17: String rootPath;
18: DataInputStream in=null;
An error occurred at line: 15 in the jsp file: /file.jsp
Out cannot be resolved
12: </head>
13:
14: <body>
15: <%
16: int MAX_SIZE=102400;
17: String rootPath;
18: DataInputStream in=null;
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:415)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:308)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:308)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.10 logs.
<%@ 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">
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>文件上传界面</title>
</head><body>
<%
int MAX_SIZE=102400;
String rootPath;
DataInputStream in=null;
FileOutPutStream fileOut=null;
String remoteAddr=request.getRemoteAddr();
String serverName=request.getServerName();
String realPath=request.getRealPath(serverName);
realPath=realPath.substring(0,realPath.lastIndexOf("\\"));
rootPath=realPath+"\\unload\\";
Out.println("上传文件保存目录为"+rootPath);
String contentType=request.getContentType();
try
{
if(contentType.indexOf("multipart/forem-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)
{
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.lastIndex0f("=");
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+"文件已经存在.</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)
{
throw new ServletException(ex.getMessage());
}
%>
<a href="shangchuan.jsp">继续上传文件</a>
</body>
</html>
出错信息如下:
type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 19 in the jsp file: /file.jsp
FileOutPutStream cannot be resolved to a type
16: int MAX_SIZE=102400;
17: String rootPath;
18: DataInputStream in=null;
19: FileOutPutStream fileOut=null;
20: String remoteAddr=request.getRemoteAddr();
21: String serverName=request.getServerName();
22: String realPath=request.getRealPath(serverName);
An error occurred at line: 15 in the jsp file: /file.jsp
Out cannot be resolved
12: </head>
13:
14: <body>
15: <%
16: int MAX_SIZE=102400;
17: String rootPath;
18: DataInputStream in=null;
An error occurred at line: 15 in the jsp file: /file.jsp
Out cannot be resolved
12: </head>
13:
14: <body>
15: <%
16: int MAX_SIZE=102400;
17: String rootPath;
18: DataInputStream in=null;
An error occurred at line: 15 in the jsp file: /file.jsp
Syntax error on token "Invalid Character", ) expected
12: </head>
13:
14: <body>
15: <%
16: int MAX_SIZE=102400;
17: String rootPath;
18: DataInputStream in=null;
An error occurred at line: 50 in the jsp file: /file.jsp
The method lastIndex0f(String) is undefined for the type String
47: String saveFile=file.substring(file.indexOf("filename=\"")+10);
48: saveFile=saveFile.substring(0,saveFile.indexOf("\n"));
49: saveFile=saveFile.substring(saveFile.lastIndexOf("\\")+1,saveFile.indexOf("\""));
50: int lastIndex=contentType.lastIndex0f("=");
51: String boundary=contentType.substring(lastIndex+1,contentType.length());
52: String fileName=rootPath+saveFile;
53: int pos;
An error occurred at line: 70 in the jsp file: /file.jsp
FileOut cannot be resolved
67: {
68: fileDir.mkdirs();
69: }
70: FileOut=new FileOutputstream(fileName);
71: fileOut.write(dataBytes,startPos,(endPos-startPos));
72: fileOut.close();
73: Out.println("<p><font color=red size=5>"+saveFile+"文件成功上传!</font></p>");
An error occurred at line: 70 in the jsp file: /file.jsp
FileOutputstream cannot be resolved to a type
67: {
68: fileDir.mkdirs();
69: }
70: FileOut=new FileOutputstream(fileName);
71: fileOut.write(dataBytes,startPos,(endPos-startPos));
72: fileOut.close();
73: Out.println("<p><font color=red size=5>"+saveFile+"文件成功上传!</font></p>");
An error occurred at line: 15 in the jsp file: /file.jsp
Out cannot be resolved
12: </head>
13:
14: <body>
15: <%
16: int MAX_SIZE=102400;
17: String rootPath;
18: DataInputStream in=null;
An error occurred at line: 15 in the jsp file: /file.jsp
Out cannot be resolved
12: </head>
13:
14: <body>
15: <%
16: int MAX_SIZE=102400;
17: String rootPath;
18: DataInputStream in=null;
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:415)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:308)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:308)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.10 logs.
FileOutputStream不是FileOutPutStream也不是FileOutputstream
out不是Out
你没有引入相关的类吧