案例 将文件上传至tomcat服务器固定位置
对于定制项目,用户可能需要互相之间传看文件,这里介绍我在接触到的定制项目中一种简单快捷的文件上传的方式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(); } } } }
雨淋淋过的季节
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!