<%@ page language="java" pageEncoding="utf-8"%>
<!DOCTYPE HTML>
<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">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>新闻发布系统管理后台</title>
<link href="css/admin.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
<script type="text/javascript">
$(function(){
$("#upload").click(function(event){
event.preventDefault();
var f=$("#uploadForm")[0];//转换成dom对象
//alert(f.action+">>>>>");
var mydata=new FormData(f);//利用form创建对象。mydata.append可以手工添加对象
//如下的方式不行
/**$.post("upload.do",data,function(d){
alert(d);
});**/
$.ajax({
url:"upload.do",
type:"POST",
data:mydata,
contentType:false,
processData:false,
success:function(d){alert(d);f.reset();}
});
});
//图片预览
$("#nfile").change(function(){
var file=this.files[0];
var preview=$("#preview").get(0);
var reader = new FileReader();
reader.onloadend = function () {
preview.src = reader.result;
}
if (file) {
reader.readAsDataURL(file);
} else {
preview.src = "";
}
});
});
</script>
</head>
<body>
<div id="header">
<div id="welcome">欢迎使用新闻管理系统!</div>
<div id="nav">
<div id="logo"><img src="images/logo.jpg" alt="新闻中国" /></div>
<div id="a_b01"><img src="images/a_b01.gif" alt="" /></div>
</div>
</div>
<div id="main">
<div id="opt_area">
<h1 id="opt_type"> 添加新闻: </h1>
<form action="upload.do" enctype="multipart/form-data" method="post" id="uploadForm">
<p>
<label> 标题 </label>
<input name="ntitle" id="ntitle" type="text" class="opt_input" />
</p>
<p>
<label> 作者 </label>
<input name="nauthor" id="nauthor" name="nauthor" type="text" class="opt_input" />
</p>
<p>
<label> 上传图片 </label>
<img src="" id="preview" height="200" width="300" alt="图片预览"/><br/>
<input type="file" name="nfile" id="nfile"/>
</p>
<input type="submit" value="提交" class="opt_sub" id="upload" />
<input type="reset" value="重置" class="opt_sub" />
</form>
</div>
</div>
</body>
</html>
request.setCharacterEncoding("utf-8");
// response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
DiskFileItemFactory d = new DiskFileItemFactory();
ServletFileUpload up = new ServletFileUpload(d);
up.setHeaderEncoding("utf-8");// 设置字符集
try {
List<FileItem> fields = up.parseRequest(request);// 拿到所有的域
String picPath = super.getServletContext().getRealPath("images");// 存放路径
String title = null, author = null;
for (FileItem f : fields) {
if (f.isFormField()) {
if (f.getFieldName().equals("ntitle"))
title = f.getString("utf-8");
else if (f.getFieldName().equals("nauthor"))
author = f.getString("utf-8");
// ....
} else {
if (f.getName() == "")
continue;
String newFileName = f.getName();
f.write(new java.io.File(picPath, newFileName));
// 存入文件
}
}
} catch (Exception e1) {
// 有错误,放弃
e1.printStackTrace();
response.getWriter().print("上传图片失败!");
// response.sendError(500, "发生错误" + e1.getLocalizedMessage());
return;
}
// request.setAttribute("msg", "增加新闻成功!");
// response.sendRedirect("newsServlet");
response.getWriter().print("上传图片成功!");