前端代码:
<script>
$(function () {
$("#btn").click(function () {
var formData=new FormData($("#Form")[0]);
$.ajax({
async : false,
cache : false,
type : "post",
data : formData,
url:"FilmServlet?method=addFilm",
dataType:"text",
contentType: false, //必须
processData: false, //必须
success:function (ip) {
if (ip=="1"){
alert("添加成功!");
} else if (ip=="0"){
alert("添加失败!")
}else {
alert("其他!")
}
}
})
})
})
</script>
<body>
<div class="container">
<!-- <form class="form-inline"> -->
<form class="form-horizontal" id="Form" method="post" enctype="multipart/form-data">
<!-- 水平列表表单 -->
<div class="form-group">
<label for="movename" class="col-sm-2 control-label">电影名:</label>
<div class="col-sm-2">
<input class="form-control" type="text" name="movename" id="movename">
</div>
</div>
<div class="form-group">
<label for="introduction" class="col-sm-2 control-label">简介:</label>
<div class="col-sm-8">
<textarea class="form-control" id="introduction" name="introduction"></textarea>
</div>
</div>
<div class="form-group">
<label for="duration" class="col-sm-2 control-label">电影时长:</label>
<div class="col-sm-2">
<input class="form-control" type="text" name="duration" id="duration">
</div>
</div>
<div class="form-group">
<label for="filmcover" class="col-sm-2 control-label">电影封面:</label>
<div class="col-sm-2">
<input type="file" name="filmcover" id="filmcover">
</div>
</div>
<div class="form-group">
<label for="filmcover" class="col-sm-2 control-label">电影彩蛋:</label>
<div class="col-sm-2">
<input type="file" name="eggs" id="eggs">
</div>
</div>
<div class="form-group">
<label for="price" class="col-sm-2 control-label">电影价格:</label>
<div class="col-sm-2">
<input type="text" name="price" id="price">
</div>
</div>
<div class="form-group">
<label for="shelftime " class="col-sm-2 control-label">上架时间:</label>
<div class="col-sm-2">
<input type="date" name="shelftime " id="shelftime ">
</div>
</div>
<div class="form-group">
<label for="undertime" class="col-sm-2 control-label">下架时间:</label>
<div class="col-sm-2">
<input type="date" name="undertime" id="undertime">
</div>
</div>
<div class="form-group">
<div class="col-sm-2 col-md-offset-2">
<input type="button" value="添加" class="btn" id="btn">
</div>
</div>
</form>
</div>
后端代码:
//获取工厂
FileItemFactory factory = new DiskFileItemFactory();
//获取上传文件工具类
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("utf-8");
String newFileName = null;
String fName = null;//电影名
String introduction = null;//介绍
float filmTime = 0;//电影时长
String filmCover = null;//电影封面
String filmEggs = null;//电影彩蛋
String actorName = null;//明星名
String actorphoto = null;//明星头像
String paraName = null;//参数名称
float price = 0;//票价
//上架时间
String shelftime=null;
//下架时间
String undertime=null;
try {
List<FileItem> list = upload.parseRequest(request);
for (FileItem item : list) {
paraName = item.getFieldName();
if (!item.isFormField()) {
//存储到服务器的web工程本地img子目录中
String realPath = request.getServletContext().getRealPath("");
int pos = item.getName().lastIndexOf(".");
newFileName = UUID.randomUUID().toString().replaceAll("-", "")
+ item.getName().substring(pos);
System.out.println(newFileName);
if ("filmcover".equals(paraName)) {
filmCover = newFileName;
} else if ("eggs".equals(paraName)) {
filmEggs = newFileName;
} else if ("icon".equals(paraName)) {
actorphoto = newFileName;
}
String filePath = realPath + "images/" + newFileName;
System.out.println("存储路径 :" + filePath);
File file = new File(filePath);
item.write(file);
} else {
//非文件项
if ("movename".equals(paraName)) {
fName = new String(item.getString().getBytes("ISO-8859-1"), "UTF-8");
} else if ("introduction".equals(paraName)) {
introduction = new String(item.getString().getBytes("ISO-8859-1"), "UTF-8");
} else if ("duration".equals(paraName)) {
filmTime = Float.parseFloat(item.getString());
} else if ("price".equals(paraName)) {
price = Float.parseFloat(item.getString());
}else if("shelftime".equals(paraName)){
shelftime=item.getString();
}else if ("undertime".equals(paraName)){
undertime=item.getString();
}
}
}
Film film = new Film(1, fName, filmCover, filmEggs, introduction, 1, filmTime, price,shelftime,undertime);
boolean flag = service.addFilm(film);
if (flag) {
jsonStr = "1";
} else {
jsonStr = "0";
}