项目实训——第五周第一次日志

本周已经来到了实训的末尾,学生端后端主要任务是对毕业端进行完善,对一些小问题进行优化。
1)毕业论文上传功能:
接口:
http://localhost:8081/student/uploadStudentGraduatePaper
实现:

 /**
     * 上传毕业论文,并保存到./static/paper
     *
     * @param request
     * @param
     * @return
     * @throws ParseException
     **/
    @RequestMapping(value = "/uploadStudentGraduatePaper")
    @ResponseBody
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Result uploadStudentGraduatePaper(HttpServletRequest request, MultipartFile file) throws ParseException, IOException {
        int stu_id = Integer.parseInt(request.getParameter("stu_id"));
        String stu_Id=Integer.toString(stu_id);
        byte[] paper = new byte[0];
        if (file != null) {
            paper=savePaper(file.getInputStream(), stu_Id , 1);
        }
        if (paper.length == 0) {
            return Result.fail("更新失败");
        } else {

            return Result.succ("上传成功");
        }
    }
    private byte[] savePaper(InputStream inputStream, String fileName,int i) {
        byte[] barray = null;
        OutputStream os = null;
        try {
            String path = null;
            if(i==1) {//毕业论文
                 path = "./static/paper/studentGraduatePaper";
            }
            else{//外审论文
                path = "./static/paper/studentEReviewPaper";
            }
            byte[] bs = new byte[1024 * 1024 * 128];
            int len;
            File tempFile = new File(path);
            os = new FileOutputStream(tempFile.getPath() + File.separator + fileName+".pdf");
            while ((len = inputStream.read(bs)) != -1) {
                os.write(bs, 0, len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        File tempFile;
        if(i==1) {
             tempFile = new File("./static/paper/studentGraduatePaper/" + fileName+".pdf");
        }
        else{
            tempFile = new File("./static/paper/studentEReviewPaper/" + fileName+".pdf");
        }
        barray = fileConvertToByteArray(tempFile);
        return barray;

    }

postman测试:
学号为201656788的学生上传了自己的毕业论文
在这里插入图片描述后端接收论文并以学号重名名保存到了指定的毕业论文存储文件夹中。
在这里插入图片描述
2)外审论文上传功能:
接口:
http://localhost:8081/student/uploadStudentEReviewPaper
实现:

/**
     * 上传毕业论文,并保存到./static/paper
     *
     * @param request
     * @param
     * @return
     * @throws ParseException
     **/
    @RequestMapping(value = "/uploadStudentEReviewPaper")
    @ResponseBody
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Result uploadStudentEReviewPaper(HttpServletRequest request, MultipartFile file) throws ParseException, IOException {
        int stu_id = Integer.parseInt(request.getParameter("stu_id"));
        String stu_Id=Integer.toString(stu_id);
        byte[] paper = new byte[0];
        if (file != null) {
            paper=savePaper(file.getInputStream(), stu_Id , 2);
        }
        /*int i = StuService.uploadPaper(paper);

        if (i == 0) {
            return Result.fail("更新失败");
        } else {

            return Result.succ("更新成功");
        }*/
        if (paper.length == 0) {
            return Result.fail("更新失败");
        } else {

            return Result.succ("上传成功");
        }
    }
    private byte[] savePaper(InputStream inputStream, String fileName,int i) {
        byte[] barray = null;
        OutputStream os = null;
        try {
            String path = null;
            if(i==1) {//毕业论文
                 path = "./static/paper/studentGraduatePaper";
            }
            else{//外审论文
                path = "./static/paper/studentEReviewPaper";
            }
            byte[] bs = new byte[1024 * 1024 * 128];
            int len;
            File tempFile = new File(path);
            os = new FileOutputStream(tempFile.getPath() + File.separator + fileName+".pdf");
            while ((len = inputStream.read(bs)) != -1) {
                os.write(bs, 0, len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        File tempFile;
        if(i==1) {
             tempFile = new File("./static/paper/studentGraduatePaper/" + fileName+".pdf");
        }
        else{
            tempFile = new File("./static/paper/studentEReviewPaper/" + fileName+".pdf");
        }
        barray = fileConvertToByteArray(tempFile);
        return barray;

    }

postman测试:
学号为201656788的学生上传自己的外审论文的存储文件夹。
在这里插入图片描述在该文件夹中可以查看到以学号重命名的论文文件,证明上传成功。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值