java使用smartupload组件实现文件上传的方法(2)_JSP教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐:jsp利用application统计在线人数的方法
这篇文章主要介绍了jsp利用application统计在线人数的方法,代码中备有较为详尽的注释便于理解,是比较实用的技巧,需要的朋友可以参考下 本文实例讲述了jsp利用application统计在线人数的方法。分享给大家供大家参考。 具体实现方法如下: 代码如下:%@ page language=jav

smartupload是由www.jspsmart.com网站开发的一套上传组件包,可以轻松的实现文件的上传及下载功能,smartupload组件使用简单、可以轻松的实现上传文件类型的限制、也可以轻易的取得上传文件的名称、后缀、大小等。

smartupload本身是一个系统提供的jar包(smartupload.jar),用户直接将此包放到classpath下即可,也可以直接将此包拷贝到TOMCAT_HOME\lib目录之中。

下面使用组件完成上传

单一文件上传:

代码如下: <html>
<head><title>smartupload组件上传</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
<body>
<form action="smartupload_demo01.jsp" method="post" enctype="multipart/form-data">
图片<input type="file" name="pic">
<input type="submit" value="上传">
</form>
</body>
</html>

 

jsp代码:

smartupload_demo01.jsp

代码如下: <%@ page contentType="text/html" pageEncoding="utf-8"%>
<%@ page import="com.jspsmart.upload.*" %>
<html>
<head><title>smartupload组件上传01</title></head>

 

<body>
<%
SmartUpload smart = new SmartUpload() ;
smart.initialize(pageContext) ; // 初始化上传操作
smart.upload(); // 上传准备
smart.save("upload") ; // 文件保存
out.print("上传成功");
%>

</body>
</html>

 

批量上传:

html文件

 

代码如下: <html>
<head><title>smartupload组件上传02</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
<body>
<form action="smartupload_demo02.jsp" method="post" enctype="multipart/form-data">
图片<input type="file" name="pic1"><br>
图片<input type="file" name="pic2"><br>
图片<input type="file" name="pic3"><br>
<input type="submit" value="上传">
<input type="reset" value="重置">
</form>
</body>
</html>

 

jsp代码

smartupload_demo02.jsp

代码如下: <%@ page contentType="text/html" pageEncoding="utf-8"%>
<%@ page import="com.jspsmart.upload.*"%>
<%@ page import="com.zhou.study.*"%>
<html>
<head><title>smartupload组件上传02</title></head>
<body>
<%
SmartUpload smart = new SmartUpload() ;
smart.initialize(pageContext) ; // 初始化上传操作
smart.upload() ; // 上传准备
String name = smart.getRequest().getParameter("uname") ;
IPTimeStamp its = new IPTimeStamp("192.168.1.1") ; // 取得客户端的IP地址
for(int x=0;x<smart.getFiles().getCount();x++){
String ext = smart.getFiles().getFile(x).getFileExt() ; // 扩展名称
String fileName = its.getIPTimeRand() + "." + ext ;
smart.getFiles().getFile(x).saveAs(this.getServletContext().getRealPath("/")+"upload"+java.io.File.separator + fileName) ;
}
out.print("上传成功");
%>
</body>
</html>

 

注意:在TOMCAT_HOME/项目目录下建立upload文件夹才能正常运行!

简单上传操作上传后的文件名称是原本的文件名称。可通过工具类重命名。

分享:jsp分页显示完整实例
这篇文章主要介绍了jsp分页显示完整实例,以文章管理页面为例详细分析了jsp的分页显示实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下 本文实例讲述了jsp分页显示的实现方法。分享给大家供大家参考。 具体实现方法如下: 代码如下:%@ page contentType=text/html;

来源:模板无忧//所属分类:JSP教程/更新时间:2015-01-31
相关JSP教程