boot 在线下载文件


	@GetMapping(value = "/logs", produces = MediaType.TEXT_HTML_VALUE + ";charset=UTF-8")
	public String logs() throws IOException {
		List<OrderWrapper> list = getLogFileNameList();
		if (CollectionUtils.isEmpty(list)) {
			return "no logs";
		}
		if (list.size() > 500) {
			return "log file size is more then 500.";
		}
		StringBuilder sb = new StringBuilder();
		sb.append("<table align='center' border='1'>");
		sb.append("<thead>");
		sb.append("<tr>");
		sb.append("<th>").append("日志文件名").append("</th>");
		sb.append("<th>").append("大小").append("</th>");
		sb.append("<th>").append("下载").append("</th>");
//		sb.append("<th>").append("查看").append("</th>");
//		sb.append("<th>").append("记录开始时间").append("</th>");
		sb.append("</tr>");
		sb.append("</thead>");
		for (OrderWrapper wrapper : list) {
			String name = wrapper.getName();
			File file = new File(infoConfigLoggerPath + CommonConstant.SPRIT + name);
			sb.append("<tr>");
			sb.append("<td>").append(name).append("</td>");
			sb.append("<td>").append(file.length()).append("byte</td>");
			sb.append("<td>").append("<a href='logs/download/").append(name).append("'>Download</a>").append("</td>");
//			sb.append("<td>").append("<a href='logs/view/").append(name).append("/-1'>View</a>").append("</td>");
//			String start = null;
//			try (FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr)) {
//				String line = br.readLine();
//				if (line != null && line.length() > 23) {
//					start = line.substring(0, 23);
//				}
//			}
//			sb.append("<td>").append(start).append("</td>");
			sb.append("</tr>");
		}
		sb.append("</table>");
		return sb.toString();
	}

	private List<OrderWrapper> getLogFileNameList() {
		List<OrderWrapper> list = new ArrayList<>();
		File dir = new File(infoConfigLoggerPath);
		if (dir.exists()) {
			File[] files = dir.listFiles();
			if (files != null && files.length > 0) {
				for (File file : files) {
					if (file.isFile()) {
						list.add(new OrderWrapper(file.getName(), OrderWrapperSort.desc));
					}
				}
			}
		}
		Collections.sort(list);
		return list;
	}

	@GetMapping(value = "/logs/view/{name}/{lines}", produces = MediaType.TEXT_HTML_VALUE + ";charset=UTF-8")
	public String logsView(@PathVariable("name") String name, @PathVariable("lines") Integer lines) throws IOException {
		File file = new File(infoConfigLoggerPath + File.separator + name);
		double max = 1024 * 1024 * 5.1;
		if (file.length() >= max) {
			return "The log file is too large, please download it.";
		}
		List<String> list = new ArrayList<>();
		try (FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr)) {
			String line;
			while ((line = br.readLine()) != null) {
				list.add(line);
			}
			StringBuilder builder = new StringBuilder();
			builder.append("<PRE>");
			if (lines > 0) {
				int start = Math.max(list.size() - lines, 0);
				int end = list.size();
				for (int i = start; i < end; i++) {
					builder.append(list.get(i));
					builder.append("\r\n");
				}
			} else {
				if (file.length() > max) {
					return "The log file is too large, please download it.";
				}
				for (String row : list) {
					builder.append(row);
					builder.append("\r\n");
				}
			}
			builder.append("</PRE>");
			return builder.toString();
		}
	}

	@GetMapping(value = "/logs/download/{name}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
	public void logsDownload(HttpServletResponse response, @PathVariable("name") String name) throws IOException {
		String pathname = infoConfigLoggerPath + CommonConstant.SPRIT + name;
		File file = new File(pathname);
		download(response, file);
	}

	public void download(@NotNull HttpServletResponse response, @NotNull File file) throws IOException {
		String name = URLEncoder.encode(file.getName(), CommonConstant.ENCODING);
		response.setCharacterEncoding(CommonConstant.ENCODING);
		response.setHeader("Content-disposition", "attachment; filename=\"" + name + "\"");
		response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
		Path source = Paths.get(file.getPath());
		Files.copy(source, response.getOutputStream());
	}

	public void downloadFile(HttpServletResponse response, File file) {
		try (InputStream is = new FileInputStream(file);
		     BufferedReader in = new BufferedReader(new InputStreamReader(is));
		     BufferedWriter out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream()))) {
			String name = URLEncoder.encode(file.getName(), CommonConstant.ENCODING);
			response.setCharacterEncoding(CommonConstant.ENCODING);
			response.setHeader("Content-disposition", "attachment; filename=\"" + name + "\"");
			response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
			String line;
			while ((line = in.readLine()) != null) {
				out.append(line);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}
private List<OrderWrapper> getLogFileNameList() {
		List<OrderWrapper> list = new ArrayList<>();
		File dir = new File(infoConfigLoggerPath);
		if (dir.exists()) {
			File[] files = dir.listFiles();
			if (files != null && files.length > 0) {
				for (File file : files) {
					if (file.isFile()) {
						list.add(new OrderWrapper(file.getName(), OrderWrapperSort.desc));
					}
				}
			}
		}
		Collections.sort(list);
		return list;
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨航 AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值