Java web项目01 停车管理项目用户界面上传图片(二)

(一)用户界面实现图片上传
1.1添加图片信息

 <%@include file="/common/sub_header.jsp"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <base href="<%=basePath%>">

        <script language="javascript">
function up() {

    var pop = new Popup( {
        contentType : 1,
        isReloadOnClose : false,
        width : 400,
        height : 200
    });

    pop.setContent("contentUrl", "<%=path%>/upload/upload.jsp");
    pop.setContent("title", "文件上传");
    pop.build();
    pop.show();
}
</script>

    </head>

    <body>
        <jsp:include page="/top.jsp"></jsp:include>
        <div class="container clearfix">
            <jsp:include page="/left.jsp"></jsp:include>
            <!--/sidebar-->
            <div class="main-wrap">

                <div class="crumb-wrap">
                    <div class="crumb-list">
                        <i class="icon-font"></i><span>新增车辆信息</span>
                    </div>
                </div>
                <div class="result-wrap">
                    <div class="result-content">
                        <form method="post" id="myform" name="myform">
                            <table class="insert-tab" width="100%">
                                <tbody>

                                    <tr>
                                        <th>
                                    <i class="require-red"></i>车牌号:
                                        </th>
                                        <td>
                    <input class="common-text required" id="hao" name="hao"
                                                size="50" type="text">
                                        </td>
                                    </tr>

                                    <tr>
                                        <th>
        <i class="require-red"></i>车辆品牌:
                                        </th>
                                        <td>
                                        <input class="common-text required" id="leixing"
                                                name="leixing" size="50" type="text">
                                        </td>
                                    </tr>

                                    <tr>
                                        <th>
                                            <i class="require-red"></i>照片:
                                        </th>
                                        <td>
                                            <input type="text" name="fujian" id="fujian" size="30"
                                                readonly="readonly" />
                    <input type="button" value="上传" onclick="up()" />
                                            <input type="hidden" name="fujian" id="fujian" />
                                        </td>
                                    </tr>

                                    <tr>
                                        <th>
                                            <i class="require-red"></i>备注:
                                        </th>
                                        <td>
                                            <input class="common-text required" id="info" name="info"
                                                size="50" type="text">
                                        </td>
                                    </tr>


                                    <tr>
                                        <th></th>
                                        <td>
                                            <input class="btn btn-primary btn6 mr10" onclick="save();"
                                                value="提交" type="button">
                                            <input class="btn btn6" onclick="history.go(-1)" value="返回"
                                                type="button">
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </form>
                    </div>
                </div>

            </div>
            <!--/main-->
        </div>
    </body>
</html>
<script>

function save() {
    if ($("#hao").val() == "") {
        $.messager.alert('警告', '车牌号不能为空!', 'warning');
        return;
    }
    if ($("#pwd").val() == "") {
        $.messager.alert('警告', '密码不能为空!', 'warning');
        return;
    }
    document.forms[0].action = "<%=path%>/AddCheAction";
    document.forms[0].submit();

}
</script>

效果演示:
这里写图片描述


(二)文件上传界面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <link rel="stylesheet" type="text/css" href="<%=path %>/css/base.css" />
    <script language="javascript">
        function check()
        {
            if(document.formAdd.fujian.value=="")
            {
                alert("请选择文件");
                return false;
            }
            return true;
        }
    </script>
  </head>

  <body>
       <--交个文件上传处理页面upload_re.jsp -->
       <form action="<%=path %>/upload/upload_re.jsp" name="formAdd" method="post"  enctype="multipart/form-data">
           <input type="file" name="fujian" id="fujian" onKeyDown="javascript:alert('此信息不能手动输入');return false;" />
           <input type="submit" value="提交" onclick="return check()"/>
       </form>
  </body>
</html>

(三)文件上传处理upload_re.jsp页面

<%@ page contentType="text/html;charset=utf-8" language="java" import="com.jspsmart.upload.*"%>

