Tomcat server to upload files to a fixed position

For custom projects, users may need to transfer files to see each other, I introduce custom project come into contact with in a quick and easy way to upload a file Ctrl + C & Ctrl + V here;

    // uploads annex 
    public String uploadProjectAttachs () throws Exception {
        
        if (attachment.length() > MAX_SIZE) {
            return null;
        }
        
        String pathStr = DocPathProperties.getValue("NewProjectPath");
        String timeStr = DateUtil.getNow(DateUtil.FORMAT_FULL_STR);
        
        Path File = new new File (pathStr);
         // if the current directory path does not exist to create a directory. 
        IF (! path.exists ()) {
            path.mkdirs();
        }
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(attachment));
        String suffix = attachmentFileName.substring(attachmentFileName.indexOf("."));
        File file = new File(path + "/" + timeStr + suffix);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
        int length = 0;
        byte[] buffered = new byte[2048];
        while (-1 !=  (length = (bis.read(buffered, 0, buffered.length)))) {
            bos.write(buffered, 0, length);
        }
        bos.flush();
        bos.close();
        
        newProjectAttach.setFileName(attachmentFileName);
        newProjectAttach.setName(timeStr + suffix);
        newProjectAttach.setContentType(attachmentContentType);
        newProjectAttach.setSysUser(currentUser());
        newProjectAttach.setUploadTime(new Date());
        newProjectAttach.setProject(newProjectService.findNewProjectById(proId));
        newProjectAttachService.addNewProjectAttach(newProjectAttach);
     return "";
} // downloads attachments public InputStream getProjectAttachs () throws Exception { newProjectAttach = newProjectAttachService.findNewProjectAttachById(newProjectAttach.getId()); String pathStr = DocPathProperties.getValue("NewProjectPath"); InputStream bis = new FileInputStream(pathStr + newProjectAttach.getName()); attachmentContentType = newProjectAttach.getContentType(); attachmentFileName = FileUtil.fileNameEncode(request, newProjectAttach.getFileName()); return bis; } // delete items in the Appendix public String delProjectAttachs () throws Exception { String pathStr = DocPathProperties.getValue("NewProjectPath"); NewProjectAttach npa = newProjectAttachService.findNewProjectAttachById(newProjectAttach.getId()); File file = new File(pathStr + npa.getName()); if (file.exists()) { file.delete(); } newProjectAttachService.delNewProjectAttachById(newProjectAttach.getId()); return ""; }

Export, download the file in the browser

/**
     * Export to excel browser
     */
    public static void getCommonExcel(String[] titles, List<String[]> datas, HttpServletResponse response, String sheetName){
        ByteArrayOutputStream bout = null;
        XSSFWorkbook xssfWorkbook;
        try{
            Row row = null;
            xssfWorkbook = new XSSFWorkbook();
            Sheet sheet = xssfWorkbook.createSheet(sheetName);
            Row row0 = sheet.createRow(0);
            for (int i = 0; i < titles.length; i++) {
                Cell cell = row0.createCell(i);
                cell.setCellType(Cell.CELL_TYPE_STRING);
                cell.setCellValue(titles[i]);
            }
            for (int i = 0; i < datas.size(); i++) {
                row = sheet.createRow(i+1);
                String[] rows = datas.get(i);
                for (int j = 0; j < rows.length; j++) {
                    Cell cell = row.createCell(j);
                     cell.setCellType(Cell.CELL_TYPE_STRING);
                     cell.setCellValue(rows[j]);
                }
            }
            String title = new String(sheetName.getBytes("utf-8"), "ISO-8859-1");
            response.setHeader("content-disposition", "attachment;filename=" + title + ".xlsx");
            response.flushBuffer();
            xssfWorkbook.write(response.getOutputStream());
        }catch(Exception e){
            e.printStackTrace ();
        }finally{
            if(bout!=null){
                try {
                    bout.close();
                } catch (IOException e) {
                    e.printStackTrace ();
                }
            }
        }
    }

Guess you like

Origin www.cnblogs.com/huakaiyoushi/p/12156222.html