Home Java javaTutorial How do the java components SmartUpload and FileUpload implement the file upload function?

How do the java components SmartUpload and FileUpload implement the file upload function?

May 12, 2023 pm 03:04 PM
java smartupload fileupload

1 SmartUpload上传组件

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

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

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

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

上传单个文件

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

必须使用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>
Copy after login

在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>
Copy after login

使用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>
Copy after login

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

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>
Copy after login

表单进行了二进制封装,单靠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<s.length;i++){ //循环设置IP地址
        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()) ;
  }
}
Copy after login

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

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

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

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="org.bug.smart.*"%>
<%@ page import="cn.com.bug.util.*"%>
<html>
<head><title>修改后的smartuoload.jsp</title></head>
<body>
<%
  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)$")%>
<h3>姓名:<%=name%></h3>
<img src="../upload/<%=fileName%>">
</body>
</html>
Copy after login

由于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的文件上传;
}
Copy after login

批量上传

  smart.getFiles().getFile(0).saveAs(this.getServletContext().getRealPath("/")+"upload"+java.io.File.separator + fileName) ;
以上证明可以一次性提交多个上传文件;

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

<html>
<head><title>编写表单,上传3个文件</title></head>
<body>
<form action="smartupload_demo03.jsp" method="post" enctype="multipart/form-data">
  照片1:<input type="file" name="pic1"><br>
  照片2:<input type="file" name="pic2"><br>
  照片3:<input type="file" name="pic3"><br>
  <input type="submit" value="上传">
  <input type="reset" value="重置">
</form>
</body>
</html>
Copy after login

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

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

smartupload_demo03.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") ;
  IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr()) ;  // 取得客户端的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) ;
  }
%>
</body>
</html>
Copy after login

2 FileUpload

FileUpload是Apache组织提供的免费上传组件,可以直接从站点进行下载;

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 : 上传表单

<html>
<head><title>上传表单</title></head>
<body>
<form action="fileupload_demo01.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>
Copy after login

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.*"%>
<html>
<head><title>接收上传文件</title></head>
<body>
<%
  DiskFileItemFactory factory = new DiskFileItemFactory() ;
  ServletFileUpload upload = new ServletFileUpload(factory) ;
  upload.setFileSizeMax(3 * 1024 * 1024) ;  // 只能上传3M
  List<FileItem> items = upload.parseRequest(request) ; // 接收全部内容
  Iterator<FileItem> iter = items.iterator() ;//将全部的内容变为Iterator实例
  while(iter.hasNext()){ //依次取出每一个内容
    FileItem item = iter.next() ; //取出每一个上传的文件
    String fieldName = item.getFieldName() ;  // 取得表单控件的名称
%>
    <ul><h5><%=fieldName%> --> <%=item.isFormField()%></h5>
<%
    if(!item.isFormField()){    // 不是普通文本
      String fileName = item.getName() ;  // 取得文件的名称
      String contentType = item.getContentType() ;  // 文件类型
      long sizeInBytes = item.getSize() ;//文件大小
%>
      <li>上传文件名称:<%=fileName%>
      <li>上传文件类型:<%=contentType%>
      <li>上传文件大小:<%=sizeInBytes%>
<%
    } else {
      String value = item.getString() ;
%>
      <li>普通参数:<%=value%>
<%
    }
%>    </ul>
<%
  }
%>
</body>
</html>
Copy after login

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

但是由于其中既有普通的文本数据又有上传的文件,每一个上传内容都使用一个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>
Copy after login

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>
<%
  DiskFileItemFactory factory = new DiskFileItemFactory() ;
  factory.setRepository(new File(this.getServletContext().getRealPath("/") + "uploadtemp")) ;    // 设置一个临时文件
  ServletFileUpload upload = new ServletFileUpload(factory) ;
  upload.setFileSizeMax(3 * 1024 * 1024) ;  // 只能上传3M
  List<FileItem> items = upload.parseRequest(request) ; // 接收全部内容
  Iterator<FileItem> iter = items.iterator() ; //将全部的内容转换为Iterator实例
  IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr()) ;//实例化IP时间戳对象
  while(iter.hasNext()){//依次取出每一个内容
    FileItem item = iter.next() ; //取出每一个上传的文件
    String fieldName = item.getFieldName() ;  // 取得表单控件的名称
%>
    <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>
Copy after login

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

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

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

FileUpload在建的不便之处:

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

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

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

The above is the detailed content of How do the java components SmartUpload and FileUpload implement the file upload function?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

See all articles