生成可保存路径的文件的方法

// 向页面写文件
	private void writeFile(List list, String tableName) {
		HttpServletResponse response = (HttpServletResponse) ActionContext
				.getContext().get(ServletActionContext.HTTP_RESPONSE);

		StringBuffer sb = new StringBuffer();// 定义要写入文件的内容

		for (int i = 0; i < list.size(); i++) {
			sb.append(list.get(i).toString());
			sb.append("\r\n");
		}
		// 文件输出方式
		response.setContentType("application/sql");

		response.setHeader("Content-disposition", "attachment;filename="
				+ tableName + ".sql");

		BufferedOutputStream bos = null;

		try {

			bos = new BufferedOutputStream(response.getOutputStream());
			bos.write(sb.toString().getBytes());
			bos.flush();

		} catch (IOException e) {

			e.printStackTrace();
		} finally {

			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}
	}
 

你可能感兴趣的:(sql)