大家好 我上网下了个jspsmartupload组件
写了个简单的实例
代码如下
<!--
文件名:upload.html
-->
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<title>文件上传 </title>
</head> <body>
<p> </p>
<p align="center">上传文件选择 </p>
<form method="post" action="upload.jsp"
enctype="multipart/form-data" > <table width="75%" border="1" align="center">
<tr> <td align="center">主题: <input type="text" name="test"> </td> </tr>
<tr>
<td> <div align="center">1、
<input type="file" name="file1" size="30">
</div> </td>
</tr>
<tr>
<td> <div align="center">2、
<input type="file" name="file2" size="30">
</div> </td>
</tr>
<tr>
<td> <div align="center">3、
<input type="file" name="file3" size="30">
</div> </td>
</tr>
<tr>
<td> <div align="center">4、
<input type="file" name="file4" size="30">
</div> </td>
</tr>
<tr>
<td> <div align="center">
<input type="submit" name="submit" value="上传它!">
</div> </td>
</tr>
</table>
</form>
</body>
</html> upload。jsp代码如下
<%@ page contentType="text/html; charset=GBK" language="java"
import="java.util.*,com.jspsmart.upload.*" errorpage="" %>
<jsp:useBean id="myu" scope="page" class="com.jspsmart.upload.SmartUpload"/>
<html>
<head>
<title>文件上传处理页面 </title>
<**** http-equiv="content-type" content="text/html; charset=GBK">
</head> <body>
<%
// 新建一个smartupload对象
String destination="/images/";
// 上传初始化
myu.initialize(pagecontext);
// 设定上传限制
// 1.限制每个上传文件的最大长度。
myu.setMaxFileSize(10000);
// 2.限制总上传数据的长度。
myu.setTotalMaxFileSize(20000);
// 3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
myu.setAllowedFilesList("doc,txt");
// 4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,
//jsp,htm,html扩展名的文件和没有扩展名的文件。
myu.setDeniedFilesList("exe,bat,jsp,htm,html,,");
// 上传文件
myu.upload();
String content=myu.getRequest().getParameter("test");
out.print(content);
for(int i=0;i <myu.getFiles().getCount();i++)
{
com.jspsmart.upload.File myf=myu.getFiles().getFile(i);
if(!myf.usMissing())
{
myf.saveAs(destination+myf.getFileName);
count++;
}
} out.print(count+"个文件上传了!");
%>
</body>
</html> 报的错误是 type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: /admin/upload.jsp(2,0) Page directive has invalid attribute: contenttype
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:198)
org.apache.jasper.compiler.JspUtil.checkAttributes(JspUtil.java:311)
org.apache.jasper.compiler.Validator$DirectiveVisitor.visit(Validator.java:106)
org.apache.jasper.compiler.Node$PageDirective.accept(Node.java:590)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2338)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2388)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2394)
org.apache.jasper.compiler.Node$Root.accept(Node.java:489)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2338)
org.apache.jasper.compiler.Validator.validate(Validator.java:1700)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:178)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:306)
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:317)
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.14 logs.
我的项目意识是把本地一些文件 如 PDF 格式 execle 格式 txt格式的文件上传到服务器上 存放到项目下的iMAGES 文件夹下 再把路径存到数据库里面 哪个仁兄帮下忙!
我把相应的jar包加到classpath里面后编译完后还是报错
请问各位
我打好JAR包了 编译好的5个 class 文件在 com\jspsmart\upload 目录下
File.class Files.class Request.class ServletUpload.class SmartUpload.class SmartUploadException.class 我打成 jspsmartupload.jar
在jsp页面引用的时候 是 import com.jspsmart.upload.* 还是 import jspsmartupload.*
写了个简单的实例
代码如下
<!--
文件名:upload.html
-->
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<title>文件上传 </title>
</head> <body>
<p> </p>
<p align="center">上传文件选择 </p>
<form method="post" action="upload.jsp"
enctype="multipart/form-data" > <table width="75%" border="1" align="center">
<tr> <td align="center">主题: <input type="text" name="test"> </td> </tr>
<tr>
<td> <div align="center">1、
<input type="file" name="file1" size="30">
</div> </td>
</tr>
<tr>
<td> <div align="center">2、
<input type="file" name="file2" size="30">
</div> </td>
</tr>
<tr>
<td> <div align="center">3、
<input type="file" name="file3" size="30">
</div> </td>
</tr>
<tr>
<td> <div align="center">4、
<input type="file" name="file4" size="30">
</div> </td>
</tr>
<tr>
<td> <div align="center">
<input type="submit" name="submit" value="上传它!">
</div> </td>
</tr>
</table>
</form>
</body>
</html> upload。jsp代码如下
<%@ page contentType="text/html; charset=GBK" language="java"
import="java.util.*,com.jspsmart.upload.*" errorpage="" %>
<jsp:useBean id="myu" scope="page" class="com.jspsmart.upload.SmartUpload"/>
<html>
<head>
<title>文件上传处理页面 </title>
<**** http-equiv="content-type" content="text/html; charset=GBK">
</head> <body>
<%
// 新建一个smartupload对象
String destination="/images/";
// 上传初始化
myu.initialize(pagecontext);
// 设定上传限制
// 1.限制每个上传文件的最大长度。
myu.setMaxFileSize(10000);
// 2.限制总上传数据的长度。
myu.setTotalMaxFileSize(20000);
// 3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
myu.setAllowedFilesList("doc,txt");
// 4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,
//jsp,htm,html扩展名的文件和没有扩展名的文件。
myu.setDeniedFilesList("exe,bat,jsp,htm,html,,");
// 上传文件
myu.upload();
String content=myu.getRequest().getParameter("test");
out.print(content);
for(int i=0;i <myu.getFiles().getCount();i++)
{
com.jspsmart.upload.File myf=myu.getFiles().getFile(i);
if(!myf.usMissing())
{
myf.saveAs(destination+myf.getFileName);
count++;
}
} out.print(count+"个文件上传了!");
%>
</body>
</html> 报的错误是 type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: /admin/upload.jsp(2,0) Page directive has invalid attribute: contenttype
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:198)
org.apache.jasper.compiler.JspUtil.checkAttributes(JspUtil.java:311)
org.apache.jasper.compiler.Validator$DirectiveVisitor.visit(Validator.java:106)
org.apache.jasper.compiler.Node$PageDirective.accept(Node.java:590)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2338)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2388)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2394)
org.apache.jasper.compiler.Node$Root.accept(Node.java:489)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2338)
org.apache.jasper.compiler.Validator.validate(Validator.java:1700)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:178)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:306)
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:317)
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.14 logs.
我的项目意识是把本地一些文件 如 PDF 格式 execle 格式 txt格式的文件上传到服务器上 存放到项目下的iMAGES 文件夹下 再把路径存到数据库里面 哪个仁兄帮下忙!
我把相应的jar包加到classpath里面后编译完后还是报错
请问各位
我打好JAR包了 编译好的5个 class 文件在 com\jspsmart\upload 目录下
File.class Files.class Request.class ServletUpload.class SmartUpload.class SmartUploadException.class 我打成 jspsmartupload.jar
在jsp页面引用的时候 是 import com.jspsmart.upload.* 还是 import jspsmartupload.*
org.apache.jasper.JasperException: /admin/upload.jsp(2,0) Page directive has invalid attribute: contenttype 你试着把 contentType="text/html; charset=GBK" 去掉看下
throws IOException, ServletException {
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
String username=(String)request.getSession().getAttribute("username");
try {
File tempfile = new File(System.getProperty("java.io.tmpdir"));// 采用系统临时文件目录
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
diskFileItemFactory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
diskFileItemFactory.setRepository(tempfile); // 设置缓冲区目录
ServletFileUpload fu = new ServletFileUpload(diskFileItemFactory);
fu.setSizeMax(4194304);// 设置上传文件大小上限
List fileItems = fu.parseRequest(request);
Iterator i = fileItems.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = new String(fi.getName().getBytes("ISO8859_1"),"GB2312").trim();
if (fileName != null) {
File fullFile = new File(fi.getName());
File savedFile = new File(uploadPath, fullFile.getName());
fi.write(savedFile);
String pict="../file/"+fullFile.getName();
UserInfo u=new UserInfo();
UserDAO dao=new UserDAO();
u=dao.findUser(username);
u.setUPicture(pict);
dao.update(u);
response.sendRedirect("../forum/modify.jsp");
}
else
{
PrintWriter out = response.getWriter();
out.print("The File name is null !");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
throws IOException, ServletException {
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
String username=(String)request.getSession().getAttribute("username");
try {
File tempfile = new File(System.getProperty("java.io.tmpdir"));// 采用系统临时文件目录
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
diskFileItemFactory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
diskFileItemFactory.setRepository(tempfile); // 设置缓冲区目录
ServletFileUpload fu = new ServletFileUpload(diskFileItemFactory);
fu.setSizeMax(4194304);// 设置上传文件大小上限
List fileItems = fu.parseRequest(request);
Iterator i = fileItems.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = new String(fi.getName().getBytes("ISO8859_1"),"GB2312").trim();
if (fileName != null) {
File fullFile = new File(fi.getName());
File savedFile = new File(uploadPath, fullFile.getName());
fi.write(savedFile);
String pict="../file/"+fullFile.getName();
UserInfo u=new UserInfo();
UserDAO dao=new UserDAO();
u=dao.findUser(username);
u.setUPicture(pict);
dao.update(u);
response.sendRedirect("../forum/modify.jsp");
}
else
{
PrintWriter out = response.getWriter();
out.print("The File name is null !");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
myu.setAllowedFilesList("doc,txt"); 和这个有关系吗?后缀名多开几个(我的项目意识是把本地一些文件 如 PDF 格式 execle 格式)我一直用的apache组件try {
DiskFileUpload upload = new DiskFileUpload();
//存放非文件类属性值
HashMap map = new HashMap();
//文件名
String filename = "";
//上传路径
String uploadPath = path; // 解析请求
try {
// 允许使用的内存容量
upload.setSizeThreshold(ThresholdSize);
//允许文件上传的容量(总)
upload.setSizeMax(FileSize);
//设置超过容量存放的硬盘目录
upload.setRepositoryPath(tempPath);
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next(); if (!item.isFormField()) {
String name = item.getName(); filename = "" + name.substring(name.lastIndexOf("."));
filename = System.currentTimeMillis() + filename;
File uploadedFile = new File(uploadPath,filename); item.write(uploadedFile);
} else {
map.put(item.getFieldName(), item.getString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
map.put("upfilename", filename);
return map;
} catch (Exception e) {
//
}
import="java.util.*,com.jspsmart.upload.*" errorpage="" %>
<jsp:useBean id="myu" scope="page" class="com.jspsmart.upload.SmartUpload"/>
<html>
<head>
<title>文件上传处理页面 </title>
<**** http-equiv="content-type" content="text/html; charset=GBK"> 红色部分重复了。去掉下边那一行
你的页面 不支持该属性contenttype ,改成contentType现在你贴出来的是contentType 估计是缓存的问题!
在页面里加入
<meta http-equiv="Expires" CONTENT="0">
<meta http-equiv="Cache-Control" CONTENT="no-cache">
<meta http-equiv="Pragma" CONTENT="no-cache">
然后重启下tomcat再看看! >_<
这句话重复有问题???