欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ssm中controller 层相关模板

程序员文章站 2024-03-24 08:28:28
...
package com.hfxt.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.hfxt.common.Pager;
import com.hfxt.entity.FilmInfo;
import com.hfxt.entity.FilmType;
import org.apache.commons.io.FilenameUtils;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping("/filmInfo")
public class FilmInfoController extends BaseController{

    /*@RequestMapping(value="index.html",method= RequestMethod.GET)*/
    @RequestMapping("index.html")
    public String index(Pager<FilmInfo> pager, FilmInfo filmInfo, Model model){
        //获取电影类型
        List<FilmType> filmTypes = filmTypeService.getFilmTypes();
        //获取总记录数
        int total = filmInfoService.getCount(filmInfo);
        pager.setTotal(total);
        List<FilmInfo> filmInfos = filmInfoService.getFilmInfos(filmInfo,(pager.getCurrentPage()-1)*pager.getPageSize(),pager.getPageSize());
        pager.setPageRecords(filmInfos);
        model.addAttribute("filmTypes",filmTypes);
        model.addAttribute("filmInfo",filmInfo);
        model.addAttribute("pager",pager);
        return "index";
    };
    @RequestMapping("toAdd.html")
    public String toAdd(Model model){
        List<FilmType> filmTypes = filmTypeService.getFilmTypes();
        model.addAttribute("typeList",filmTypes);
        return "addUpload";
    }

    @RequestMapping(value = "add.html",produces="text/html;charset=UTF-8")
    @ResponseBody
    public String add(FilmInfo filmInfo,Model model){
        int result =filmInfoService.add(filmInfo);
        Map<String,Object> maps = new HashMap<String,Object>();
        if(result==1){
            maps.put("msg","添加成功");
            maps.put("retcode",1);
        }else{
            maps.put("msg","添加失败");
            maps.put("retcode",0);
        }
        return JSONArray.toJSONString(maps);
    }

    @RequestMapping(value = "del.html")
    @ResponseBody
    public String del(Integer filmId,Model model){
        int result =filmInfoService.del(filmId);
        Map<String,Object> maps = new HashMap<String,Object>();
        if(result==1){
            maps.put("msg","删除成功");
            maps.put("retcode",1);
        }else{
            maps.put("msg","删除失败");
            maps.put("retcode",0);
        }
        return JSONArray.toJSONString(maps);
    }

    @RequestMapping(value = "del/{filmId}")
    @ResponseBody
    public String delInfo(@PathVariable Integer filmId, Model model){
        int result =filmInfoService.del(filmId);
        Map<String,Object> maps = new HashMap<String,Object>();
        if(result==1){
            maps.put("msg","删除成功");
            maps.put("retcode",1);
        }else{
            maps.put("msg","删除失败");
            maps.put("retcode",0);
        }
        return JSONArray.toJSONString(maps);
    }

    @RequestMapping(value = "insert/info",produces="text/html;charset=UTF-8")
    @ResponseBody
    public String addUpload(FilmInfo filmInfo, HttpServletRequest request, @RequestParam(value ="picture",required = false) MultipartFile multipartFile, Model model){
        Map<String,Object> maps = new HashMap<String,Object>();
        if(!multipartFile.isEmpty()){
            //上传路径
            String path="D:\\images";
           /* String path=request.getSession().getServletContext().getRealPath("statics/images");*/
            //原有名字
            String oldName = multipartFile.getOriginalFilename();
            //文件类型
            String suffix = FilenameUtils.getExtension(oldName);
            //上传文件大小 单位 MB
            int fileSize = 300000;
            if(multipartFile.getSize()>fileSize){
                maps.put("msg","上传文件太大");
                maps.put("retcode",0);
            }else if(suffix.equals("jpg")||suffix.equals("png")||suffix.equals("gif")||suffix.equals("bmp")){
                Random random = new Random();
                String newFile = System.currentTimeMillis()+random.nextInt()+".jpg";
                File file = new File(path,newFile);
                filmInfo.setPhoto(newFile);
                int result = filmInfoService.add(filmInfo);
                if(result==1){
                    maps.put("msg","添加成功");
                    maps.put("retcode",1);
                }else{
                    maps.put("msg","添加失败");
                    maps.put("retcode",0);
                }
                try {
                    multipartFile.transferTo(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else{
                maps.put("msg","上传文件类型不正确");
                maps.put("retcode",0);
            }
        }
        return JSONArray.toJSONString(maps);
    }

    @RequestMapping("download/{filmName:.+}")
    @ResponseBody
    public String download(@PathVariable String filmName, HttpServletRequest request, HttpServletResponse response, Model model){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        String path = "D:\\images";
        String downloadPath = path+File.separator+filmName;
        System.out.println(downloadPath);
        File file = new File(downloadPath);
        long fileLength = file.length();
        //设置下载消息框
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Length",String.valueOf(fileLength));
        response.setHeader("Content-disposition","attachment;fileName="+filmName);

        try {
            bis = new BufferedInputStream(new FileInputStream(downloadPath));
            bos = new BufferedOutputStream(response.getOutputStream());

            byte[] bytes = new byte[2048];
            int len;
            while ((len=bis.read(bytes))!=-1){
                bos.write(bytes,0,len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                bos.close();
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    @RequestMapping(value = "toUpdate/{filmId}",produces = "text/html;charset=UTF-8")
    public String toUpdate(@PathVariable Integer filmId,Model model){
        FilmInfo filmInfo = filmInfoService.getFilmInfo(filmId);
        List<FilmType> typeList= filmTypeService.getFilmTypes();
        model.addAttribute("filmInfo",filmInfo);
        model.addAttribute("typeList",typeList);
        return "update";
    }

    @RequestMapping(value = "update",produces="text/html;charset=UTF-8")
    @ResponseBody
    public String update(FilmInfo filmInfo,Model model){
        int result =filmInfoService.update(filmInfo);
        Map<String,Object> maps = new HashMap<String,Object>();
        if(result==1){
            maps.put("msg","修改成功");
            maps.put("retcode",1);
        }else{
            maps.put("msg","修改失败");
            maps.put("retcode",0);
        }
        return JSONArray.toJSONString(maps);
    }
    @RequestMapping(value="show/{filmId}")
    @ResponseBody
    public String show(@PathVariable Integer filmId){
        if(filmId==null){
            return "nodata";
        }else{
           FilmInfo filmInfo = filmInfoService.getFilmInfo(filmId);
           return JSON.toJSONString(filmInfo);
        }
    }

    @RequestMapping(value="show2/{filmId}")
    @ResponseBody
    public FilmInfo show2(@PathVariable Integer filmId){
        if(filmId==null){
            return null;
        }else{
            FilmInfo filmInfo = filmInfoService.getFilmInfo(filmId);
            return filmInfo;
        }
    };

}

相关标签: ssm