(一)用户界面实现图片上传
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>
效果演示: