首先我用到了两张表
我们以书本表和书本文件表为例:
表结构分明
能看到书本表中的bookimage列段是书本文件表的主键
废话不多说直接上代码
一、添加pom依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
二、在Spring-mvc.xml文件上配置传解析器(CommonsMultipartResolver)
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 必须和用户JSP 的pageEncoding属性一致,以便正确解析表单的内容 -->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 文件最大大小(字节) 1024*1024*50=50M-->
<property name="maxUploadSize" value="52428800"></property>
<!--resolveLazily属性启用是为了推迟文件解析,以便捕获文件大小异常-->
<property name="resolveLazily" value="true"/>
</bean>
三、我定义了三个实体类
public class Book implements Serializable{
private Integer id;
private String bookname;
private Float price;
private String booktype;
private String bookimage;
public Book(Integer id, String bookname, Float price, String booktype, String bookimage) {
this.id = id;
this.bookname = bookname;
this.price = price;
this.booktype = booktype;
this.bookimage = bookimage;
}
public Book() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getBooktype() {
return booktype;
}
public void setBooktype(String booktype) {
this.booktype = booktype;
}
public String getBookimage() {
return bookimage;
}
public void setBookimage(String bookimage) {
this.bookimage = bookimage;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", bookname='" + bookname + '\'' +
", price=" + price +
", booktype='" + booktype + '\'' +
", bookimage='" + bookimage + '\'' +
'}';
}
}
public class BookFile implements Serializable{
private String fileId;
private String realName;
private String contentType;
private Date createdate;
public BookFile(String fileId, String realName, String contentType, Date createdate) {
this.fileId = fileId;
this.realName = realName;
this.contentType = contentType;
this.createdate = createdate;
}
public BookFile() {
super();
}
public String getFileId() {
return fileId;
}
public void setFileId(String fileId) {
this.fileId = fileId;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public Date getCreatedate() {
return createdate;
}
public void setCreatedate(Date createdate) {
this.createdate = createdate;
}
}
public class BookFileVo extends BookFile implements Serializable{
private Integer id;//书本id
private MultipartFile bookFile;//书本文件(图片)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public MultipartFile getBookFile() {
return bookFile;
}
public void setBookFile(MultipartFile bookFile) {
this.bookFile = bookFile;
}
}
注意:bookFileVo是继承于bookFile
四、Controller层
@Controller
@RequestMapping("/bookFile")
public class BookFileController {
private final String DEFUALT_PATH="/uploads";
@Autowired
private IBookService bookServicel;
@Autowired
private IBookFileService bookFileService;
@RequestMapping("/upload")
public String upload(HttpServletRequest request, BookFileVo bookFileVo){
//拿到bookFileVo中的MultiparFile 可以把他看作上传的图片
MultipartFile bookFile = bookFileVo.getBookFile();
Book book=new Book();
String fileId= UUID.randomUUID().toString().replace("-", "");
book.setBookimage(fileId);
book.setId(bookFileVo.getId());
bookFileVo.setFileId(fileId);
bookFileVo.setContentType(bookFile.getContentType());
bookFileVo.setRealName(bookFile.getOriginalFilename());
//调用Servicel层的方法 作用是改变书本bookimage 新增bookFile
bookServicel.updateBookImage(book,bookFileVo);
//上传文件
String relativePath=DEFUALT_PATH+ File.separator+bookFile.getOriginalFilename();//拿到文件名称
String realPath=request.getServletContext().getRealPath(relativePath); //转换成绝对路径
try {
bookFile.transferTo(new File(realPath)); //上传文件 一行代码
}catch (Exception e){
e.printStackTrace();
}
return "redirect:/book/queryBookPager"; //重定向跳转到jsp
}
}
updateBookImage方法
/*
* 要执行两条sql语句
* */
@Override
@Transactional
public void updateBookImage(Book book,BookFile bookFile) {
bookMapper.updateBookImage(book);
bookFileMapper.insert(bookFile);
}
文件上传其实很简单
就一行代码 bookFile.transferTo(new File(realPath));
前面只是做铺垫。
文件下载
在Controller层定义方法
@Controller
@RequestMapping("/bookFile")
public class BookFileController {
private final String DEFUALT_PATH="/uploads";
@Autowired
private IBookService bookServicel;
@Autowired
private IBookFileService bookFileService;
@RequestMapping(value="/download")
public ResponseEntity<byte[]> download(HttpServletRequest request,@RequestParam String fileId){
try{
//先根据文件id查询对应图片信息
BookFile bookFile = bookFileService.selectByPrimaryKey(fileId);
//上传文件
String relativePath=DEFUALT_PATH+ File.separator+bookFile.getRealName();//得到相对路径
String realPath=request.getServletContext().getRealPath(relativePath);//转为绝对路径
//下载关键代码
File file=new File(realPath);
HttpHeaders headers = new HttpHeaders();//http头信息
String downloadFileName = new String(bookFile.getRealName().getBytes("UTF-8"),"iso-8859-1");//设置编码
headers.setContentDispositionFormData("attachment", downloadFileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//MediaType:互联网媒介类型 contentType:具体请求中的媒体类型信息
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.OK);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}
Service层:
@Service
public class BookFileServiceImpl implements IBookFileService {
@Autowired
private BookFileMapper bookFileMapper;
@Override
public BookFile selectByPrimaryKey(String fileId) {
return bookFileMapper.selectByPrimaryKey(fileId);
}
}
ok 完毕 应该很好理解把