java组件SmartUpload和FileUpload实现文件上传功能

网友投稿 298 2023-03-17


java组件SmartUpload和FileUpload实现文件上传功能

本文为大家分享了java组件实现文件上传功能的具体代码,供大家参考,具体内容如下

1 SmartUpload上传组件

SmartUpload上传组件包,可以轻松的实现文件的上传和下载功能;

使用简单,实现上传文件类型的限制,也可以轻易的取得上传文件的名称、后缀、大小等;

SmartUpload本身是系统提供的jar包,将此包考入到lib文件夹中;

此组件的提供方网站已关闭,SmartUpload在非框架中开发中较为好用;

上传单个文件

要进行上传,必须使用HTML中提供给的file空间,而且

smartupload_demo01.html : 上传表单

请选择要上传的文件:

在form上使用enctype进行了表单封装,表示表单将按二进制的方式提交,即所有的表单此时不在是分别提交,而是将所有的内容都按照二进制的方式提交;

smartupload_demo01.jsp : 接收图片,保存在根目录中的upload文件夹中

<%@ page contentType="text/html" pageEncoding="GBK"%>

<%@ page import="org.bug.smart.*"%>

<%

SmartUpload smart = new SmartUpload() ;

smart.initialize(pageContext) ; // 初始化上传操作

smart.upload() ; // 上传准备

smart.save("upload") ; // 文件保存

%>

使用SmartUpload时必须严格按照如上程序进行,最后在保存时只是写了一个upload,表示上传文件的保存文件夹,此文件要在根目录中手工建立;

保存的名称和上传的文件一样,所以如果出现相同的文件名称,将出现覆盖的情况;

混合表单

当一个表单使用了enctyoe封装后,其它文件类的表单控件的内容将无法通过request内置对象取得;

此时,必须通过SmartUpload类中提供的getRequest()方法取得全部的请求参数;

smartupload_demo02.html ; 混合表单

姓名:

照片:

以上表单中包含了文本和文件两个控件;

smartupload_demo02.jsp : 接收封装表单的文本数据

<%@ page contentType="text/html" pageEncoding="GBK"%>

<%@ page import="org.bug.smart.*"%>

<%@ page import="cn.com.bug.util.*"%>

<%

request.setCharacterEncoding("GBK") ;

%>

<%

SmartUpload smart = new SmartUpload() ;

smart.initialize(pageContext) ; // 初始化上传操作

smart.upload() ; // 上传准备

String name = smart.getRequest().getParameter("uname") ;

smart.upload("upload");%>

表单进行了二进制封装,单靠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

buf.append(this.addZero(s[i],3)) ; //不够三位要补0

}

}

buf.append(this.getTimeStamp()) ; //取得时间戳

Random r = new Random() ; //定义Random对象,已产生随机数

for(int i=0;i<3;i++){ //循环三次

buf.append(r.nextInt(10)) ; //增加随机数

}

return buf.toString() //返回名称

}

public String getDate(){ //取得当前系统的时间

this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") ;

return this.sdf.format(new Date()) ;

}

public String getTimeStamp(){ //取得时间戳

this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS") ;

return this.sdf.format(new Date()) ;

}

private String addZero(String str,int len){ //补0操作

StringBuffer s = new StringBuffer() ;

s.append(str) ;

while(s.length() < len){

s.insert(0,"0") ;

}

return s.toString() ;

}

public static void main(String args[]){

System.out.println(new IPTimeStamp("192.168.1.1").getIPTimeRand()) ;

}

}

直接修改上传的操作页,在上传的操作页中不直接使用save()方法保存;

而是取得一个具体上传文件对象才可以保存,由于上传文件时文件的后缀需要统一,所以可以使用getFileExt()方法取得文件的后缀;

smartupload_demo02.jsp : 增加自动命名的功能;

<%@ page contentType="text/html" pageEncoding="GBK"%>

<%@ page import="org.bug.smart.*"%>

<%@ page import="cn.com.bug.util.*"%>

修改后的smartuoload.jsp

buf.append(this.addZero(s[i],3)) ; //不够三位要补0

}

}

buf.append(this.getTimeStamp()) ; //取得时间戳

Random r = new Random() ; //定义Random对象,已产生随机数

for(int i=0;i<3;i++){ //循环三次

buf.append(r.nextInt(10)) ; //增加随机数

}

return buf.toString() //返回名称

}

public String getDate(){ //取得当前系统的时间

this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") ;

return this.sdf.format(new Date()) ;

}

public String getTimeStamp(){ //取得时间戳

this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS") ;

return this.sdf.format(new Date()) ;

}

private String addZero(String str,int len){ //补0操作

StringBuffer s = new StringBuffer() ;

s.append(str) ;

while(s.length() < len){

s.insert(0,"0") ;

}

return s.toString() ;

}

public static void main(String args[]){

System.out.println(new IPTimeStamp("192.168.1.1").getIPTimeRand()) ;

}

}

直接修改上传的操作页,在上传的操作页中不直接使用save()方法保存;

而是取得一个具体上传文件对象才可以保存,由于上传文件时文件的后缀需要统一,所以可以使用getFileExt()方法取得文件的后缀;

smartupload_demo02.jsp : 增加自动命名的功能;

<%@ page contentType="text/html" pageEncoding="GBK"%>

<%@ page import="org.bug.smart.*"%>

<%@ page import="cn.com.bug.util.*"%>

<%

request.setCharacterEncoding("GBK") ;

%>

<%

SmartUpload smart = new SmartUpload() ;

smart.initialize(pageContext) ; // 初始化上传操作

smart.upload() ; // 上传准备

String name = smart.getRequest().getParameter("uname") ;

IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr()) ; // 取得客户端的IP地址

String ext = smart.getFiles().getFile(0).getFileExt() ; // 扩展名称

String fileName = its.getIPTimeRand() + "." + ext ;

smart.getFiles().getFile(0).saveAs(this.getServletContext().getRealPath("/")+"upload"+java.io.File.separator + fileName) ;

%>

<%=smart.getFiles().getFile(0).getFileName().matches("^\\w+.(jpg|gif)$")%>

由于SmartUpload可以同时接收多个文件;

此时通过smart.getFiles().getFile(0).getFileExt()取得第一个上传文件的文件后缀,在与之前IPTimeStampl类生成的文件名称一起拼凑出一个新的文件名;

此时要用的新的文件名称保存上传文件,所以要通过smart.getFiles().getFile(0).saveAs()方法进行收工保存;

以上代码没有对上传文件的类型进行限制,可以通过正则判断文件的后缀是否合法;

eg:验证上传文件的合法性代码片段

if(smart.getFiles().getFile(0).getFileName().matches("^\\w+\\.(jsp|gif)$")){

//表示只允许后缀为jpg或gif的文件上传;

}

批量上传

smart.getFiles().getFile(0).saveAs(this.getServletContext().getRealPath("/")+"upload"+java.io.File.separator + fileName) ;

以上证明可以一次性提交多个上传文件;

smartupload_demo03.jsp : 编写表单,上传3个文件

照片1:

照片2:

照片3:

如果要完成批量上传,则肯定要使用循环的方式进行,必须通过以下方式取得上传文件数量;

取得上传文件的数量:smart.getFiles().getCount();

smartupload_demo03.jsp : 批量上传

<%@ page contentType="text/html" pageEncoding="GBK"%>

<%@ page import="org.bug.smart.*"%>

<%@ page import="cn.com.bug.util.*"%>

<%

request.setCharacterEncoding("GBK") ;

%>

<%

SmartUpload smart = new SmartUpload() ;

smart.initialize(pageContext) ; // 初始化上传操作

smart.upload() ; // 上传准备

String name = smart.getRequest().getParameter("uname") ;

IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr()) ; // 取得客户端的IP地址

for(int x=0;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) ;

}

%>

2 FileUpload

FileUpload是Apache组织提供的免费上传组件,可以直接从站点进行下载(http://commons.apache.org/fileupload/);

FileUpload组件本身依赖于Commons组件包,所以从Apache下载此组件的同时连同Commons组件的IO包一起下载(http://commons.apache.org/io);

Commons组件包在很多框架开发中都可以直接使用,此包中提供了大量开发类可作为java的有力补充;

将Commons-fileupload-1.2.1.jar和Common-io-1.4.jar配置到lib文件夹中;

使用FileUpload接收上传内容

不论是使用SmartUpload和FileUpload进行上传操作,都是依靠HTML的file控件完成的;

fileupload_demo01.html : 上传表单

上传表单

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) ;

}

%>

2 FileUpload