<%@ page import="java.util.*"%>
<%
String path = request.getContextPath();
%>
<%

    String newFile1Name=null;
    String file_name=null;

    SmartUpload mySmartUpload = new SmartUpload();

    //初始化上传
    mySmartUpload.initialize(pageContext);

    //只允许上载此类文件
    try 
    {
        //mySmartUpload.setAllowedFilesList("jpg,Jpg,JPG,GIF,gif,Gif,png");
        mySmartUpload.upload();
    } 
    catch (Exception e)
    {
        //out.println("<script language=javascript>alert('上传格式错误!');   history.back(-1);</script>");
        //return;
    }

    try 
    {
           com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
           if (myFile.isMissing())
           {
              out.println("<script language=javascript>alert('必须选择图片!');   history.back(-1);</script>");
              return;

           } 
           else 
           {
               int file_size = myFile.getSize(); //取得文件的大小 (单位是b) 
               file_name=myFile.getFileName();
               System.out.println("文件大小:"+file_size+"文件名称:"+file_name);
               //if (file_size > 10*1024*1024)
               //{
                  //out.println("<script language=javascript>alert('上传图片大小应控制在10K~1M之间!');   history.back(-1);</script>");
                  //return;
               //}
               //else
               //{
                   newFile1Name=new Date().getTime()+file_name.substring(file_name.indexOf("."));
                   System.out.println("新文件名称:"+newFile1Name);

                   String saveurl = request.getSession().getServletContext().getRealPath("upload");

                   saveurl = saveurl+"/"+newFile1Name;
                   myFile.saveAs(saveurl, mySmartUpload.SAVE_PHYSICAL);

              // }
            } 
      } 
      catch (Exception e)
      {
        e.toString();
      }
%>

<script language="javascript">
    document.write("上传成功");
    window.parent.document.getElementById("fujian").value="/upload/<%= newFile1Name%>";
    window.parent.document.getElementById("fujianYuanshiming").value="<%= file_name%>";
</script>

(四)将消息添加到数据库

package com.wang.action;    

import java.io.IOException; 
import java.io.PrintWriter; 
import java.sql.*;  

import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;   
import javax.servlet.http.HttpServletResponse;  

import com.wang.db.DBManager;   

/**
 * 添加汽车
 */
public class AddCheAction extends HttpServlet { 

    private static final long serialVersionUID = 1L;

    public void doPost(HttpServletRequest request, HttpServletResponse response)    
            throws ServletException, IOException {  

        response.setContentType("text/html");   
        PrintWriter out = response.getWriter(); 
        String hao=request.getParameter("hao");
        String leixing=request.getParameter("leixing");
        //1.fujian附件
        String pic=request.getParameter("fujian");
        String info=request.getParameter("info");
        int uid=Integer.parseInt((String)request.getSession().getAttribute("uid"));

        DBManager dbm = new DBManager();    

        //1.添加车辆
        String sql = "insert into che(hao,leixing,pic,info,uid) values('"+hao+"','"+leixing+"','"+pic+"','"+info+"',"+uid+")";  

        Statement stat = null;  
        Connection conn=null;   
        try {   
            conn=dbm.getConnection();   
            stat = conn.createStatement();  
            System.out.println(sql);    
            stat.execute(sql);  
        } catch (SQLException e) {  

            e.printStackTrace();    
        } finally { 
            try {   
                if(stat!=null)  
                    stat.close();   
                if(conn!=null)  
                    conn.close();   
            } catch (SQLException e) {  

                e.printStackTrace();    
            }   
        }   
        response.sendRedirect("che/list.jsp");  
        out.flush();    
        out.close();    
    }   

}   

(五)list.jsp将添加的车辆信息显示处理

<%@include file="/common/sub_header.jsp"%>
<%@ page language="java" import="java.util.*,java.sql.*,com.biyeseng.db.*"
    pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <base href="<%=basePath%>">

        <link rel="stylesheet" type="text/css" href="css/common.css" />
        <link rel="stylesheet" type="text/css" href="css/main.css" />
        <script type="text/javascript" src="js/libs/modernizr.min.js">
