zoukankan      html  css  js  c++  java
  • JSP文件上传--Smartupload组件

    把smartupload.jar copy到D:apache-tomcat-7.0.57lib下。

    创建htm上传文件:smartupload_demo01.htm

    由于是上传文件,需要对表单进行封装: enctype="multipart/form-data"

    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</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>

    表单完成就可以上传,但是需要在D:Workspace下新建一个upload文件夹,保存所有上传的文件

    完成后,要想上传,需要按照以下步骤:

    1. 实例化SmartUpload对象

    2. 初始化上传的操作

    3. 准备上传

    4. 保存文件

    smartupload_demo01.jsp:

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <%@ page import="org.lxh.smart.*"%>
    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
    <body>
    <%
        SmartUpload smart = new SmartUpload() ;
        smart.initialize(pageContext) ;    // 初始化上传操作
        smart.upload() ;            // 上传准备
        smart.save("upload") ;    // 文件保存
    %>
    </body>
    </html>

    就会发现,文件已经保存在:D:Workspaceupload 文件夹里。

    特点:原本文件名没有改变。

    现在也有个问题,如果进行文件上传,就需要对表单进行封装,但是表单一旦封装后,就无法使用request.getParameter()接收参数。

    smartupload_demo02.htm,传送一个name会怎么样?

    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</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

    由于表单被封装,表单无法接收。所有的数据不再是文本,而是二进制的byte流,此时要接收,使用SmartUpload的方法来获得,见代码:smart.getreQuest().getParameter()

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <%@ page import="org.lxh.smart.*"%>
    <%@ page import="cn.mldn.lxh.util.*"%>
    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</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)$")%>
    <h2>姓名:<%=name%></h2>
    <img src="../upload/<%=fileName%>">
    </body>
    </html>

    程序中还有一个问题,如果多个用户上传的文件名一样,就会发生覆盖的现象。为解决这个,采用自动命名的方式

    IP地址+时间戳+三位随机数

    如果IP地址是 192.168.12.19,日期是2009-09-19 21:15:35.123,三位随机数是 678

    拼凑出来的文件名就是 19216801201920080919211535123678.文件后缀

    需要一个类IpTimeStamp.java:

    package cn.mldn.lxh.util ;
    import java.text.SimpleDateFormat ;
    import java.util.Date ;
    import java.util.Random ;
    public class IPTimeStamp {
        private SimpleDateFormat sdf = null ;
        private String ip = null ;
        public IPTimeStamp(){
        }
        public IPTimeStamp(String ip){
            this.ip = ip ;
        }
        public String getIPTimeRand(){
            StringBuffer buf = new StringBuffer() ;
            if(this.ip != null){
                String s[] = this.ip.split("\.") ;
                for(int i=0;i<s.length;i++){
                    buf.append(this.addZero(s[i],3)) ;
                }
            }
            buf.append(this.getTimeStamp()) ;
            Random r = new 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){
            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()) ;
        }
    }

    不管上传多少个文件都是利用getFiles()完成的,以下例子就可以上传多个文件:

    smartupload_demo03.htm

    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</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>

    smartupload_demo03.jsp:

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <%@ page import="org.lxh.smart.*"%>
    <%@ page import="cn.mldn.lxh.util.*"%>
    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</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>

    通过SmartUpload上传比较方便,FileUpload比较难。

  • 相关阅读:
    python 并发编程 多线程 event
    python 并发编程 多线程 定时器
    python 并发编程 多线程 信号量
    linux top 查看CPU命令
    python 并发编程 多线程 GIL与多线程
    python 并发编程 多线程 死锁现象与递归锁
    python 并发编程 多线程 GIL与Lock
    python GIL全局解释器锁与互斥锁 目录
    python 并发编程 多线程 GIL全局解释器锁基本概念
    执行python程序 出现三部曲
  • 原文地址:https://www.cnblogs.com/wujixing/p/4961484.html
Copyright © 2011-2022 走看看