主要代码:
jsp:
<form role="form" class="form-horizontal" method="post" action="${pageContext.request.contextPath}/file/addnewnews.do" enctype="multipart/form-data">
<div class="form-group">
<label for="newstitle" class="control-label col-lg-2">标题</label>
<div class="col-lg-8">
<input id="newstitle" name="newstitle" type="text" class="form-control" placeholder="请输入标题">
</div>
</div>
<div class="form-group">
<label for="typeid" class="control-label col-lg-2">频道</label>
<div class="col-lg-8">
<select id="typeid" name="typeid" class="form-control">
<c:forEach items="${types}" var="type" varStatus="i">
<option value="${type.typeid}">${type.typename}</option>
</c:forEach>
</select>
</div>
</div>
<div class="form-group">
<label for="newsintro" class="control-label col-lg-2">导语</label>
<div class="col-lg-8">
<textarea id="newsintro" name="newsintro" class="form-control" style="resize:none;height: 100px;" placeholder="请输入说明"></textarea>
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-2">具体内容</label>
<div class="col-lg-8">
<div id="div1">
<p>请输入新闻正文</p>
</div>
<textarea id="newstext" name="newstext" style="width:100%; height:200px;"></textarea>
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-2" for="file">首图上传</label>
<div class="col-lg-8">
<input type="file" class="form-control" placeholder="请选择图片" name="file" id="file"/>
<p>
<span class="label label-warning">图片大小应小于5M</span>
<span class="label label-info">图片添加为可填项</span>
</p>
</div>
</div>
<div class="form-group">
<div class="col-lg-2 col-lg-offset-4">
<button type="submit" class="btn btn-success" >提交</button>
</div>
<div class="col-lg-2">
<a class="btn btn-warning" onclick="history.back(-1);">返回</a>
</div>
</div>
</form>
controller层:
//新编写新闻
@RequestMapping("/addnewnews.do")
public String addnewnews(NewNews newNews, HttpSession session,MultipartFile file,HttpServletRequest request){
int adminid=(int)session.getAttribute("adminid");
System.out.println(newNews);
if(!file.isEmpty()){
try {
//文件上传路径
String path = request.getSession().getServletContext().getRealPath("/upload");
System.out.println(path);
//设置图片名称
//获取图片扩展名
String picname=file.getOriginalFilename();
String externname=picname.substring(picname.lastIndexOf(".")+1);
int newsid=new Long(System.currentTimeMillis()/1000).intValue();
newNews.setNewsid(newsid);
String newpicname=String.valueOf(adminid)+String.valueOf(newsid)+"."+externname;
//文件传输保存
File f=new File(path,newpicname);
if(!f.exists()){
f.mkdirs();
}
//如果图片存在就销毁再创建
file.transferTo(f);
//添加图片路径
String url="upload/"+newpicname;
newNews.setNewsfirstpic(url);
}catch (Exception e) {
e.printStackTrace();
}
}
重点:
1.如果不想写注释的话,画框两处参数要同名:
2.文件上传相关代码:
if(!file.isEmpty()){
try {
//文件上传路径
String path = request.getSession().getServletContext().getRealPath("/upload");
System.out.println(path);
//设置图片名称
//获取图片扩展名
String picname=file.getOriginalFilename();
String externname=picname.substring(picname.lastIndexOf(".")+1);
int newsid=new Long(System.currentTimeMillis()/1000).intValue();
newNews.setNewsid(newsid);
String newpicname=String.valueOf(adminid)+String.valueOf(newsid)+"."+externname;
//文件传输保存
File f=new File(path,newpicname);
if(!f.exists()){
f.mkdirs();
}
//如果图片存在就销毁再创建
file.transferTo(f);
//添加图片路径
String url="upload/"+newpicname;
newNews.setNewsfirstpic(url);
}catch (Exception e) {
e.printStackTrace();
}