目录
一:SpringMVC增删改查
springmvc工作流程
springmvc的常用注解以及返回结果的处理
springmvc对静态资源的处理
web.xml配置文件
classcontroller.java
主界面
二:文件上传
classcontroller.java
clzupload.jsp
Springmvc_servlet.xml
三:文件下载
classcontroller.java
http://localhost:8080/ssm/hello2
1、通过dispatcherservlet,拿到请求url并且处理获得/hello2
2、通过handlerMapping(处理器映射器),找到被@requestMapping注解所标记的类或者方法
3、通过handlerAdapter(处理器适配器),动态的实例化类,动态调用被标记的方法实现业务
4、调用方法后会产生结果,视图解析器internalResourceViewResolver会对结果进行渲染处理,
然后将其返回给浏览器,用于页面展示
@controller
@requestMapping
@pathviable
在struts中要直接将属性封装到实体类中,是需要实现modeldriven接口
在springmvc中,只需要将实体类当作参数,就可以自动接受页面上的属性值
其次,springmvc的方法是可以直接使用httpservletrequest
只需要在springmvc.xml中配置一个映射即可
如果没有该配置,那么springmvc框架会将静态资源文件访问,当作一个request请求来处理,最终
会找不到对应的资源文件
package com.zhoujuan.ssm.model;
public class Clazz {
protected Integer cid;
protected String cname;
protected String cteacher;
protected String pic;
public Clazz(Integer cid, String cname, String cteacher, String pic) {
this.cid = cid;
this.cname = cname;
this.cteacher = cteacher;
this.pic = pic;
}
public Clazz() {
super();
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getCteacher() {
return cteacher;
}
public void setCteacher(String cteacher) {
this.cteacher = cteacher;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
}
Archetype Created Web Application
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/springmvc-servlet.xml
1
true
SpringMVC
/
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
encodingFilter
/*
package com.zhoujuan.ssm.controller;
import com.zhoujuan.ssm.biz.ClassBiz;
import com.zhoujuan.ssm.model.Clazz;
import com.zhoujuan.ssm.model.dto.ClazzDto;
import com.zhoujuan.ssm.util.PageBean;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
/**
* @author zhuojuan
* @site www.zhoujuan.com
* @company 玉渊工作室
* @create 2022-08-17 20:50
*/
@Controller
@RequestMapping("/clz")
public class ClazzController {
@Autowired
private ClassBiz classBiz;
// list->bookList
// tolist->重定向list->"redirect:/clz/list"
// toEdit->跳转到编辑界面-》clzEdit
// clazz:以前是通过模型驱动接口封装现在是直接在方法中接收参数即可
@RequestMapping("/list")
public String list(Clazz clazz, HttpServletRequest request){
PageBean pageBean= new PageBean();
pageBean.setRequest(request);
List lst = this.classBiz.listPager(clazz, pageBean);
request.setAttribute("lst",lst);
request.setAttribute("paggeBean",pageBean);
return "clzList";
}
@RequestMapping("/toEdit")
public String toEdit(Clazz clazz, HttpServletRequest request){
Integer cid=clazz.getCid();
if(cid != null){
List lst = this.classBiz.listPager(clazz, null);
request.setAttribute("b",lst.get(0));
}
return "clzEdit";
}
@RequestMapping("/add")
public String add(Clazz clazz){
this.classBiz.insertSelective(clazz);
return "redirect:/clz/list";
}
@RequestMapping("/edit")
public String edit(Clazz clazz){
this.classBiz.updateByPrimaryKeySelective(clazz);
return "redirect:/clz/list";
}
@RequestMapping("/del")
public String del(Clazz clazz){
this.classBiz.deleteByPrimaryKey(clazz.getCid());
return "redirect:/clz/list";
}
ID
班级名称
指导老师
班级相册
操作
${b.cid }
${b.cname }
${b.cteacher }
修改
删除
上传图片
下载图片
struts文件上传
1、二进制存在数据库
2、存储到具体的硬盘目录
3、存储到服务器中
真实路径与虚拟路径
通过虚拟路径获取真实路径
request.getservletContext.getrealPath(虚拟路径);
fileutil.copyInputStreamtofile(file.getInput);
fileutil.copyofile(file,new file(真是路径))
添加文件上传相关依赖
commons-fileupload
commons-fileupload
1.3.3
配置文件上传解析器(CommonsMultipartResolver)
6.3 表单提交方式为method="post" enctype="multipart/form-data"
6.4 文件项用spring提供的MultipartFile进行接收
6.5 IO流读写文件
6.6 保存文件上传记录注:springmvc文件上传关键代码
File targetFile = ....;
MultipartFile mf = ....;
String fileName = mf.getOriginalFilename();
mf.transferTo(targetFile);
// 文件上传
@RequestMapping("/upload")
public String upload(ClazzDto clazzDto){
try{
// 前台上传文件
MultipartFile picFile = clazzDto.getPicFile();
// 实际应该配置到resource.properties中
String diskPath = "D:/图片";//图片的存放地址
// http://localhost:8080/upload/mvc/0516.png
String requestPath = "/upload/mvc/";//数据库的保存地址,也是访问地址
// 拿到上传文件的名字
String fileName = picFile.getOriginalFilename();
FileUtils.copyInputStreamToFile(picFile.getInputStream(),new File(diskPath+fileName));
// 将图片上传之后,并且将图片地址更新到数据库中
Clazz clazz = new Clazz();
clazz.setCid(clazzDto.getCid());
clazz.setPic(requestPath+fileName);
this.classBiz.updateByPrimaryKeySelective(clazz);
}catch (Exception e){
e.printStackTrace();
}
return "redirect:/clz/list";
}
<%--
Created by IntelliJ IDEA.
User: zjjt
Date: 2022-8-17
Time: 21:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
@RequestMapping("/download")
public ResponseEntity download(Clazz clazzDto){
try{
// 1.点击下载传递文件的id,通过文件的id查询出文件的路径
Clazz clazz = this.classBiz.selectByPrimaryKey(clazzDto.getCid());
String pic=clazz.getPic();//拿到数据库pic
// 实际应该配置到resource.properties中
String diskPath = "D:/图片";//图片的存放地址
// http://localhost:8080/upload/mvc/0516.png
String requestPath = "/upload/mvc/";//数据库的保存地址,也是访问地址
// 2.通过文件的请求地址,转换文件的存放的硬盘地址
String realPath = pic.replace(requestPath,diskPath);
String fileName =realPath.substring(realPath.lastIndexOf("/")+1);
// 3.将硬盘中文件的下载下来-》固定代码
File file=new File(realPath);
HttpHeaders headers = new HttpHeaders();//http头信息
String downloadFileName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");//设置编码
headers.setContentDispositionFormData("attachment", downloadFileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//MediaType:互联网媒介类型 contentType:具体请求中的媒体类型信息
return new ResponseEntity(FileUtils.readFileToByteArray(file),headers, HttpStatus.OK);
}catch (Exception e){
e.printStackTrace();
}
return null;
}