</script>
    </head>
    <body>
        <jsp:include page="/top.jsp"></jsp:include>
        <jsp:include page="/left.jsp"></jsp:include>
        <!--/sidebar-->
        <div class="main-wrap">

            <div class="crumb-wrap">
                <div class="crumb-list">
                    <span class="crumb-name">车辆信息</span>
                </div>
            </div>
            <div class="search-wrap">
                <div class="search-content">
                    <form action="<%=path%>/che/list.jsp" method="post">
                        <table class="search-tab">

                        </table>
                    </form>
                </div>
            </div>
            <div class="result-wrap">
                <form name="myform" id="myform" method="post">
                    <div class="result-title">
                        <div class="result-list" id="result-list">
                            <a href="<%=path%>/che/addChe.jsp"><i class="icon-font"></i>添加车辆信息</a>

                        </div>
                    </div>
                    <div class="result-content">
                        <table class="result-tab" width="100%">
                            <tr>


                                <th>
                                    ID
                                </th>
                                <th>
                                    车牌号
                                </th>

                                <th>
                                    车辆品牌
                                </th>

                                <th>
                                    照片
                                </th>

                                <th>
                                    备注
                                </th>

                                <th>
                                    操作
                                </th>
                            </tr>
                            <%
                                DBManager dbm = new DBManager();
                                Connection conn = dbm.getConnection();
                                String sql = "select * from che ";

                                PreparedStatement pstmt = conn.prepareStatement(sql);
                                ResultSet rs = pstmt.executeQuery();

                                while (rs.next()) {
                                    String id = rs.getString("id");
                            %>
                            <tr>


                                <td>
                                    <%=id%>
                <script type="text/javascript">
                                       document.getElementById("result-list").style.display="none";
                                    </script>
                                </td>
                                <td title="">
                                    <%=rs.getString("hao")%>
                                </td>

                                <td title="">
                                    <%=rs.getString("leixing")%>
                                </td>

                                <td title="">
                                    <!--获取数据库中的图片名称  -->
<img src="<%=path%>/<%=rs.getString("pic")%>" height="100px" width="200px"/>
                                </td>

                                <td title="">
                                    <%=rs.getString("info")%>
                                </td>

                                <td>
                                    <a class="link-update"
        href="<%=path%>/che/modChe.jsp?id=<%=id%>">修改</a>
        <a class="link-del" href="<%=path%>/DelCheAction?id=<%=id%>">删除</a>
                                </td>
                            </tr>
                            <%
                                }
                                if (rs != null)
                                    rs.close();
                                if (pstmt != null)
                                    pstmt.close();
                                if (conn != null)
                                    conn.close();
                            %>
                        </table>
                        <div class="list-page">

                        </div>
                    </div>
                </form>
            </div>
        </div>
        <!--/main-->

    </body>
</html>

效果演示:
这里写图片描述

弹出可移动的层,有多种弹出方式 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <script language='javascript' src='pop.js'></script> <TITLE> JS弹出浮动层窗口_演示_www.codefans.net </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY> <Script Language="JavaScript"> function ShowIframe() //显示iframe { var pop=new Popup({ contentType:1,isReloadOnClose:false,width:400,height:500}); pop.setContent("contentUrl","http://www.codefans.net"); pop.setContent("title","框架iframe示例"); pop.build(); pop.show(); } function ShowHtmlString() //显示html { var strHtml = "<table border=1 style='width:90%; text-align:center;'><tr style='height:40px'><td>ds</td><td>dads</td></tr><tr style='height:40px'><td>dadas</td><td>dasd</td></tr><tr style='height:40px'><td>dadasd</td><td>dsadads</td></tr></table>"; var pop=new Popup({ contentType:2,isReloadOnClose:false,width:340,height:300}); pop.setContent("contentHtml",strHtml); pop.setContent("title","字符串示例html"); pop.build(); pop.show(); } function ShowAlert() //显示警示对话框 { var pop=new Popup({ contentType:4,isReloadOnClose:true,width:340,height:80}); pop.setContent("title","源码爱好者警告框示例"); pop.setContent("alertCon","您好!欢迎来到源码爱好者!"); pop.build(); pop.show(); } function ShowConfirm() //显示确认对话框 { var pop=new Popup({ contentType:3,isReloadOnClose:false,width:340,height:80}); pop.setContent("title","对话框示例confirm"); pop.setContent("confirmCon","您好!您要关闭源码爱好者吗??"); pop.setContent("callBack",ShowCallBack); pop.setContent("parameter",{id:"divCall",str:"点击确定后显示的字符串",obj:pop}); pop.build(); pop.show(); } function ShowCallBack(para) //回调函数 { var o_pop = para["obj"] var obj = document.getElementById(para["id"]); o_pop.close(); obj.innerText = para["str"]; } </Script> <INPUT TYPE="button" value="ShowHtmlString()" onclick="ShowHtmlString();"> <br> <INPUT TYPE="button" value="ShowIframe()" onclick="ShowIframe();"> <br> <INPUT TYPE="button" value="ShowAlert()" onclick="ShowAlert();"> <br> <INPUT TYPE="button" value="ShowConfirm()" onclick="ShowConfirm();"> </BODY> </HTML>
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值