Search in sources :

Example 1 with CustomException

use of com.hfut.exception.CustomException in project Workload by amoxu.

the class DownloadExcel method download.

/**
 * @param request
 * @param response
 * @throws Exception
 */
@RequestMapping(value = "/downdetail/{type}/{years}/{teacher}", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
@ResponseBody
public void download(HttpServletRequest request, HttpServletResponse response, @PathVariable("years") Integer years, @PathVariable("teacher") String teacher, @PathVariable("type") String type) throws Exception {
    response.setContentType("text/html;charset=UTF-8");
    BufferedInputStream in = null;
    BufferedOutputStream out = null;
    request.setCharacterEncoding("UTF-8");
    String separator = File.separator;
    String rootpath = request.getSession().getServletContext().getRealPath(separator + "upload");
    String fileName = new Date().getTime() + ".xls";
    String path = rootpath + separator + fileName;
    switch(type) {
        case ExcelServiceImpl.REMOTE_TEACH:
            excelService.outTeachWorkload(path, teachService.getList(years, teacher, -1, 0));
            break;
        case ExcelServiceImpl.REMOTE_EXP:
            excelService.outExpWorkload(path, expService.getList(years, teacher, -1, 0));
            break;
        case ExcelServiceImpl.REMOTE_DESIGN:
            excelService.outDesignWorkload(path, designService.getList(years, teacher, -1, 0));
            break;
        case ExcelServiceImpl.REMOTE_GRA:
            excelService.outGraduateWorkload(path, graService.getList(years, teacher, -1, 0));
            break;
        case ExcelServiceImpl.LOACAL_COURSE:
            excelService.outLCourseWorkload(path, localCourseService.getList(years, teacher, -1, 0));
            break;
        case ExcelServiceImpl.LOACAL_EXP:
            excelService.outLExpWorkload(path, localExpService.getList(years, teacher, -1, 0));
            break;
        case ExcelServiceImpl.LOACAL_DESIGN:
            excelService.outLDesignWorkload(path, localDesignService.getList(years, teacher, -1, 0));
            break;
        case ExcelServiceImpl.LOACAL_GRA_PRACTICE:
            excelService.outLGraPracticeWorkload(path, localGraPracticeService.getList(years, teacher, -1, 0));
            break;
        case ExcelServiceImpl.LOACAL_GRA_DESIGN:
            excelService.outLGraDesignWorkload(path, localGraDesignService.getList(years, teacher, -1, 0));
            break;
        case ExcelServiceImpl.LOACAL_PRACTICE:
            excelService.outLPracticeDesignWorkload(path, localPracticeService.getList(years, teacher, -1, 0));
            break;
        case ExcelServiceImpl.LOACAL_PROJECT:
            excelService.outLProjectWorkload(path, localProjectService.getList(years, teacher, -1, 0));
            break;
        case ExcelServiceImpl.LOACAL_MATCH:
            excelService.outLMatchWorkload(path, localMatchService.getList(years, teacher, -1, 0));
            break;
        case ExcelServiceImpl.LOACAL_NET:
            excelService.outLNetWorkload(path, localNetService.getList(years, teacher, -1, 0));
            break;
        case ExcelServiceImpl.EXP:
            excelService.outExpriment(path, exprimentService.getList(years, teacher, -1, 0));
            break;
        case ExcelServiceImpl.REMOTE_NON_LESSON:
            excelService.outNonLesson(path, nonLessonService.getList(years, teacher, -1, 0));
            break;
        default:
            return;
    }
    File f = new File(path);
    try {
        response.setContentType("application/x-excel");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        response.setHeader("Content-Length", String.valueOf(f.length()));
        in = new BufferedInputStream(new FileInputStream(f));
        out = new BufferedOutputStream(response.getOutputStream());
        byte[] data = new byte[1024];
        int len = 0;
        while (-1 != (len = in.read(data, 0, data.length))) {
            out.write(data, 0, len);
        }
    } catch (Exception e) {
        throw new CustomException("下载文件异常");
    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
        f.delete();
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) CustomException(com.hfut.exception.CustomException) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File) Date(java.util.Date) FileInputStream(java.io.FileInputStream) CustomException(com.hfut.exception.CustomException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with CustomException

use of com.hfut.exception.CustomException in project Workload by amoxu.

the class UploadExcel method importExcel.

@RequestMapping(value = "/uploadExcel", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
@ResponseBody
public String importExcel(@RequestParam(value = "file", required = false) MultipartFile buildInfo, HttpServletRequest request, HttpServletResponse response) {
    if (buildInfo != null) {
        String path = request.getSession().getServletContext().getRealPath("/upload") + File.separatorChar + buildInfo.getOriginalFilename();
        // 需要先保存在本地
        File file = null;
        AjaxResult ajaxResult = new AjaxResult();
        try {
            file = ToolKit.getFileFromBytes(buildInfo.getBytes(), path);
        } catch (CustomException e) {
            e.printStackTrace();
            ajaxResult.failed();
            ajaxResult.setMsg(e.getMessage());
            return ajaxResult.toString();
        } catch (IOException e) {
            e.printStackTrace();
            ajaxResult.failed();
            ajaxResult.setMsg(e.getMessage());
            return ajaxResult.toString();
        }
        List list = null;
        String type = request.getParameter("type");
        System.out.println(type);
        try {
            switch(type) {
                case ExcelServiceImpl.REMOTE_TEACH:
                    // 这里是解析excel文件
                    list = excelService.inTeachWorkload(file);
                    list = remoteTeachService.insertLoadByList(list);
                    break;
                case ExcelServiceImpl.REMOTE_EXP:
                    // 这里是解析excel文件
                    list = excelService.inExpWorkLoad(file);
                    list = remoteExpService.insertLoadByList(list);
                    break;
                case ExcelServiceImpl.REMOTE_DESIGN:
                    list = excelService.inDesignList(file);
                    list = designService.insertLoadByList(list);
                    break;
                case ExcelServiceImpl.REMOTE_GRA:
                    list = excelService.inGraduateWorkload(file);
                    list = graService.insertLoadByList(list);
                    break;
                case ExcelServiceImpl.LOACAL_COURSE:
                    list = excelService.inLCourseWorkload(file);
                    list = localCourseService.insertLoadByList(list);
                    break;
                case ExcelServiceImpl.LOACAL_EXP:
                    list = excelService.inLExpWorkload(file);
                    list = localExpService.insertLoadByList(list);
                    break;
                case ExcelServiceImpl.LOACAL_DESIGN:
                    list = excelService.inLDesignWorkload(file);
                    list = localDesignService.insertLoadByList(list);
                    break;
                case ExcelServiceImpl.LOACAL_GRA_PRACTICE:
                    list = excelService.inLGraPracticeWorkload(file);
                    list = localGraPracticeService.insertLoadByList(list);
                    break;
                case ExcelServiceImpl.LOACAL_GRA_DESIGN:
                    list = excelService.inLGraDesignWorkload(file);
                    list = localGraDesignService.insertLoadByList(list);
                    break;
                case ExcelServiceImpl.LOACAL_PRACTICE:
                    list = excelService.inLPracticeWorkload(file);
                    list = localPracticeService.insertLoadByList(list);
                    break;
                case ExcelServiceImpl.LOACAL_PROJECT:
                    list = excelService.inLProjectWorkload(file);
                    list = localProjectService.insertLoadByList(list);
                    break;
                case ExcelServiceImpl.LOACAL_MATCH:
                    list = excelService.inLMatchWorkload(file);
                    list = localMatchService.insertLoadByList(list);
                    break;
                case ExcelServiceImpl.LOACAL_NET:
                    list = excelService.inLNetWorkload(file);
                    list = localNetService.insertLoadByList(list);
                    break;
                case ExcelServiceImpl.EXP:
                    list = excelService.inExpriment(file);
                    list = exprimentService.insertLoadByList(list);
                    break;
                case ExcelServiceImpl.REMOTE_NON_LESSON:
                    list = excelService.inNonLesson(file);
                    list = nonLessonService.insertLoadByList(list);
                    break;
                default:
            }
        } catch (Exception e) {
            e.printStackTrace();
            // 这里做的是一个插入数据库功能
            ajaxResult.setMsg(e.getMessage());
            ajaxResult.failed();
            ajaxResult.setData(list);
            return ajaxResult.toString();
        }
        ToolKit.deleteFile(path);
        if (list.size() == 0) {
            ajaxResult.ok();
            ajaxResult.setMsg("导入成功");
            return ajaxResult.toString();
        } else {
            ajaxResult.failed();
            ajaxResult.setMsg("导入失败");
            return ajaxResult.toString();
        }
    }
    AjaxResult ajaxResult = new AjaxResult<>();
    ajaxResult.failed();
    return ajaxResult.toString();
}
Also used : AjaxResult(com.hfut.entity.AjaxResult) List(java.util.List) CustomException(com.hfut.exception.CustomException) IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) IOException(java.io.IOException) CustomException(com.hfut.exception.CustomException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with CustomException

use of com.hfut.exception.CustomException in project Workload by amoxu.

the class UserServiceImpl method updateUser.

@Transient
@Override
public void updateUser(User user) throws CustomException {
    user.setPassword(userMapper.selectByPrimaryKey(user.getId()).getPassword());
    user.setAnswer(userMapper.selectByPrimaryKey(user.getId()).getAnswer());
    Subject subject = SecurityUtils.getSubject();
    int ret;
    if (10000 == user.getId()) {
        if (subject.hasRole("超级管理员")) {
            ret = userMapper.updateByPrimaryKey(user);
        } else {
            throw new CustomException("权限不足");
        }
    } else {
        ret = userMapper.updateByPrimaryKey(user);
    }
    if (ret == 1) {
        return;
    } else {
        throw new CustomException("更新失败,请联系管理员。");
    }
}
Also used : CustomException(com.hfut.exception.CustomException) Subject(org.apache.shiro.subject.Subject) Transient(java.beans.Transient)

Example 4 with CustomException

use of com.hfut.exception.CustomException in project Workload by amoxu.

the class ExcelServiceImpl method inTeachWorkload.

@Override
public List inTeachWorkload(File file) throws CustomException {
    try {
        // 创建需要批量插入数据集合
        // 创建一个FileInputStream 文件输入流
        FileInputStream inputStream = new FileInputStream(file);
        // 创建对Excel工作簿文件的引用
        Workbook wookbook = null;
        String name = file.getName();
        String fileType = name.substring(name.lastIndexOf(".") + 1, name.length());
        if (fileType.equals("xlsx")) {
            wookbook = new XSSFWorkbook(inputStream);
        } else if (fileType.equals("xls")) {
            wookbook = new HSSFWorkbook(inputStream);
        }
        // 在Excel文档中,第一张工作表的缺省索引是0
        Sheet sheet = wookbook.getSheetAt(0);
        // 获取到Excel文件中的所有行数
        int rows = sheet.getPhysicalNumberOfRows();
        // 创建对象
        List<RemoteTeachWorkload> list = new ArrayList<RemoteTeachWorkload>();
        RemoteTeachWorkload object;
        // 遍历行 从第二行开始遍历
        int line;
        for (int i = 1; i < rows; i++) {
            // 读取左上端单元格
            Row row = sheet.getRow(i);
            // 行不为空
            if (row != null) {
                object = new RemoteTeachWorkload();
                line = 0;
                // 获取老师
                object.setCollege(POIUtil.getStringCell(row.getCell(line++)));
                object.setTeacher(POIUtil.getStringCell(row.getCell(line++)));
                object.setLesson(POIUtil.getStringCell(row.getCell(line++)));
                object.setYears(POIUtil.getIntCell(row.getCell(line++)));
                object.setGrades(POIUtil.getStringCell(row.getCell(line++)));
                object.setTeachClass(POIUtil.getIntCell(row.getCell(line++)));
                object.setPeriod(POIUtil.getFloatCell(row.getCell(line++)));
                object.setCredit(POIUtil.getFloatCell(row.getCell(line++)));
                object.setPeople(POIUtil.getIntCell(row.getCell(line++)));
                object.setClassCoefficient(POIUtil.getFloatCell(row.getCell(line++)));
                object.setWorkload(POIUtil.getFloatCell(row.getCell(line++)));
                object.setMoney(POIUtil.getFloatCell(row.getCell(line++)));
                object.setNote(POIUtil.getStringCell(row.getCell(line++)));
                // 将对象增加到集合中
                list.add(object);
            }
        }
        System.out.println(list.toString());
        return list;
    // 返回集合
    } catch (IOException e) {
        throw new CustomException("创建导入excel对象报错!" + e);
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) CustomException(com.hfut.exception.CustomException) FileInputStream(java.io.FileInputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook)

Example 5 with CustomException

use of com.hfut.exception.CustomException in project Workload by amoxu.

the class TotalController method downloadTotal.

@RequestMapping(value = "/total/download/{years}/{location}/{college}/{teacher}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf8")
@ResponseBody
public void downloadTotal(HttpServletRequest request, HttpServletResponse response, @PathVariable("years") Integer years, @PathVariable("location") Integer location, @PathVariable("teacher") String teacher, @PathVariable("college") String college) throws CustomException, IOException {
    String path;
    try {
        path = totalService.download(request, years, teacher, college, location);
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }
    BufferedInputStream in = null;
    BufferedOutputStream out = null;
    File f = new File(path);
    try {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        response.setContentType("application/x-excel");
        response.setHeader("Content-Disposition", "attachment; filename=" + years + URLEncoder.encode("年工作量汇总", "UTF-8") + ".xls");
        response.setHeader("Content-Length", String.valueOf(f.length()));
        in = new BufferedInputStream(new FileInputStream(f));
        out = new BufferedOutputStream(response.getOutputStream());
        byte[] data = new byte[1024];
        int len = 0;
        while (-1 != (len = in.read(data, 0, data.length))) {
            out.write(data, 0, len);
        }
    } catch (Exception e) {
        throw new CustomException("下载文件异常");
    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
        f.delete();
    }
}
Also used : CustomException(com.hfut.exception.CustomException) CustomException(com.hfut.exception.CustomException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

CustomException (com.hfut.exception.CustomException)12 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)6 User (com.hfut.entity.User)5 AjaxResult (com.hfut.entity.AjaxResult)4 Subject (org.apache.shiro.subject.Subject)4 File (java.io.File)3 IOException (java.io.IOException)3 BufferedOutputStream (java.io.BufferedOutputStream)2 FileInputStream (java.io.FileInputStream)2 UserExample (com.hfut.entity.UserExample)1 Transient (java.beans.Transient)1 BufferedInputStream (java.io.BufferedInputStream)1 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 List (java.util.List)1 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)1 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)1