2020-12-05springboot+layui简单的文件上传下载

 上传Controller和页面

2020-12-05springboot+layui简单的文件上传下载_第1张图片

 

package com.kyz.dissertation_system.controller;

import com.kyz.dissertation_system.pojo.Student;
import com.kyz.dissertation_system.pojo.paper.Papers;
import com.kyz.dissertation_system.service.Impl.paper.PaperServiceImpl;
import com.kyz.dissertation_system.util.EnumMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Slf4j
@Api(tags = "论文上传Controller",value = "paperUpload")
@Controller
@RequestMapping("paper")
public class PaperController {

    @Autowired
    HttpSession httpSession;

    @Autowired
    PaperServiceImpl paperService;

    @ApiOperation(value = "学生word.docx文件上传")
    @PostMapping("/upload")
    @ResponseBody
    public String PaperUpload(@RequestParam("file") MultipartFile file , HttpServletRequest request) throws IOException {
        Subject subject = SecurityUtils.getSubject();
        Student student = (Student) subject.getPrincipal();

        Date date = new Date();
        log.info(date.toString());
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMddHHmmss");


        String paperUUID = UUID.randomUUID().toString();

        log.info(file.toString());
        String pathString = null;
        if(file!=null) {
            pathString = "D:/paperCross/upload/" + paperUUID + "_" + dateformat.format(new Date()) + "_" +file.getOriginalFilename();
        }

        File files=new File(pathString);
        log.info(pathString);
        if(!files.getParentFile().exists()){
            files.getParentFile().mkdirs();
        }
        file.transferTo(files);

        Papers paper = new Papers(null,student.getStuAccount(),paperUUID,date,pathString,null);
        paperService.commitPaperInfo(paper);

        return EnumMessage.setMessage(pathString);
    }
}



    
    功能演示二 - 上传组件
    
    
    
    
    





上传我的论文
文件名 大小 状态 操作

下载Controller和页面

2020-12-05springboot+layui简单的文件上传下载_第2张图片

package com.kyz.dissertation_system.controller;

import com.kyz.dissertation_system.pojo.ApiResult;
import com.kyz.dissertation_system.pojo.Student;
import com.kyz.dissertation_system.pojo.Teacher;
import com.kyz.dissertation_system.pojo.paper.Papers;
import com.kyz.dissertation_system.service.Impl.TeacherServiceImpl;
import com.kyz.dissertation_system.service.Impl.paper.PaperServiceImpl;
import com.kyz.dissertation_system.util.ApiResultHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

@Slf4j
@Api(tags = "教师Controller",value = "查看学生论文")
@Controller
@RequestMapping("/tch")
public class TeacherController {

    @Autowired
    TeacherServiceImpl teacherService;

    @Autowired
    PaperServiceImpl paperService;

    @ApiOperation("跳转查看论文页面")
    @GetMapping("/queryMyStudent")
    public String queryMyStudent(){
        return "home/teacher/queryStuPaper";
    }

    @ApiOperation("教师查看自己学生的论文")
    @GetMapping("/queryMyStudentAndPaper")
    @ResponseBody
    public ApiResult queryMyStudentAndPaper(HttpServletRequest request){
        int page = Integer.parseInt(request.getParameter("page"));
        int limit =Integer.parseInt(request.getParameter("limit"));
        Subject subject = SecurityUtils.getSubject();
        Teacher teacher = (Teacher) subject.getPrincipal();
        log.info("session teacher Account" + teacher.toString());
        int pageCount = teacherService.queryMyStudentByMentorIdCount(teacher.getTchAccount());
        List studentList = teacherService.queryMyStudentByMentorId(teacher.getTchAccount(),page,limit);
        for (Student student : studentList) {
            log.info(student.toString());
        }
        ApiResult data = ApiResultHandler.buildApiResult(0, "success", pageCount, studentList);
        return data;
    }

    @ApiOperation("下载学生论文")
    @RequestMapping("/toDownLoadPaper")
    @ResponseBody
    public String toDownLoadPaper(@Param("stuAccount")String stuAccount
    , HttpServletRequest request
    , HttpServletResponse response) throws IOException {
        Papers paper = paperService.queryDownLoadPaper(stuAccount);
        log.info(paper.toString());
        // - id: 8
        // - account: stu
        // - uuid: 36bb573d-01b9-431d-9b4d-2e84cf84c2be
        // - time: 2020-12-04 16:26:14
        // - address: D:/paperCross/upload/36bb573d-01b9-431d-9b4d-2e84cf84c2be_20201205002613_基于GPU的光线追踪原理.docx
        String path = paper.getPaperAddress();
        String realPath ="D:/paperCross/upload/";
        String fullname = path.substring(path.indexOf("_")-36);


        response.reset();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/msword");
        //这种响应头写法是不正确的
        //response.setHeader("Content-Disposition","attchment; filename:"+ URLEncoder.encode(fullname,"UTF-8"));
        response.setHeader( "Content-Disposition", "attachment;filename=" + new String( fullname.getBytes("gb2312"), "ISO8859-1" ) );


        log.info("path:"+realPath+"document name:"+fullname);



        File file = new File(realPath,fullname);
        InputStream inputStream = new FileInputStream(file);
        OutputStream outputStream = response.getOutputStream();

        byte[] buff = new byte[10485760];
        int index = 0;

        while ((index = inputStream.read(buff))!= -1)
        {
            outputStream.write(buff,0,index);
            outputStream.flush();
        }
        outputStream.close();
        inputStream.close();
        return null;
    }
}



    
    学生信息 - 数据
    
    
    
    
    



搜索学生论文:

 

你可能感兴趣的:(2020-12-05springboot+layui简单的文件上传下载)