SpringMVC+Spring+MyBatis 整合与图片上传简单示例(6)

CREATE TABLE `pic` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `url` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8

package com.cenobitor.pojo;

import lombok.Data;

/**
 * @Description:
 * @Date: Created in 11:05 AM 2018/5/1
 * @Author: Cenobitor
 * @Modified By:
 */
@Data
public class Pic {

private Integer id;
    private String name;
    private String url;

public Pic(String name, String url) {
        this.name = name;
        this.url = url;
    }

public Pic() {
    }
}

2、设置文件存储的虚拟路径

SpringMVC+Spring+MyBatis 整合与图片上传简单示例

2、创建上传页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>上传图片</title>
</head>
<body>
    <form action="/uploadpic.action" method="post" enctype="multipart/form-data">
        <input type="file" />
        <br/>
        <input type="submit" value="提交"/>
    </form>
</body>
</html>

3、创建Controller类

package com.cenobitor.controller;

import com.cenobitor.service.UploadFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UploadFileController {

@Autowired
    private UploadFileService uploadFileService;

//图片上传用MultipartFile接收文件,其中参数名要表单中的文件名一致
    @RequestMapping("/uploadpic")
    public String uploadPic(MultipartFile picFile) throws Exception {
        String picUrl = uploadFileService.UploadPic(picFile);
        if (picUrl == null){
            return "redirect:error.html";
        }
        //重定向预览图片
        return "redirect:"+picUrl;
    }
}

4、创建service层

public interface UploadFileService {
    public String UploadPic(MultipartFile picFile);
 }

package com.cenobitor.service.impl;

import com.cenobitor.mapper.PicMapper;
import com.cenobitor.pojo.Pic;
import com.cenobitor.service.UploadFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Service
@Transactional
public class UploadFileServiceImpl implements UploadFileService {

@Autowired
    private PicMapper picMapper;

@Override
    public String UploadPic(MultipartFile picFile) {
        try {
            // 图片新名字
            String name = UUID.randomUUID().toString();
            // 图片原名字
            String oldName = picFile.getOriginalFilename();
            // 后缀名
            String exeName = oldName.substring(oldName.lastIndexOf("."));
            //保存图片路径为绝对路径
            File pic = new File("/Users/Shared/jpg/" + name + exeName);
            // 保存图片到本地磁盘
            picFile.transferTo(pic);

//保存数据到数据库中
            picMapper.add(new Pic(name,"../jpg/"+name+exeName));
            return "/jpg/"+name+exeName;

} catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

5、创建dao层(mapper层)

package com.cenobitor.mapper;

import com.cenobitor.pojo.Pic;

public interface PicMapper {
    //添加图片
    int add(Pic pic);

}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/26187803e665d2ebf6502bb16764c25c.html