基于javaweb+mysql的springboot+mybatis在线商城水果蔬菜商城果蔬商城(前台、后台)(java+springboot+ssm+freemarker+redis+mysql)
运行环境
Java≥8、MySQL≥5.7
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
该项目分为前台用户和后台管理员两个角色,
用户角色的功能:登录(JWT的token验证)、注册、浏览商品、修改个人信息(上传图片)、修改密码、评论商品、添加商品到购物车(Redis缓存)、提交订单、查看订单、收藏商品等等功能。
管理员角色的功能:管理用户信息、管理用户评论信息、管理商品信息、管理订单信息等等功能。
前台:
后台:
技术框架
SpringBoot SpringMVC MyBatis FreeMarker JWT Redis
基于javaweb+mysql的SpringBoot+MyBatis在线商城水果蔬菜商城果蔬商城(前台、后台)(java+springboot+ssm+freemarker+redis+mysql)
Menu selectByPrimaryKey = menuMapper.selectByPrimaryKey(id);
if(selectByPrimaryKey == null) {
return "error/404";
}
model.addAttribute("secondMenu",selectByPrimaryKey);
return "admin/menu/add_button";
}
/**
* 菜单添加表单处理
* @param menu
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> add(Menu menu){
return menuService.add(menu);
}
/**
* 菜单编辑表单处理
* @param menu
* @return
*/
@RequestMapping(value="/edit",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> edit(Menu menu){
return menuService.edit(menu);
}
/**
* 菜单删除处理
* @param id
* @return
*/
@RequestMapping(value="/delete",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> delete(Integer id){
return menuService.delete(id);
}
/**
* 判断当前菜单是否是二级菜单
* @param id
* @return
*/
@RequestMapping(value="/level",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Integer> level(Integer id){
return menuService.level(id);
}
*/
/**
* 后台管理系统用户控制器
*
*/
@RequestMapping("/admin/user")
@Controller
public class UserController {
@Autowired
private IMenuService menuService;
@Autowired
private MenuMapper menuMapper;
@Autowired
private AdminMapper adminMapper;
@Autowired
private AuthorityMapper authorityMapper;
@Autowired
private IUserService userService;
@Autowired
private ICommentService commentService;
/**
* 用户列表页面
* @param model
* @param id
* @param request
* @param content
* @param pageNum
* @param pageSize
* @return
*/
@RequestMapping(value="/index",method= RequestMethod.GET)
public String index(Model model, Integer id, HttpServletRequest request, String content,
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "5") Integer pageSize //每页5个数据
) {
//获取列表展示有关信息
@Autowired
private IMenuService menuService;
@Autowired
private MenuMapper menuMapper;
@Autowired
private IAnnouncementService announcementService;
@Autowired
private AdminMapper adminMapper;
@Autowired
private AuthorityMapper authorityMapper;
/**
* 公告列表页面
* @param model
* @param id
* @return
*/
@RequestMapping(value="/index",method=RequestMethod.GET)
public String index(Model model,Integer id,String content,HttpServletRequest request,
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "5") Integer pageSize //每页5个数据
) {
//获取列表展示有关信息
if(StringUtil.isEmpty(content)) {
//如果查询信息为空
model.addAttribute("PageInfo", announcementService.getAnnouncementByPage(pageNum, pageSize).getData());
}else {
model.addAttribute("PageInfo", announcementService.getAnnouncementByPageAndContent(pageNum, pageSize, content).getData());
model.addAttribute("content",content);
}
//获取路径上有关信息
Menu selectByPrimaryKey = menuMapper.selectByPrimaryKey(id);
if(selectByPrimaryKey == null) {
return "error/404";
}
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
List<Authority> selectByRoleId = authorityMapper.selectByRoleId(loginedAdmin.getRoleId()); //获取当前用户所有权限
Set<Integer> menuIdSet = selectByRoleId.stream().map(Authority :: getMenuId).collect(Collectors.toSet());//把权限中所有菜单id取出来
List<Menu> allMenusByStateAndPrimaryKeys = menuMapper.selectByStateAndPrimaryKeys(MenuStateEnum.OPEN.getCode(), menuIdSet);
model.addAttribute("allAdmins", adminMapper.selectAll());
model.addAttribute("onThirdMenus", menuService.getThirdMenus(allMenusByStateAndPrimaryKeys).getData());
model.addAttribute("parentMenu", menuMapper.selectByPrimaryKey(selectByPrimaryKey.getParentId()));
@Autowired
private IMenuService menuService;
@Autowired
private MenuMapper menuMapper;
@Autowired
private IAnnouncementService announcementService;
@Autowired
private AdminMapper adminMapper;
@Autowired
private AuthorityMapper authorityMapper;
/**
* 公告列表页面
* @param model
* @param id
* @return
*/
@RequestMapping(value="/index",method=RequestMethod.GET)
public String index(Model model,Integer id,String content,HttpServletRequest request,
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "5") Integer pageSize //每页5个数据
) {
//获取列表展示有关信息
if(StringUtil.isEmpty(content)) {
//如果查询信息为空
model.addAttribute("PageInfo", announcementService.getAnnouncementByPage(pageNum, pageSize).getData());
}else {
model.addAttribute("PageInfo", announcementService.getAnnouncementByPageAndContent(pageNum, pageSize, content).getData());
model.addAttribute("content",content);
}
//获取路径上有关信息
Menu selectByPrimaryKey = menuMapper.selectByPrimaryKey(id);
if(selectByPrimaryKey == null) {
return "error/404";
}
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
List<Authority> selectByRoleId = authorityMapper.selectByRoleId(loginedAdmin.getRoleId()); //获取当前用户所有权限
Set<Integer> menuIdSet = selectByRoleId.stream().map(Authority :: getMenuId).collect(Collectors.toSet());//把权限中所有菜单id取出来
List<Menu> allMenusByStateAndPrimaryKeys = menuMapper.selectByStateAndPrimaryKeys(MenuStateEnum.OPEN.getCode(), menuIdSet);
model.addAttribute("allAdmins", adminMapper.selectAll());
model.addAttribute("onThirdMenus", menuService.getThirdMenus(allMenusByStateAndPrimaryKeys).getData());
model.addAttribute("parentMenu", menuMapper.selectByPrimaryKey(selectByPrimaryKey.getParentId()));
@Autowired
private MenuMapper menuMapper;
@Autowired
private AdminMapper adminMapper;
@Autowired
private IAdminService adminService;
@Autowired
private RoleMapper roleMapper;
@Autowired
private AuthorityMapper authorityMapper;
/**
* 管理员列表页面
* @param model
* @param id
* @return
*/
@RequestMapping(value="/index",method=RequestMethod.GET)
public String index(Model model,Integer id,String name,HttpServletRequest request,
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "5") Integer pageSize //每页5个数据
) {
//获取列表展示有关信息
if(StringUtil.isEmpty(name)) {
//如果查询信息为空
model.addAttribute("PageInfo", adminService.getAdminListByPage(pageNum, pageSize).getData());
}else {
model.addAttribute("PageInfo", adminService.getAdminListByPageAndName(pageNum, pageSize, name).getData());
model.addAttribute("name",name);
}
model.addAttribute("RoleList", roleMapper.selectAll());
//获取路径上有关信息
Menu selectByPrimaryKey = menuMapper.selectByPrimaryKey(id);
if(selectByPrimaryKey == null) {
return "error/404";
}
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
List<Authority> selectByRoleId = authorityMapper.selectByRoleId(loginedAdmin.getRoleId()); //获取当前用户所有权限
Set<Integer> menuIdSet = selectByRoleId.stream().map(Authority :: getMenuId).collect(Collectors.toSet());//把权限中所有菜单id取出来
List<Menu> allMenusByStateAndPrimaryKeys = menuMapper.selectByStateAndPrimaryKeys(MenuStateEnum.OPEN.getCode(), menuIdSet);
model.addAttribute("onThirdMenus", menuService.getThirdMenus(allMenusByStateAndPrimaryKeys).getData());
model.addAttribute("parentMenu", menuMapper.selectByPrimaryKey(selectByPrimaryKey.getParentId()));
model.addAttribute("currentMenu", selectByPrimaryKey);
* @param model
* @param id
* @param request
* @param content
* @param pageNum
* @param pageSize
* @return
*/
@RequestMapping(value="/index",method= RequestMethod.GET)
public String index(Model model, Integer id, HttpServletRequest request, String content,
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "5") Integer pageSize //每页5个数据
) {
//获取列表展示有关信息
if(StringUtil.isEmpty(content)) {
//如果查询信息为空
model.addAttribute("PageInfo", orderService.selectByPage(pageNum, pageSize).getData());
}else {
model.addAttribute("PageInfo", orderService.selectByPageAndContent(Long.valueOf(content), pageNum, pageSize).getData());
model.addAttribute("content",content);
}
//获取路径上有关信息
Menu selectByPrimaryKey = menuMapper.selectByPrimaryKey(id);
if(selectByPrimaryKey == null) {
return "error/404";
}
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
List<Authority> selectByRoleId = authorityMapper.selectByRoleId(loginedAdmin.getRoleId()); //获取当前用户所有权限
Set<Integer> menuIdSet = selectByRoleId.stream().map(Authority :: getMenuId).collect(Collectors.toSet());//把权限中所有菜单id取出来
List<Menu> allMenusByStateAndPrimaryKeys = menuMapper.selectByStateAndPrimaryKeys(MenuStateEnum.OPEN.getCode(), menuIdSet);
model.addAttribute("allAdmins", adminMapper.selectAll());
model.addAttribute("onThirdMenus", menuService.getThirdMenus(allMenusByStateAndPrimaryKeys).getData());
model.addAttribute("parentMenu", menuMapper.selectByPrimaryKey(selectByPrimaryKey.getParentId()));
model.addAttribute("currentMenu", selectByPrimaryKey);
return "admin/order/index";
}
/**
* 订单详情查看页面
* @param model
* @param orderId
* @return
*/
@RequestMapping(value="/view",method=RequestMethod.GET)
public String view(Model model, Long orderId) {
model.addAttribute("OrderItemList", orderService.getOrderItemByOrderId(orderId));
return roleService.delete(id);
}
}
/**
* 后台管理系统菜单控制器
*
*/
@RequestMapping("/admin/menu")
@Controller
public class MenuController {
@Autowired
private IMenuService menuService;
@Autowired
private MenuMapper menuMapper;
@Autowired
private AuthorityMapper authorityMapper;
/**
* 菜单列表页面
* @param model
* @return
return ResponseVo.errorByMsg(CodeMsg.UPLOAD_PHOTO_SUFFIX_ERROR);
}
//photo.getSize()单位是B
if (photo.getSize() / 1024 > uploadPhotoMaxSize) {
CodeMsg codeMsg = CodeMsg.UPLOAD_PHOTO_ERROR;
codeMsg.setMsg("图片大小不能超过" + (uploadPhotoMaxSize / 1024) + "M");
return ResponseVo.errorByMsg(codeMsg);
}
//准备保存文件
File filePath = new File(uploadPhotoPath);
if (!filePath.exists()) {
//若不存在文件夹,则创建一个文件夹
filePath.mkdir();
}
filePath = new File(uploadPhotoPath + "/" + StringUtil.getFormatterDate(new Date(), "yyyyMMdd"));
//判断当天日期的文件夹是否存在,若不存在,则创建
if (!filePath.exists()) {
//若不存在文件夹,则创建一个文件夹
filePath.mkdir();
}
String filename = StringUtil.getFormatterDate(new Date(), "yyyyMMdd") + "/" + System.currentTimeMillis() + suffix;
try {
photo.transferTo(new File(uploadPhotoPath + "/" + filename)); //把文件上传
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
log.info("图片上传成功,保存位置:" + uploadPhotoPath + filename);
return ResponseVo.success(filename);
}
/**
* 附件统一上传类
*
* @param attachment
* @return
*/
@RequestMapping(value = "/upload_attachment", method = RequestMethod.POST)
@ResponseBody
public ResponseVo<Attachment> uploadAttachment(@RequestParam(name = "attachment", required = true) MultipartFile attachment, HttpServletRequest request) {
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
String originalFilename = attachment.getOriginalFilename(); //附件名字
//获取文件后缀
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."), originalFilename.length());
//attachment.getSize()单位是B
if (attachment.getSize() / 1024 > uploadAttachmentMaxSize) {
CodeMsg codeMsg = CodeMsg.UPLOAD_ATTACHMENT_ERROR;
codeMsg.setMsg("附件大小不能超过" + (uploadAttachmentMaxSize / 1024) + "M");
return ResponseVo.errorByMsg(codeMsg);
}
//准备保存文件
return ResponseVo.errorByMsg(CodeMsg.USER_PREPASSWORD_ERROR);
}
//判断用户输入的新密码是否符合规范
user.setPassword(newPassword);
CodeMsg validate = ValidateEntityUtil.validate(user);
if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())) {
return ResponseVo.errorByMsg(validate);
}
//判断用户输入的新密码和确认新密码是否一致
if(!newPassword.equals(reNewPassword)){
return ResponseVo.errorByMsg(CodeMsg.USER_RENEWPASSWORD_ERROR);
}
//修改数据库中的用户密码信息
if(userMapper.updateByPrimaryKeySelective(user) <= 0){
return ResponseVo.errorByMsg(CodeMsg.USER_PASSWORD_EDIT_ERROR);
}
return ResponseVo.successByMsg(true, "修改密码成功!");
}
@Override
public ResponseVo<PageInfo> getUserByPage(Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<User> userList = userMapper.selectAll();
PageInfo pageInfo = new PageInfo<>(userList);
pageInfo.setList(userList);
return ResponseVo.success(pageInfo);
}
@Override
public ResponseVo<PageInfo> getUserByPageAndContent(Integer pageNum, Integer pageSize, String content) {
PageHelper.startPage(pageNum, pageSize);
List<User> userList = userMapper.selectBySearchContent(content);
PageInfo pageInfo = new PageInfo<>(userList);
pageInfo.setList(userList);
return ResponseVo.success(pageInfo);
}
@Override
public User selectByPrimaryKey(Long id) {
return userMapper.selectByPrimaryKey(id);
}
@Override
public ResponseVo<Boolean> updateUserPasswd(String passwd, Long userId) {
if(StringUtil.isEmpty(passwd) || userId == null){
return ResponseVo.errorByMsg(CodeMsg.DATA_ERROR);
}
User user = userMapper.selectByPrimaryKey(userId);
user.setPassword(passwd);
//统一表单验证
CodeMsg validate = ValidateEntityUtil.validate(user);
/**
* 管理员更改状态处理
* @param id
* @return
*/
@RequestMapping(value="/change_state",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> chageState(Integer id){
return adminService.chageState(id);
}
}
/**
*/
/**
* 前台用户管理控制器
*/
@RequestMapping("/home/user")
@Controller
public class HomeUserController {
/**
* 公用的上传类
*/
@RequestMapping("/upload")
@Controller
public class UploadController {
private String uploadPhotoSufix = ".jpg,.png,.gif,.jpeg";
private long uploadPhotoMaxSize = 10240; //大小1024KB
private String uploadPhotoPath = System.getProperty("user.dir") + "/src/main/resources/upload/photo/";
private long uploadAttachmentMaxSize = 204800; //大小204800KB
private String uploadAttachmentPath = System.getProperty("user.dir") + "/src/main/resources/upload/attachment/";
private Logger log = LoggerFactory.getLogger(UploadController.class);
@Autowired
private AttachmentMapper attachmentMapper;
/**
* 图片统一上传类
*
* @param photo
* @return
*/
@RequestMapping(value = "/upload_photo", method = RequestMethod.POST)
@ResponseBody
public ResponseVo<String> uploadPhoto(@RequestParam(name = "photo", required = true) MultipartFile photo) {
//判断文件类型是否是图片
String originalFilename = photo.getOriginalFilename();
//获取文件后缀
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."), originalFilename.length());
if (!uploadPhotoSufix.contains(suffix.toLowerCase())) {
return ResponseVo.errorByMsg(CodeMsg.UPLOAD_PHOTO_SUFFIX_ERROR);
}
//photo.getSize()单位是B
if (photo.getSize() / 1024 > uploadPhotoMaxSize) {
CodeMsg codeMsg = CodeMsg.UPLOAD_PHOTO_ERROR;
@RequestMapping(value = "/download_file", method = RequestMethod.GET)
public void downloadFile(HttpServletResponse response, Integer id) {
Attachment selectByPrimaryKey = attachmentMapper.selectByPrimaryKey(id);
try {
File file = new File(uploadAttachmentPath, selectByPrimaryKey.getUrl());
response.setHeader("Content-Disposition", "attachment;filename=" + new String(selectByPrimaryKey.getName().getBytes("UTF-8"), "ISO8859-1"));
writefile(response, file);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 打印到浏览器上下载
*
* @param response
* @param file
*/
public void writefile(HttpServletResponse response, File file) {
ServletOutputStream sos = null;
FileInputStream aa = null;
try {
aa = new FileInputStream(file);
sos = response.getOutputStream();
// 读取文件问字节码
byte[] data = new byte[(int) file.length()];
IOUtils.readFully(aa, data);
// 将文件流输出到浏览器
IOUtils.write(data, sos);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
sos.close();
aa.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
public ResponseVo<Boolean> userDelete(Long orderId, Integer isDeleted) {
if(orderId == null || isDeleted == null){
return ResponseVo.errorByMsg(CodeMsg.DATA_ERROR);
}
Order order = orderMapper.selectByPrimaryKey(orderId);
if(order == null){
return ResponseVo.errorByMsg(CodeMsg.ORDER_NOT_EXIST);
}
if(orderMapper.updateIsDeletedByOrderId(orderId, isDeleted) <= 0){
return ResponseVo.errorByMsg(CodeMsg.ORDER_DELETE_ERROR);
}
return ResponseVo.successByMsg(true, "成功删除该订单!");
}
@Override
public ResponseVo<PageInfo> selectByPage(Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<Order> orderList = orderMapper.selectAll();
PageInfo pageInfo = new PageInfo<>(orderList);
pageInfo.setList(orderList);
return ResponseVo.success(pageInfo);
}
@Override
public ResponseVo<PageInfo> selectByPageAndContent(Long orderNo, Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<Order> orderList = orderMapper.selectBySearchContent(orderNo);
PageInfo pageInfo = new PageInfo<>(orderList);
pageInfo.setList(orderList);
return ResponseVo.success(pageInfo);
}
@Override
public Order selectByPrimaryKey(Long id) {
return orderMapper.selectByPrimaryKey(id);
}
@Override
public List<OrderItem> getOrderItemByOrderId(Long orderId) {
return orderMapper.getOrderItemByOrderId(orderId);
}
@Override
public ResponseVo<Boolean> deleteOrder(Long orderId) {
if(orderId == null){
return ResponseVo.errorByMsg(CodeMsg.DATA_ERROR);
}
//从数据库中删除订单信息
if(orderMapper.deleteByPrimaryKey(orderId) <= 0){
return ResponseVo.errorByMsg(CodeMsg.ORDER_DELETE_ERROR);
}
return ResponseVo.successByMsg(true, "订单删除成功!");
}
List<Menu> allMenusByStateAndPrimaryKeys = menuMapper.selectByStateAndPrimaryKeys(MenuStateEnum.OPEN.getCode(), menuIdSet);
model.addAttribute("allAdmins", adminMapper.selectAll());
model.addAttribute("onThirdMenus", menuService.getThirdMenus(allMenusByStateAndPrimaryKeys).getData());
model.addAttribute("parentMenu", menuMapper.selectByPrimaryKey(selectByPrimaryKey.getParentId()));
model.addAttribute("currentMenu", selectByPrimaryKey);
return "admin/product/index";
}
/**
* 商品添加页面
* @param model
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.GET)
public String add(Model model) {
model.addAttribute("productCategoryList", productCategoryMapper.selectAll());
return "admin/product/add";
}
/**
* 商品编辑页面
* @param model
* @param id
* @return
*/
@RequestMapping(value="/edit",method=RequestMethod.GET)
public String edit(Model model,Long id) {
Product selectByPrimaryKey = productMapper.selectByPrimaryKey(id);
if(selectByPrimaryKey == null) {
return "error/404";
}
model.addAttribute("productCategoryList", productCategoryMapper.selectAll());
model.addAttribute("Product", selectByPrimaryKey);
return "admin/product/edit";
}
/**
* 添加商品操作处理
* @param product
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
}
String filename = StringUtil.getFormatterDate(new Date(), "yyyyMMdd") + "/" + System.currentTimeMillis() + suffix;
try {
attachment.transferTo(new File(uploadAttachmentPath + "/" + filename)); //把文件上传
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//把附件信息写入数据库
BigDecimal size = new BigDecimal((double) attachment.getSize() / 1024);
size = size.setScale(2, BigDecimal.ROUND_HALF_EVEN);
Attachment saveAttachment = new Attachment(null, loginedAdmin.getId(), filename, originalFilename, size); //id,senderid,url,name
CodeMsg validate = ValidateEntityUtil.validate(saveAttachment);
if (!validate.getCode().equals(CodeMsg.SUCCESS.getCode())) {
return ResponseVo.errorByMsg(validate);
}
if (attachmentMapper.insertSelective(saveAttachment) <= 0) {
return ResponseVo.errorByMsg(CodeMsg.UPLOAD_ATTACHMENT_ERROR);
}
log.info("附件上传成功,保存位置:" + uploadAttachmentPath + filename);
return ResponseVo.success(saveAttachment);
}
/**
* 文件统一下载类
*
* @param id
* @param response
* @return
*/
@RequestMapping(value = "/download_file", method = RequestMethod.GET)
public void downloadFile(HttpServletResponse response, Integer id) {
Attachment selectByPrimaryKey = attachmentMapper.selectByPrimaryKey(id);
try {
File file = new File(uploadAttachmentPath, selectByPrimaryKey.getUrl());
response.setHeader("Content-Disposition", "attachment;filename=" + new String(selectByPrimaryKey.getName().getBytes("UTF-8"), "ISO8859-1"));
writefile(response, file);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 打印到浏览器上下载
*
* @param response
* @param file
*/
public void writefile(HttpServletResponse response, File file) {
ServletOutputStream sos = null;
*/
@RequestMapping(value="/send_mail",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> sendMail(Mail mail,String receivers,HttpServletRequest request){
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
return mailService.sendMail(mail, receivers, loginedAdmin.getId());
}
/**
* 邮件删除处理
* @param id
* @return
*/
@RequestMapping(value="/delete",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> delete(Integer id,HttpServletRequest request){
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
return mailService.delete(id, loginedAdmin.getId());
}
}
/**
public void downloadFile(HttpServletResponse response, Integer id) {
Attachment selectByPrimaryKey = attachmentMapper.selectByPrimaryKey(id);
try {
File file = new File(uploadAttachmentPath, selectByPrimaryKey.getUrl());
response.setHeader("Content-Disposition", "attachment;filename=" + new String(selectByPrimaryKey.getName().getBytes("UTF-8"), "ISO8859-1"));
writefile(response, file);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 打印到浏览器上下载
*
* @param response
* @param file
*/
public void writefile(HttpServletResponse response, File file) {
ServletOutputStream sos = null;
FileInputStream aa = null;
try {
aa = new FileInputStream(file);
sos = response.getOutputStream();
// 读取文件问字节码
byte[] data = new byte[(int) file.length()];
IOUtils.readFully(aa, data);
// 将文件流输出到浏览器
IOUtils.write(data, sos);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
sos.close();
aa.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}