所需要的jar包:
<!-- 图片上传相关的两个包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
第一步:实体类
首先在实体类中定义两个字段:
第二步:表单
在jsp中提交图片文件的表单中添加如下 :
<input type="file" name="file"/>
第三步:controller
然后在控制器中书写(action中关于文件上传的关键代码):
//获取项目的根路径
String filepath=request.getServletContext().getRealPath("/");
//获取项目名(目前项目名是多余的)
String projectName=request.getContextPath(); //多余的代码
//保存数据库的路径 (相对路径)
String sqlPath = null;
//定义文件保存的本地路径(这里为项目的路径)
String localPath=filepath+"/"+"static/";
//定义文件名
String filename=null;
if(!user.getFile().isEmpty()){
//生成uuid作为文件名称
String uuid = UUID.randomUUID().toString().replaceAll("-","");
//获得文件类型(可以判断如果不是图片,禁止上传)
String contentType=user.getFile().getContentType();
//获得文件后缀名
String suffixName=contentType.substring(contentType.indexOf("/")+1);
//得到 文件名
filename=uuid+"."+suffixName;
//文件保存路径
//E:\workspace\work\practice-articleManagementSystem\src\main\webapp\a\static\9e0888277f3e40f18db172c993450a7a.jpeg
user.getFile().transferTo(new File(localPath+filename));
}
//把文件的相对路径保存在数据库中
user.setImagePath(filename);
这里将文件保存在webapps下的static中,需要在springmvc.xml文件中配置static文件夹下的静态资源映射,如下:
<mvc:resources mapping="/static/**" location="/static/" />
下面这里为更新用户信息的action的一个完整版:
@RequestMapping("/updateuser")
public String editUser(User user,@RequestParam(value="pageNum",defaultValue="1")int pageNum,HttpServletRequest request,Model model) throws IllegalStateException, IOException {
/**
* 实现图片上传到数据库中
*/
{
//获取项目的根路径
String filepath=request.getServletContext().getRealPath("/");
//获取项目名(目前项目名是多余的)
String projectName=request.getContextPath(); //多余的代码
//保存数据库的路径 (相对路径)
String sqlPath = null;
//定义文件保存的本地路径(这里为项目的路径)
String localPath=filepath+"/"+"static/";
//定义文件名
String filename=null;
if(!user.getFile().isEmpty()){
//生成uuid作为文件名称
String uuid = UUID.randomUUID().toString().replaceAll("-","");
//获得文件类型(可以判断如果不是图片,禁止上传)
String contentType=user.getFile().getContentType();
//获得文件后缀名
String suffixName=contentType.substring(contentType.indexOf("/")+1);
//得到 文件名
filename=uuid+"."+suffixName;
//文件保存路径
//E:\workspace\work\practice-articleManagementSystem\src\main\webapp\a\static\9e0888277f3e40f18db172c993450a7a.jpeg
user.getFile().transferTo(new File(localPath+filename));
}
//把文件的相对路径保存在数据库中"/images/"+
user.setImagePath(filename);
}
boolean b=userService.updateUser(user);
System.out.println(b);
if(b) {
model.addAttribute("msg", "信息修改成功");
}else {
model.addAttribute("msg", "信息修改不成功");
}
User result=userService.findUserById(user.getId());
//判断用户是否存在
if(result!=null) {
User result1=selectUser(user.getId(),pageNum);
System.out.println("这里为model中的"+result1.getImagePath());
model.addAttribute("user", result1);
}
return "/user/editUser";
}
第四步:数据库
需要将数据库中保存文件的列的长度设得大一点,我在这里设置了500,长度太小就会报错
第五步:显示
最后是图片的获取显示:
首先在页面的头部获取项目的根:
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
在head标签中添加:
<base href="<%=basePath%>">
然后在body中引用:
<img src="/static/${user.imagePath}">
user.imagePath这里为从控制器中传进来的保存在实体类对象中的中的图片路径