ElementUI图片上传

本例采用图片数据存储到数据库字段的方式

上传文件实体类

package com.test.model;

import java.io.Serializable;
import java.sql.Blob;
import java.sql.Date;

import javax.sql.rowset.serial.SerialBlob;

public class FileInfo implements Serializable{
	private Integer id = null;
	private String name = null;
	private String contextType = null;
	private Long length = null;
	private Date dt = null;
	private byte[] content = null;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getContextType() {
		return contextType;
	}
	public void setContextType(String contextType) {
		this.contextType = contextType;
	}
	public Long getLength() {
		return length;
	}
	public void setLength(Long length) {
		this.length = length;
	}
	public Date getDt() {
		return dt;
	}
	public void setDt(Date dt) {
		this.dt = dt;
	}
	public byte[] getContent() {
		return content;
	}
	public void setContent(byte[] content) {
		this.content = content;
	}

}

商品分类关联上传文件

package com.test.model;

import java.util.ArrayList;
import java.util.List;

public class TypeInfo {
	private Integer id = null;
	private String label = null;
	private Integer pid = null;
	private Integer seqno = null;
	private List children = new ArrayList();
	private Integer imgid = null;

	public String getLabel() {
		return label;
	}
	public void setLabel(String label) {
		this.label = label;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Integer getPid() {
		return pid;
	}
	public void setPid(Integer pid) {
		this.pid = pid;
	}
	public Integer getSeqno() {
		return seqno;
	}
	public void setSeqno(Integer seqno) {
		this.seqno = seqno;
	}
	public List getChildren() {
		return children;
	}
	public void setChildren(List children) {
		this.children = children;
	}
	public void addChildren(TypeInfo ti) {
		this.children.add(ti);
	}
	public Integer getImgid() {
		return imgid;
	}
	public void setImgid(Integer imgid) {
		this.imgid = imgid;
	}
}

SQL

drop table T_FILE;
create table T_FILE(id int auto_increment primary key,
	name varchar(200),contexttype varchar(100),length bigint,dt date,content mediumblob);

Mybatis Mapper XML

	
		insert into T_FILE(name,contexttype,length,dt,content) values (#{name},#{contextType},
			#{length},#{dt},#{content,jdbcType=BLOB})
	
	

Mapper接口

package com.test.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import com.test.model.AddressInfo;
import com.test.model.FileInfo;
import com.test.model.StudentInfo;
import com.test.model.TypeInfo;

@Mapper
public interface AddressMapper {
	public List getProvince();
	public List getCity(@Param("code") String code);
	public List getCounty(@Param("code") String code);
	public void saveStudent(StudentInfo si);
	public void updateStudent(StudentInfo si);
	public List findStudent(@Param("name") String name,
			@Param("age1") String age1,
			@Param("age2") String age2);
	public StudentInfo findStudentById(@Param("id") Integer id);
	public void deleteStudent(@Param("id") Integer id);
	public TypeInfo getRootType();
	public List getTypeList(@Param("pid") Integer id);
	public void updateType(TypeInfo ti);
	public void saveType(TypeInfo ti);
	
	public void saveFile(FileInfo fi);
	public FileInfo getFileById(@Param("id") Integer id);
}

服务接口

package com.test.service;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.github.pagehelper.PageInfo;
import com.test.model.AddressInfo;
import com.test.model.FileInfo;
import com.test.model.StudentInfo;
import com.test.model.TypeInfo;

public interface IAddressService {
	public List getProvince();
	public List getCity(String code);
	public List getCounty(String code);
	
	public void updateStudent(StudentInfo si);
	public void saveStudent(StudentInfo stud);
	public PageInfo findStudent(Integer page,Integer rows,String name,String age1,String age2);
	public StudentInfo findStudentById(Integer id);
	
	public void deleteStudent(Integer id);
	
	public TypeInfo getRootType();
	public List getTypeList(Integer id);
	public void updateType(TypeInfo ti);
	public void saveType(TypeInfo ti);
	
	public void saveFile(FileInfo fi);
	public FileInfo getFileById(Integer id);	
}

服务实现类

package com.test.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.mapper.AddressMapper;
import com.test.model.AddressInfo;
import com.test.model.FileInfo;
import com.test.model.StudentInfo;
import com.test.model.TypeInfo;
import com.test.service.IAddressService;

@Service
public class AddressServiceImpl implements IAddressService{
	@Autowired
	private AddressMapper mapper;

	@Override
	public List getProvince() {
		// TODO Auto-generated method stub
		return mapper.getProvince();
	}

	@Override
	public List getCity(String code) {
		if(code != null)
		{
			String code2 = code.substring(0,2);
			List citys = mapper.getCity(code2);
			List resultCitys = new ArrayList();
			for(AddressInfo city:citys)
			{
				if(!code.equals(city.getCode()))
					resultCitys.add(city);
			}
		
			return resultCitys;
		}
		return null;
	}

	@Override
	public List getCounty(String code) {
		if(code != null)
		{
			String code2 = code.substring(0,4);
			List countys = mapper.getCounty(code2);
			List resultCountys = new ArrayList();
			for(AddressInfo county:countys)
			{
				if(!code.equals(county.getCode()))
					resultCountys.add(county);
			}
		
			return resultCountys;
		}
		return null;
	}

	@Override
	public void saveStudent(StudentInfo stud) {
		
		mapper.saveStudent(stud);
	}

	@Override
	public PageInfo findStudent(Integer page,Integer rows,String name,
			String age1,String age2) {
		System.out.println("findStudent================page="+page+",rows="+rows+",name="+name);
		if(page == null)
			page = 1;
		if(rows == null)
			rows = 4;
		PageHelper.startPage(page, rows);
		List result = mapper.findStudent(name,age1,age2);
		PageInfo pInfo = new PageInfo(result);
		return pInfo;
	}

	@Override
	public StudentInfo findStudentById(Integer id) {
		// TODO Auto-generated method stub
		return mapper.findStudentById(id);
	}

	@Override
	public void updateStudent(StudentInfo si) {
		mapper.updateStudent(si);
		
	}

	@Override
	public void deleteStudent(Integer id) {
		mapper.deleteStudent(id);
	}

	@Override
	public TypeInfo getRootType() {
		return mapper.getRootType();
	}

	@Override
	public List getTypeList(Integer id) {
		return mapper.getTypeList(id);
	}

	@Override
	public void updateType(TypeInfo ti) {
		mapper.updateType(ti);
	}

	@Override
	public void saveType(TypeInfo ti) {
		mapper.saveType(ti);
	}

	@Override
	public void saveFile(FileInfo fi) {
		mapper.saveFile(fi);		
	}

	@Override
	public FileInfo getFileById(Integer id) {
		return mapper.getFileById(id);
	}

}

Controller

package com.test.ctrl;

import static org.junit.Assert.assertNotNull;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.test.model.AddressInfo;
import com.test.model.FileInfo;
import com.test.model.StudentInfo;
import com.test.model.TypeInfo;
import com.test.service.IAddressService;

@Controller
@CacheConfig(cacheNames="student")
@CrossOrigin(origins = {"http://localhost:8080"})
public class StudCtrl {
	@Autowired
	private IAddressService addsrv;
	
	@RequestMapping("/studlist")
	public String studlist()
	{
		return "studlist";
	}
	
	
	@RequestMapping("/getprovice")
	@ResponseBody
	public List getprovice()
	{
//		List list = new ArrayList();
//		Map m1 = new HashMap();
//		m1.put("code","001");
//		m1.put("name", "北京");
//		list.add(m1);
//		Map m2 = new HashMap();
//		m2.put("code","002");
//		m2.put("name", "上海");
//		list.add(m2);
//		return list;
		
		return addsrv.getProvince();
	}
	
	@RequestMapping("/getcity")
	@ResponseBody
	public List getcity(String code)
	{
		return addsrv.getCity(code);
	}
	
	@RequestMapping("/getcounty")
	@ResponseBody
	public List getcounty(String code)
	{
		return addsrv.getCounty(code);
	}
	
	@RequestMapping("/savestudent")
	@ResponseBody
	@CacheEvict
	public Boolean savestudent(HttpServletRequest req,@RequestBody StudentInfo si) throws Exception
	{
		/**
		InputStream is = req.getInputStream();
		byte[] content = FileCopyUtils.copyToByteArray(is);
		System.out.println(new String(content));
		JSONObject jsonObj = JSONObject.parseObject(new String(content));
		StudentInfo si = (StudentInfo)JSONObject.toJavaObject(jsonObj,StudentInfo.class);
		*/

		System.out.println(si);
		if(si.getId() != null)
			addsrv.updateStudent(si);
		else {
			addsrv.saveStudent(si);
		}
		
		return true;
	}
	
	@RequestMapping("/findstudbyid")
	@ResponseBody
	public StudentInfo findstudbyid(Integer id) throws Exception
	{
		return addsrv.findStudentById(id);
	}
	
	@RequestMapping("/findstud")
	@ResponseBody
	@Cacheable
	public PageInfo findstud(@RequestBody Map map) throws Exception
	{
		System.out.println("map==="+map);
		Integer page = 1;
		Integer rows = 4;
		String name = null;
		String age1 = null;
		String age2 = null;
		if(map != null)
		{
			Object temp = map.get("currentpage");
			if(temp != null && !"".equals(temp))
				page = new Integer(temp.toString());
			temp = map.get("pagesize");
			if(temp != null && !"".equals(temp))
				rows = new Integer(temp.toString());
			name = (String)map.get("name");
			age1 = (String)map.get("age1");
			age2 = (String)map.get("age2");
		}
		
		return addsrv.findStudent(page, rows, name,age1,age2);
	}
	
	@RequestMapping("/deletestud")
	@ResponseBody
	@CacheEvict
	public Boolean deletestud(String ids)
	{
		if(ids != null)
		{
			String[] dim = ids.split(",");
			for(String s:dim)
			{
				addsrv.deleteStudent(new Integer(s));
			}
		}
		return true;
	}
	
	@RequestMapping("/loadtype")
	@ResponseBody
	public List getTypeJson()
	{
		List rtn = getFirstNode();
		
		return rtn;
	}
		
	@RequestMapping("/loadtypechild")
	@ResponseBody
	public List getTypeByPid(Integer pid)
	{
		System.out.println("pid==="+pid);
		List rtn = addsrv.getTypeList(pid);
		
		return rtn;
	}
	
	private List getFirstNode()
	{
		TypeInfo root = addsrv.getRootType();
		List firstList = addsrv.getTypeList(root.getId());
		for(TypeInfo ti:firstList)
			recurseNode(ti);
		return firstList;
	}
	
	private void recurseNode(TypeInfo ti)
	{
		List children = addsrv.getTypeList(ti.getId());
		System.out.println("ti.id"+ti.getId()+",children="+children);
		if(children==null || children.size()==0)
			return;
		ti.setChildren(children);
		for(TypeInfo chd:children)
		{
			recurseNode(chd);
		}
	}
	
	@RequestMapping("/savetype")
	@ResponseBody
	public Boolean savetype(@RequestBody TypeInfo ti)
	{
		try {
			Integer id = ti.getId();
			if(id != null)
				addsrv.updateType(ti);
			else {
				addsrv.saveType(ti);
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	
	@RequestMapping("/upload")
	@ResponseBody
	public Integer upload(HttpServletRequest req)
	{
		try {
			FileInfo fi = uploadFile(req);
			System.out.println(fi);
			addsrv.saveFile(fi);
			return fi.getId();
		} catch (Exception e) {
			return 0;
		}
	}
	
	public FileInfo uploadFile(HttpServletRequest req)
	{
		if(!(req instanceof MultipartHttpServletRequest))
			return null;
		MultipartHttpServletRequest mreq = (MultipartHttpServletRequest)req;
		MultipartFile multipartFile = mreq.getFile("file");
		try
		{
			InputStream is = multipartFile.getInputStream();
			byte[] bytes = FileCopyUtils.copyToByteArray(is);
			FileInfo fi = new FileInfo();
			fi.setName(multipartFile.getName());
			fi.setContextType(multipartFile.getContentType());
			fi.setLength(new Long(multipartFile.getBytes().length));
			fi.setContent(bytes);
			return fi;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
}

前端VUE




后台代码
https://pan.baidu.com/s/1-CIzMewuu0AfGFgvJ2HFNw 提取码sq4n

前台代码
https://pan.baidu.com/s/1XD2dAXR0zj1zp5DtibWE-Q 提取码9g94

你可能感兴趣的:(VUE)