jspsmart组件上传
作者:admin 日期:2007-08-10
例1:
-------------------------------------------------------------------------
<%@ page contentType="text/html; charset=gb2312" language="Java"
import="java.util.*,com.jspsmart.upload.*" errorPage="" %>
<%@ page import="java.util.Date,java.text.SimpleDateFormat" %>
<html>
<head>
<title>文件上传处理页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
if(request.getMethod().equals("POST")){
//错误提示
String ok = "<script language=\"javascript\">alert(\"上传成功\");</script>";
String fail = "<script language=\"JavaScript\">alert(\"上传失败!请上传gif、jpg、bmp格式!文件不能大于1M\");</script>";
// 新建一个SmartUpload对象
SmartUpload su = new SmartUpload();
// 上传初始化
su.initialize(pageContext);
// 设定上传限制
// 1.限制每个上传文件的最大长度。
// su.setMaxFileSize(10000);
// 2.限制总上传数据的长度。
// su.setTotalMaxFileSize(20000);
// 3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
//su.setAllowedFilesList("jpg,gif,JPG,GIF,bmp,BMP");
// 4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,jsp,htm,html扩展名的文件和没有扩展名的文件。
// su.setDeniedFilesList("exe,bat,jsp,htm,html,,");
// 上传文件
try {
//过滤
su.setAllowedFilesList("jpg,gif,JPG,GIF,bmp,BMP");
su.setMaxFileSize(3024000);
su.upload();
// 将上传文件全部保存到指定目录
//生成时间
SimpleDateFormat df=new SimpleDateFormat("yyyyMMdd-HH:mm:ss");
String s=df.format(new Date());
s=s.replace(":","");
//生成扩展名
String temp=su.getFiles().getFile(0).getFileExt();
//生成文件
su.getFiles().getFile(0).saveAs("/upload/"+s+"."+temp);
//out.print(su.getFiles().getFile(0).getFileExt());
out.print(ok);
} catch (Exception e) {
log(e.getMessage());
out.print(fail);
}
}
%>
</body>
</html>
例2:
-------------------------------------------------------------------------
一个上传文件的例子,需要使用jspsmartupload.jar
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page language="java" import="com.jspsmart.upload.*"%>
<%
if(request.getMethod().equals("POST")){
String ok = "<script language=\"javascript\">alert(\"上传成功\");</script>";
String fail = "<script language=\"JavaScript\">alert(\"上传失败!请上传gif、jpg格式!文件不能大于1M\");</script>";
String path="e:/abc/";
try {
SmartUpload uploader=new SmartUpload();
uploader.initialize(pageContext);//初始化
uploader.setMaxFileSize(3024000);
uploader.setAllowedFilesList("jpg,gif,JPG,GIF");
uploader.upload();
uploader.save(path);
out.print(ok);
} catch (Exception e) {
log(e.getMessage());
out.print(fail);
}
}
%>
<html>
<form action="upload.jsp" enctype="multipart/form-data" method="post" >
<td height="20" colspan="2">从本地导入图片:<input type="file" name="picfile" size="20"><input type="submit" value=" 上传 ">
</td>
</form>
</html>
例3(节选部分,主要是改扩展名):
-------------------------------------------------------------------------
<%@page language="java" contentType="text/html; charset=gb2312" pageEncoding="GB2312"%>
<%@page import="com.jspsmart.upload.*,java.util.*"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload"/>
<%
String saveattachpath = String.valueOf("d:/"+System.currentTimeMillis()+"/"); //设置保存文件的路径.
mySmartUpload.getFiles().getFile(i).saveAs(savedfilepath+mySmartUpload.getFiles().getFile(i).getFileName());
%>
点击下载此组件
-------------------------------------------------------------------------
<%@ page contentType="text/html; charset=gb2312" language="Java"
import="java.util.*,com.jspsmart.upload.*" errorPage="" %>
<%@ page import="java.util.Date,java.text.SimpleDateFormat" %>
<html>
<head>
<title>文件上传处理页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
if(request.getMethod().equals("POST")){
//错误提示
String ok = "<script language=\"javascript\">alert(\"上传成功\");</script>";
String fail = "<script language=\"JavaScript\">alert(\"上传失败!请上传gif、jpg、bmp格式!文件不能大于1M\");</script>";
// 新建一个SmartUpload对象
SmartUpload su = new SmartUpload();
// 上传初始化
su.initialize(pageContext);
// 设定上传限制
// 1.限制每个上传文件的最大长度。
// su.setMaxFileSize(10000);
// 2.限制总上传数据的长度。
// su.setTotalMaxFileSize(20000);
// 3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
//su.setAllowedFilesList("jpg,gif,JPG,GIF,bmp,BMP");
// 4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,jsp,htm,html扩展名的文件和没有扩展名的文件。
// su.setDeniedFilesList("exe,bat,jsp,htm,html,,");
// 上传文件
try {
//过滤
su.setAllowedFilesList("jpg,gif,JPG,GIF,bmp,BMP");
su.setMaxFileSize(3024000);
su.upload();
// 将上传文件全部保存到指定目录
//生成时间
SimpleDateFormat df=new SimpleDateFormat("yyyyMMdd-HH:mm:ss");
String s=df.format(new Date());
s=s.replace(":","");
//生成扩展名
String temp=su.getFiles().getFile(0).getFileExt();
//生成文件
su.getFiles().getFile(0).saveAs("/upload/"+s+"."+temp);
//out.print(su.getFiles().getFile(0).getFileExt());
out.print(ok);
} catch (Exception e) {
log(e.getMessage());
out.print(fail);
}
}
%>
</body>
</html>
例2:
-------------------------------------------------------------------------
一个上传文件的例子,需要使用jspsmartupload.jar
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page language="java" import="com.jspsmart.upload.*"%>
<%
if(request.getMethod().equals("POST")){
String ok = "<script language=\"javascript\">alert(\"上传成功\");</script>";
String fail = "<script language=\"JavaScript\">alert(\"上传失败!请上传gif、jpg格式!文件不能大于1M\");</script>";
String path="e:/abc/";
try {
SmartUpload uploader=new SmartUpload();
uploader.initialize(pageContext);//初始化
uploader.setMaxFileSize(3024000);
uploader.setAllowedFilesList("jpg,gif,JPG,GIF");
uploader.upload();
uploader.save(path);
out.print(ok);
} catch (Exception e) {
log(e.getMessage());
out.print(fail);
}
}
%>
<html>
<form action="upload.jsp" enctype="multipart/form-data" method="post" >
<td height="20" colspan="2">从本地导入图片:<input type="file" name="picfile" size="20"><input type="submit" value=" 上传 ">
</td>
</form>
</html>
例3(节选部分,主要是改扩展名):
-------------------------------------------------------------------------
<%@page language="java" contentType="text/html; charset=gb2312" pageEncoding="GB2312"%>
<%@page import="com.jspsmart.upload.*,java.util.*"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload"/>
<%
String saveattachpath = String.valueOf("d:/"+System.currentTimeMillis()+"/"); //设置保存文件的路径.
mySmartUpload.getFiles().getFile(i).saveAs(savedfilepath+mySmartUpload.getFiles().getFile(i).getFileName());
%>
点击下载此组件
[本日志由 admin 于 2007-08-10 01:32 AM 编辑]
文章来自: 本站原创
引用通告地址: http://www.is21.cn/trackback.asp?tbID=8
Tags:
文章来自: 本站原创
引用通告地址: http://www.is21.cn/trackback.asp?tbID=8
Tags:
评论: 0 | 引用: 0 | 查看次数: 1909
发表评论
你没有权限发表留言!