JAVA檔案上傳下載

Edis.發表於2020-09-24

上傳作業

@RequestMapping("/addhomework")
public String homework(HttpSession session,HttpServletRequest request) throws IOException {
    String sno="";
    if(session.getAttribute("sno")!=null)sno=session.getAttribute("sno").toString();
    String tno="";
    if(session.getAttribute("tno")!=null)tno=session.getAttribute("tno").toString();
    String lrno="";
    if(session.getAttribute("lrno")!=null)lrno=session.getAttribute("lrno").toString();
    String sclass="";
    if(session.getAttribute("sclass")!=null)sclass=session.getAttribute("sclass").toString();
    List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("swork");
    if(files.isEmpty()) {
        session.setAttribute("load","提交作業:請新增檔案!");
        return  "redirect:/student/homework";
    }
    String classfile = homeworkPath + sclass;
    File file = new File(classfile);
    if (!file.exists()) {
        file.mkdir();
    }
    String lrnofile = homeworkPath + sclass + "/" + lrno;
    File file1 = new File(lrnofile);
    if (!file1.exists()) {
        file1.mkdir();
    }
    for (int i = 0; i < files.size(); i++) {
        String fileNamePath = homeworkPath + sclass + "/" + lrno + "/" + getNewFileName(files.get(i).getOriginalFilename());
        File newFile = new File(fileNamePath);
        if (newFile.exists()) {
            session.setAttribute("load", "提交作業:該檔案已存在!請將檔案正確命名!");
            return "redirect:/student/homework";
        }
        files.get(i).transferTo(newFile);
        Homework homework=new Homework();
        homework.setLrno(lrno);
        homework.setSclass(sclass);
        homework.setSwork(getNewFileName(files.get(i).getOriginalFilename()));
        homework.setSno(sno);
        homework.setTno(tno);
        homework.setHaveupdate(0);
        studentService.addHomework(homework);
    }
    session.setAttribute("load","提交作業:提交作業成功!");
    return "redirect:/student/homework";
}

下載作業

@RequestMapping("/homework/download/{lrno}/{swork}/{main}")
public void downloadhomework(@PathVariable("main")String main,@PathVariable("lrno")String lrno,@PathVariable("swork")String swork,HttpSession session,HttpServletResponse response) throws UnsupportedEncodingException {
    String sclass="";
    if(session.getAttribute("sclass")!=null&&main.equals("0"))sclass=session.getAttribute("sclass").toString();
    else if(session.getAttribute("sclass1")!=null&&main.equals("1"))sclass=session.getAttribute("sclass1").toString();
    String path = homeworkPath + sclass + "/" + lrno + "/" + swork;
    File file = new File(path);
    if (file.exists()) {
        response.setContentType("application/octet-stream");//
        response.setHeader("content-type", "application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;fileName=" + new String(swork.getBytes("gb2312"), "ISO8859-1"));// 設定檔名
        byte[] buffer = new byte[1024*10];
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        try {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            OutputStream os = response.getOutputStream();
            int read = 0;
            while((read=bis.read(buffer, 0, 1024*10)) != -1){
                os.write(buffer,0,read);
            }//讀取檔案內容
            System.out.println("success");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

相關文章