标题 | JSP点击链接后下载文件功能 |
内容 | /** *//** * 实现文件另存功能 * * @param text * 文件内容 * @param fileName * 文件名称 * @return */ protected String renderFile(String text, String fileName) throws IOException { response.addHeader("Content-Disposition", "attachment; filename=" + fileName); response.setContentType("application/octet-stream"); response.setCharacterEncoding("GB2312"); response.getWriter().write(text); response.flushBuffer(); response.getWriter().close(); return null; } 下载的action: /** *//** * 提供下载的方法 * @return */ public String down() { String dir = getFullPath() + "https://upload.ynpxrz.com/upload/file/"; try { if (!FileUtils.exists(dir)) { new File(dir).mkdirs(); } Random r = new Random(System.currentTimeMillis()); Integer randomInt = r.nextInt(); this.renderFile("test content:" + randomInt,randomInt + ".txt"); } catch (IOException e) { e.printStackTrace(); this.renderText(e.getMessage()); } return null; } |
随便看 |
|
在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。