(jsp需要引入 :jquery-1.9.0.js、jquery.form.js ) ,jsp页面使用的是bootstrap制作的,看不懂的标签不用管,form表单大同小异。代码比较简陋,只是为了演示使用ajaxSubmit异步上传图片及保存数据,请海含!
(参考文献:https://www./shouce/jquery/jquery_api/Plugins/Form/ajaxSubmit.html)
一:web (add.jsp)
复制代码 代码如下:
<%@page import="com.fingerknow.project.vo.UserInformation"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="ctx" value="${pageContext.request.contextPath }" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>注册商圈</title>
<link href="${ctx}/bootstrap/css/bootstrap.css" rel="stylesheet">
<link href="${ctx}/bootstrap/css/bootstrap-responsive.css" rel="stylesheet">
<link rel="stylesheet" href="${ctx}/css/bootstrap-responsive.min.css" />
<link rel="stylesheet" href="${ctx}/css/jquery-ui.css" />
<link rel="stylesheet" href="${ctx}/css/uniform.css" />
<link rel="stylesheet" href="${ctx}/css/select2.css" />
<link rel="stylesheet" href="${ctx}/css/unicorn.main.css" />
<link rel="stylesheet" href="${ctx}/css/common.css" />
<%
response.setCharacterEncoding("utf-8");//这个是设置编码方式
response.setContentType("text/html");//这个是设置网页类型,为文本代码
UserInformation user=null;
String username="";
Integer userId=null;
if(request.getSession().getAttribute("userinfo")!=null){
user=(UserInformation)request.getSession().getAttribute("userinfo");
username=user.getUsername();
userId=user.getUserId();
}else{
username="请<a href='http://localhost:8080/fk/test/login.jsp'>登录</a>";
}
%>
</head>
<body>
<div class="header-inner">
<div class="container">
<div class="row">
<div class=" page-header clearfix">
<div class="span11"> <h1 class="page-header" style="background:black;"><img alt="fingerknow" src="${ctx}/images/fingerknow.png" width=""><small>中文最大的购物经验分享平台</small></h1> </div>
<div class="span1"> <a href="#">首页</a> |<a href="#">帮助</a></div>
</div>
</div>
</div>
</div>
<div class="container" id="businessEname_div">
<div class="row">
<div class="span1"></div>
<div class="span10">
<div class="widget-box">
<div class="widget-title">
<span class="icon">
<i class="icon-align-justify"></i>
</span>
<h5>注册商圈</h5>
</div>
<div class="widget-content nopadding">
<form class="form-horizontal" method="post" action="${ctx}/upload/upload.do" id="uploadImgForm" enctype="multipart/form-data">
<div class="control-group" style="border: 0px solid red;">
<label class="control-label">*商圈名:</label>
<div class="controls" style="width: 350px;border: 0px solid red;vertical-align: middle;" >
<input type="text" name="businessName" maxlength="20" id="businessName" width="200px;"/>
<input type="text" name="userId" maxlength="20" value="<%=userId%>" id="userId" width="200px;"/>
<div id="businessName_error" ></div>
</div>
</div>
<div class="control-group">
<label class="control-label">*商圈logo:</label>
<div class="controls" style="width:350px;">
<input type="file" name="file" id="file">
<div id="file_error"></div>
</div>
</div>
<div class="control-group">
<label class="control-label">*商圈英文名:</label>
<div class="controls" style="width: 350px;">
<input type="text" name="businessEname" id="businessEname" />
<div id="businessEname_error"></div>
</div>
</div>
<div class="form-actions">
<button type="button" id="imgSave" value="Validate" class="btn btn-primary">提交注册</button>
</div>
</form>
</div>
</div>
</div>
<div class="span1"></div>
</div>
</div>
<div class="container" style="background:white;">
<div class="row">
<div class="span12" style="margin-left: 25%;">
<p>© 2012 fingerknow.com <span>|</span><a href="#" rel="nofollow" >隐私条款</a><span>|</span><a href="#" rel="nofollow">服务条款</a><span>|</span><a href="#" rel="nofollow" >粤ICP备12003619号-1</a></p>
</div>
</div>
</div>
<script src="${ctx}/js/jquery-1.9.0.js"></script>
<script src="${ctx}/js/jquery.form.js"></script>
<script type="text/javascript">
/**
*
* V1.0
*/
$(document).ready(function() {
//验证商圈名
$("#businessName").blur(function(){
var businessName=$("#businessName").val();
if(businessName!=""){
$("#businessName_error").html("<img src='${ctx}/images/success_img.gif' style='width:15px;height:15px;'/>");
}else{
$("#businessName_error").attr("class","error_div").html("<img src='${ctx}/images/error_img2.gif' style='width:15px;height:15px;'/>"+"商圈名不能为空!");
}
});
//验证商圈英文名
$("#businessEname").blur(function(){
var businessEname=$("#businessEname").val();
if(businessEname!=""){
$("#businessEname_error").html("<img src='${ctx}/images/success_img.gif' style='width:15px;height:15px;'/>");
}else{
$("#businessEname_error").attr("class","error_div").html("<img src='${ctx}/images/error_img2.gif' style='width:15px;height:15px;'/>"+"商圈英文名不能为空!");
}
});
//图片上传 及数据保存
$("#imgSave").click(function(){
var ext = '.jpg.jpeg.gif.bmp.png.';
var f=$("#file").val();
if (f== "") {//先判断是否已选择了文件
$("#file_error").attr("class","error_div").html("<img src='${ctx}/images/error_img2.gif' style='width:15px;height:15px;'/>"+"请添加商圈logo!");
return false;
}
f = f.substr(f.lastIndexOf('.') + 1).toLowerCase();
if (ext.indexOf('.' + f + '.') == -1) {
$("#file_error").attr("class","error_div").html("<img src='${ctx}/images/error_img2.gif' style='width:15px;height:15px;'/>"+"图片格式不正确!");
return false;
}
var options = {
url: "${ctx}/upload/upload.do",
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function(data) {
// 'data' is an object representing the the evaluated json data
// 如果图片上传成功则保存表单注册数据
if(data.status=="0"){
var fileName=data.fileName;
//alert(fileName);
var businessName=$("#businessName").val();
var userId=$("#userId").val();
var businessEname=$("#businessEname").val();
businessName=encodeURI(businessName);
businessName=encodeURI(businessName);
var urls="${ctx}/business/addBusiness.do?businessName="+businessName+"&businessPic="+fileName+"&businessEname="+businessEname+"&userId="+userId;
$.ajax({
type: "post",
url:urls ,
dataType: "json", /*这句可用可不用,没有影响*/
contentType: "application/json; charset=utf-8",
success: function (data) {
if(data.status=="0"){
alert("注册成功!");
}else{
alert("注册失败!原因是:"+data.message);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}else{
$("#file_error").attr("class","error_div").html("<img src='${ctx}/images/error_img2.gif' style='width:15px;height:15px;'/>"+data.message);
}
}
};
// 提交表单
$('#uploadImgForm').ajaxSubmit(options);
});
});
</script>
</body>
</html>
二:service(FileUploadController.java ----springMVC 之controller层)
复制代码 代码如下:
@Controller
@RequestMapping(value = "/upload")
public class FileUploadController {
private Logger logger;
@RequestMapping(value = "upload.do", method = RequestMethod.POST)
public void fileUpload(HttpServletRequest request, HttpServletResponse response) {
Map<String, Object> resultMap = new HashMap<String, Object>();
String newRealFileName = null;
try {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file");
// 获得文件名:
String realFileName = file.getOriginalFilename();
if(file.getSize()/1024>=5*1024){
resultMap.put("status", 1);
resultMap.put("message", "图片不能大于5M");
}else{
System.out.println("获得文件名:" + realFileName);
newRealFileName = FileUploadController.getNowTime() + realFileName.substring(realFileName.indexOf("."));
// 获取路径
String ctxPath = request.getSession().getServletContext().getRealPath("//") + "//temp//";
// 创建文件
File dirPath = new File(ctxPath);
if (!dirPath.exists()) {
dirPath.mkdir();
}
File uploadFile = new File(ctxPath + newRealFileName);
FileCopyUtils.copy(file.getBytes(), uploadFile);
request.setAttribute("files", loadFiles(request));
resultMap.put("status", 0);
resultMap.put("fileName", newRealFileName);
}
} catch (Exception e) {
resultMap.put("status", 1);
resultMap.put("message", "图片上传出错");
logger.info("***** 图片上传出错 *****");
System.out.println(e);
} finally {
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
//必须设置字符编码,否则返回json会乱码
response.setContentType("text/html;charset=UTF-8");
out.write(JSONSerializer.toJSON(resultMap).toString());
out.flush();
out.close();
}
}
}
相关推荐:
ChatGPT-深度学习与自然语言处理的革命性突破,金华ai视觉锁螺丝机
ChatGPT付款被拒?如何应对与解决常见支付问题,掉包ai
SEO行销:开启网站流量增长的秘密武器,百威产品关键词查询排名
AI人物生成:重新定义虚拟形象创作的未来
SEO师:如何在数字时代为企业赢得流量与销量,十堰网站关键词优化教程
seo需要会什么技术呢,seo需要会什么技术呢知乎 ,红色框ai
pbootcms自动翻译插件,让你的网站轻松实现多语言覆盖,ai 镀金
SEO难吗?揭秘SEO背后的真相,让你轻松上手!,大旺百度网站推广
SEO舆情:如何通过有效的舆情管理提升企业品牌形象,seo推广外包提高收录
BingAdapter设置数据后没有显示数据?解决方案在这里!,ai中如何把图变形
seo用什么写属性写,seo属于什么 ,芭田科学施肥AI
AI文章精简-高效提炼与优化你的内容创作,ai quid
AI人工智能:开发与应用的必备软件推荐
SEO数量-如何提升你的网站排名与流量?,射阳seo优化五星服务
AI助手推进:智能化时代的企业革命,ai改变图片颜色
为什么网站要做seo,网站做seo的目的是什么 ,ai初选
ChatGPT故障你从未听过的真相,究竟是什么让它偶尔“失灵”?,ai 图片 矢量
为什么选择SEO会员服务,让您的网站流量飞速增长,靖边专业网站建设公司
【BVIP尊享体验:超越奢华,开启专属未来】,珠宝网站建设思路
SEO兼职:如何通过SEO兼职实现收入增长与职业突破,濮阳网站建设官网
在线AI文章生成器开启智能创作新时代
SEO通过-如何通过SEO优化让你的业务在搜索引擎中脱颖而出,保定网站建设推广专家
GoogleGPT-引领未来智能对话的革命,灵境ai画质
ChatGPT无法访问原因分析及解决方案,斑马ai报道
SEO超级,助力企业站点冲刺搜索引擎排名,白云全网营销推广哪家强
OpenAI银行卡扣款的公司是哪家?揭秘背后的支付流程与安全保障,AI素描相片
文章续写AI:提升写作效率,创意无限
seo链接有什么用,seo外链是什么意思 ,one ai写作
SEO元素-提升网站排名的核心要素,推广分成网站有哪些
亚马逊seo信息是什么,亚马逊seo关键词优化软件 ,光谷ai
SEO资讯:最新趋势,提升网站排名,快速突破流量瓶颈,廊坊seo推广咨询服务
seo站长什么意思,站长工具 - seo综合查询 ,ai少女身材
seo辅助词选什么,seo助手 ,各车企ai
人工AI软件的未来:智能时代的创新驱动力
seo站内链接有什么作用,seo中网站内链的作用 ,781900ai
优化入口:提升网站流量与转化率的秘密武器,琼海关键词排名品牌
整理文章的AI:提升写作效率的智能助手
什么是seo公司口碑,seo品牌 ,ai大模型训练是什么意思
SEO查:如何用精准的SEO诊断助力网站流量爆发,美团关键词排名怎么补
什么是seo长尾词,什么是seo长尾词的概念 ,绿茶ai换脸杨超越
SEO那么重要,企业如何通过它脱颖而出?,自媒体推广营销的概念
ChatGPT:打破语言障碍,理解与沟通的新时代,交通轨道ai
SEO精通:让你的内容在搜索引擎中脱颖而出,跨境电商 推广营销
URL泄露:如何防止信息泄露带来的严重后果,抖音推广营销服务多少钱
软件AI的全称:人工智能驱动未来的关键力量
SEO选择:如何挑选最合适的SEO优化策略?,wb免费刷粉网站推广
AI自动读文:让阅读更轻松、更高效的智能革命,寄ai空
SEO添加:提升网站流量与排名的必备策略,seo团队成员中国人
SEO合同:确保您网站优化成功的关键保障,微博营销推广规则最新
智能AI生成文章释放创作新可能