smartupload上传组件包,可以轻松的实现文件的上传和下载功能;
使用简单,实现上传文件类型的限制,也可以轻易的取得上传文件的名称、后缀、大小等;
smartupload本身是系统提供的jar包,将此包考入到lib文件夹中;
此组件的提供方网站已关闭,smartupload在非框架中开发中较为好用;
上传单个文件
要进行上传,必须使用html中提供给的file空间,而且<form>必须使用enctype属性进行封装;
smartupload_demo01.html : 上传表单
<html><head><title>上传表单</title></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>
在form上使用enctype进行了表单封装,表示表单将按二进制的方式提交,即所有的表单此时不在是分别提交,而是将所有的内容都按照二进制的方式提交;
smartupload_demo01.jsp : 接收图片,保存在根目录中的upload文件夹中
<%@ page contenttype="text/html" pageencoding="gbk"%><%@ page import="org.bug.smart.*"%><html><head><title>接收图片,保存在根目录中的upload文件夹中</title></head><body><% smartupload smart = new smartupload() ; smart.initialize(pagecontext) ; // 初始化上传操作 smart.upload() ; // 上传准备 smart.save("upload") ; // 文件保存%></body></html>
使用smartupload时必须严格按照如上程序进行,最后在保存时只是写了一个upload,表示上传文件的保存文件夹,此文件要在根目录中手工建立;
保存的名称和上传的文件一样,所以如果出现相同的文件名称,将出现覆盖的情况;
混合表单
当一个表单使用了enctyoe封装后,其它文件类的表单控件的内容将无法通过request内置对象取得;
此时,必须通过smartupload类中提供的getrequest()方法取得全部的请求参数;
smartupload_demo02.html ; 混合表单
<html><head><title>混合表单</title></head><body><form action="smartupload_demo02.jsp" method="post" enctype="multipart/form-data"> 姓名:<input type="text" name="uname"><br> 照片:<input type="file" name="pic"><br> <input type="submit" value="上传"> <input type="reset" value="重置"></form></body></html>
以上表单中包含了文本和文件两个控件;
smartupload_demo02.jsp : 接收封装表单的文本数据
<%@ page contenttype="text/html" pageencoding="gbk"%><%@ page import="org.bug.smart.*"%><%@ page import="cn.com.bug.util.*"%><html><head><title>接收封装表单的文本数据</title></head><body><% request.setcharacterencoding("gbk") ;%><% smartupload smart = new smartupload() ; smart.initialize(pagecontext) ; // 初始化上传操作 smart.upload() ; // 上传准备 string name = smart.getrequest().getparameter("uname") ; smart.upload("upload");%><h3>姓名:<%=name%></h3><h3>request无法取得 : <%=request.getparameter("uname")%> </h3></body></html>
表单进行了二进制封装,单靠request对象是无法取得提交参数的,必须依靠smartupload类中的getrequest().getparameter()方法才能取得请求的参数;
由于是通过smartupload完成参数接收,所以smart.getrequest()方法一定要在执行完upload()方法后才可使用;
为上传文件自动命名
为了解决文件名称相同而出现覆盖的情况,可以采用为上传文件自动命名的方式;
自动命名可采用格式: ip地址+时间戳+三位随机数
iptimestamp.java : 定义取得ip时间戳的操作类
package cn.com.bug.util ;import java.text.simpledateformat ;import java.util.date ;import java.util.random ;public class iptimestamp { private simpledateformat sdf = null ; //定义simpledateformat对象 private string ip = null ; //接收ip地址 public iptimestamp(){ } public iptimestamp(string ip){ //得到ip地址+时间戳+三位随机数 this.ip = ip ; } public string getiptimerand(){ stringbuffer buf = new stringbuffer() ; //实例化stringbuffer对象 if(this.ip != null){ string s[] = this.ip.split(\\.) ; //进行拆分操作 for(int i=0;i
但是由于其中既有普通的文本数据又有上传的文件,每一个上传内容都使用一个fileitem类对象表示;
所以当使用iterator依次取出每一个fileitem对象时,就可以使用fileitem类中的isformfield()方法来判断当前操作的内容是普通的文本还是附件上传;
如果是上传文件,则将文件的内容依次取出;如果是普通的文本,则直接通过getstring()方法取得具体的信息;
保存上传内容
以上完成了接收上传文件内容的操作,但是所上传的文件现在并没有真正的保存在服务器上;
而要进行文件的保存,在fileupload中就必须通过java.io包中inputstream和outputstream两个类完成文件的自动命名操作;
inputstream和outputstream为两个抽象类,必须依靠fileinputstream和outputstream类进行对象的实例化操作;
fileupload_demo02.html : 定义上传表单,可以同时上传多个文件
<html><head><title>定义表单,可以同时上传多个文件</title></head><body><form action="fileupload_demo02.jsp" method="post" enctype="multipart/form-data"> 姓名:<input type="text" name="uname"><br> 照片:<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>
fileupload_demo02.jsp : 保存上传的内容
<%@ page contenttype="text/html" pageencoding="gbk"%><%@ page import="java.util.*,java.io.*"%><%@ page import="org.apache.commons.fileupload.*"%><%@ page import="org.apache.commons.fileupload.disk.*"%><%@ page import="org.apache.commons.fileupload.servlet.*"%><%@ page import="cn.com.bug.util.*"%><html><head><title>保存上传内容</title></head><body> <ul><h5><%=fieldname%> --> <%=item.isformfield()%></h5><% if(!item.isformfield()){ // 不是普通文本,是上传文件 file savefile = null ; //定义保存的文件 inputstream input = null ; outputstream output = null ; input = item.getinputstream() ; output = new fileoutputstream(new file(this.getservletcontext().getrealpath("/")+"upload"+file.separator+its.getiptimerand()+ "."+item.getname().split("\\.")[1])) ;//定义输入文件的路径 int temp = 0 ; byte data[] = new byte[512] ; while((temp=input.read(data,0,512))!=-1){ //依次读取内容 output.write(data) ; // 分块保存 } input.close() ; output.close() ; } else { string value = item.getstring() ;//取出表单的内容%> <li>普通参数:<%=value%><% }%> </ul><% }%></body></html>
以上代码中,首先会将所有的上传文件设置到临时文件夹中;
如果发现取得的表单是上传文件,则使用inputstream,从fileitem类中取得文件的输入流;
在使用outputstream将内容依次取出,保存在具体的文件路径中;
fileupload在建的不便之处:
1 无法像使用request.getparameter()方法那样准确地取得提交的参数;
2 无法像使用request.getparametervalues()那样准确的取得一组提交参数;
3 所有的上传文件度需要进行一次的判断,才能够分别保存,不能一次性批量保存;
以上就是java组件smartupload和fileupload怎么实现文件上传功能的详细内容。