文件上传所需要的两个jar包,下载链接:https://pan.baidu.com/s/1slG8Bud
下载完放到lib文件夹下,build path即可。
html页面:
<form id="form1" enctype="multipart/form-data" method="post" action="mgr/person/promote">
<div class="register-container" align="center">
<h2>申请升入下一级</h2><br><br>
<h4><table>
<tr style="height:50px">
<td>当前等级:</td>
<td><span class="u_level"></span></td>
</tr>
<tr style="height:50px">
<td>等级特权:</td>
<td><span class="u_level_privilege"></span></td>
</tr>
<tr style="height:50px">
<td>上传图片:</td>
<td><input type="file" class="u_pic" name="pic" accept="image/*"/></td>
</tr>
<tr style="height:50px">
<td>视频链接:</td>
<td><input type="text" class="u_video" name="video" placeholder="直接输入视频所在的URL"></td>
</tr>
<tr style="height:50px">
<td>备注:</td>
<td><textarea rows="5" cols="23" class="u_note" name="note"></textarea></td>
</tr>
</table> </h4>
</div><br><br>
<center><input type="submit" onclick="return promote()" style="width:100px;height:40px;font-size:18px" value="申请升级"></center>
</form>
js代码:
function promote()
{
var pic=$("input.u_pic").val();
var patn = /\.jpg$|\.jpeg$|\.png$|\.gif$/i;
if (!patn.test(pic))
{
alert("请选择图片!");
return false;
}
var videopath=$("input.u_video").val();
if(videopath=="")
{
alert("必须输入视频链接地址!");
return false;
}
}
controller中代码:
@ResponseBody
@RequestMapping(value = "promote")
@SecureValid(code = "11", desc = "申请升级", type = MethodType.FIND)
public ModelAndView promote(@RequestParam("pic") MultipartFile pic,@RequestParam("video") String video,@RequestParam("note") String note,HttpSession httpsession,HttpServletRequest request,HttpServletResponse response)
{
String path=request.getSession().getServletContext().getRealPath("upload");
String originalFilename=pic.getOriginalFilename();
String fileName=new Date().getTime()+originalFilename.substring(originalFilename.lastIndexOf("."));
System.out.println(path);
System.out.println(fileName);
File targetFile=new File(path,fileName);
if(!targetFile.exists())
{
targetFile.mkdirs();
}
try
{
pic.transferTo(targetFile);
}
catch(Exception e)
{
e.printStackTrace();
}
userPersonService.promote(fileName,video,note,acctName(httpsession));
ModelAndView mv = new ModelAndView("redirect:/a_user.html");
return mv;
}
以上代码是部分,不能直接运行,作为思路的参考,如有疑问,可以留言。