对于定制项目,用户可能需要互相之间传看文件,这里介绍我在接触到的定制项目中一种简单快捷的文件上传的方式Ctrl+C & Ctrl+V;
// 上传项目附件 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); File path = new File(pathStr); //如果当前的路径目录不存在就创建一个目录。 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 ""; } // 下载项目附件 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; } // 删除项目附件 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 ""; }
导出,在浏览器上下载文件
/** * 导出excel到浏览器 */ 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(); } } } }