FileUpload是Apache组织提供的免费上传组件,可以直接从站点进行下载(http://commons.apache.org/fileupload/);

FileUpload组件本身依赖于Commons组件包,所以从Apache下载此组件的同时连同Commons组件的IO包一起下载(http://commons.apache.org/io);

Commons组件包在很多框架开发中都可以直接使用,此包中提供了大量开发类可作为java的有力补充;

将Commons-fileupload-1.2.1.jar和Common-io-1.4.jar配置到lib文件夹中;

使用FileUpload接收上传内容

不论是使用SmartUpload和FileUpload进行上传操作,都是依靠HTML的file控件完成的;

fileupload_demo01.html : 上传表单

姓名:

照片:

FileUpload的具体上传操作与SmartUpload相比有着很高的复杂度;

以下是FileUpload上传的步骤:

1 创建磁盘工厂:DiskFileItemFactory factory = new DiskFileFactory();

2 创建处理工具:ServletFileUpload upload =new ServletFileUpload(factory);

3 设置文件的上传大小:upload.setFileSizeMax(3145728);

4 接收全部的内容:List items = upload.parseRequest(request);

FileUpload对所有的上传内容都采用同样的方式操作;

与SmartUpload不同的是,会将所有的上传内容一起(包括文件和普通参数)接收;

所以需要依次判断你每一次上传的内容是文件还是普通文本;

fileupload_demo01.jsp : 接收上传文件

<%@ page contentType="text/html" pageEncoding="GBK"%>

<%@ page import="java.util.*"%>

<%@ page import="org.apache.commons.fileupload.*"%>

<%@ page import="org.apache.commons.fileupload.disk.*"%>

<%@ page import="org.apache.commons.fileupload.servlet.*"%>

<%

DiskFileItemFactory factory = new DiskFileItemFactory() ;

ServletFileUpload upload = new ServletFileUpload(factory) ;

upload.setFileSizeMax(3 * 1024 * 1024) ; // 只能上传3M

List items = upload.parseRequest(request) ; // 接收全部内容

Iterator iter = items.iterator() ;//将全部的内容变为Iterator实例

while(iter.hasNext()){ //依次取出每一个内容

FileItem item = iter.next() ; //取出每一个上传的文件

String fieldName = item.getFieldName() ; // 取得表单控件的名称

%>

<%

if(!item.isFormField()){ // 不是普通文本

String fileName = item.getName() ; // 取得文件的名称

String contentType = item.getContentType() ; // 文件类型

long sizeInBytes = item.getSize() ;//文件大小

%>

<%

} else {

String value = item.getString() ;

%>

<%

}

%>

<%

}

%>

FileUpload组件接收完全部的数据后,所有的数据都保存在List集合中,则需要使用Iterator取出每一个数据;

但是由于其中既有普通的文本数据又有上传的文件,每一个上传内容都使用一个FileItem类对象表示;

所以当使用Iterator依次取出每一个FileItem对象时,就可以使用FileItem类中的isFormField()方法来判断当前操作的内容是普通的文本还是附件上传;

如果是上传文件,则将文件的内容依次取出;如果是普通的文本,则直接通过getString()方法取得具体的信息;

保存上传内容

以上完成了接收上传文件内容的操作,但是所上传的文件现在并没有真正的保存在服务器上;

而要进行文件的保存,在FileUpload中就必须通过java.io包中InputStream和outputStream两个类完成文件的自动命名操作;

InputStream和OutputStream为两个抽象类,必须依靠FileInputStream和OutputStream类进行对象的实例化操作;

fileupload_demo02.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.*"%>

<%

DiskFileItemFactory factory = new DiskFileItemFactory() ;

factory.setRepository(new File(this.getServletContext().getRealPath("/") + "uploadtemp")) ; // 设置一个临时文件

ServletFileUpload upload = new ServletFileUpload(factory) ;

upload.setFileSizeMax(3 * 1024 * 1024) ; // 只能上传3M

List items = upload.parseRequest(request) ; // 接收全部内容

Iterator iter = items.iterator() ; //将全部的内容转换为Iterator实例

IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr()) ;//实例化IP时间戳对象

while(iter.hasNext()){//依次取出每一个内容

FileItem item = iter.next() ; //取出每一个上传的文件

String fieldName = item.getFieldName() ; // 取得表单控件的名称

%>

<%

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() ;//取出表单的内容

%>

<%

}

%>

<%

}

%>

以上代码中,首先会将所有的上传文件设置到临时文件夹中;

如果发现取得的表单是上传文件,则使用InputStream,从FileItem类中取得文件的输入流;

在使用OutputStream将内容依次取出,保存在具体的文件路径中;

FileUpload在建的不便之处:

1 无法像使用request.getParameter()方法那样准确WHFFaLfmXJ地取得提交的参数;

2 无法像使用request.getParameterValues()那样准确的取得一组提交参数;

3 所有的上传文件度需要进行一次的判断,才能够分别保存,不能一次性批量保存;


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java实现TFIDF算法代码分享
下一篇:spring入门教程之bean的继承与自动装配详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~