解决方案 »
- 请教开发者们,关于WEB后台开发学习方向
- [ssh问题] javax.servlet.ServletException: could not load an entity:
- jsp 嵌入pdf 的问题
- 查询句执行报ORA-01008:并非所有变量都已绑定
- radio校验问题 100分急求
- 从多个表查询数据库:
- FusionCharts 双Y轴如何让0刻度线重合,保持在同一水平线上
- 自定义标签问题,无法编译?请教
- 急急,用sun.net.ftp出问题了!(在线等,问题解决就结贴!)急急!
- 很多人都会的:怎样把客户端的文件上传到服务器上某个固定的文件夹
- struts2 s:param标签问题
- Ext中怎么动态获取(表头)ColumnModel,列名从数据库中取
我是要把post数据包的内容或者post包里面的参数保存到一个日志文件里,关键是获取post提交的参数的值,并且保存到文件,文件操作只占核心1/4
别人都说了。别人不懂JSP。
用户上传的参数?还是head里面的信息?
这个简单一看就OK
<%@page import="java.io.File"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.io.FileWriter"%>
<%@page import="java.io.IOException"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String logtpath = request.getSession().getServletContext().getRealPath("")+"/1.log"; //得到tomcat容器下web路径
String filename = request.getParameter("");
filename = "c:\\xxx.txt";
String time ="20000909";
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
StringBuffer context = new StringBuffer("记录时间:");
context.append(sd.format(new Date()));
context.append("\n");
context.append("filename:");
context.append(filename);
context.append("\n");
context.append("time:");
context.append(time);
context.append("-----------------------------------------------");
File outFile = new File(logtpath);
try{
if(!outFile.exists()){
//如果没有这个文件 先创建
outFile.createNewFile();
PrintWriter ow = new PrintWriter(new FileOutputStream(outFile));
ow.write(context.toString());
ow.close();
}else{
//如果有这个文件,则追加内容至结尾
FileWriter writer = new FileWriter(outFile, true);
writer.write("\n"+context);
writer.close();
}
}catch(IOException e){
e.printStackTrace();
}
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
This is my JSP page. <br>
</body>
</html>
[/code]
String time= request.getParameter("time");
替换掉那几行,虽然这样写了,但建议楼主不要这样写!文件操作写在前台是不理智的
<%@page import="java.io.File"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.io.FileWriter"%>
<%@page import="java.io.IOException"%>
<%
String logtpath = "/root/1.log"; //linux系统下的保存文件路径String filename = request.getParameter("filename");
String time= request.getParameter("time"); context.append(filename);
context.append(":");
context.append(time);
context.append("/n"); File outFile = new File(logtpath);
try{
if(!outFile.exists()){
//如果没有这个文件 先创建
outFile.createNewFile();
PrintWriter ow = new PrintWriter(new FileOutputStream(outFile));
ow.write(context.toString());
ow.close();
}else{
//如果有这个文件,则追加内容至结尾
FileWriter writer = new FileWriter(outFile, true);
writer.write("\n"+context);
writer.close();
}
}catch(IOException e){
e.printStackTrace();
}
%> 虽然我不会jsp,但是我懂得其他语言,所以看的懂,就粗略的改了下
获取提交的filename和time,保存到系统的/root/1.log(我用的linux系统,所以这个路径)
保存的文件的内容实例为:
c:\1.txt:20090901
c:\2.txt:20090902以上是我改了之后的期望效果xiayuqijava麻烦你看我这样是否有错误
context.append("/n");
这个只是换行符,如果需要换行写成\n,不需要就删掉
\n刚才笔误,写成/n了下面是我的最终版本,麻烦大侠再看看最终版本:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.io.File"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.io.FileWriter"%>
<%@page import="java.io.IOException"%>
<%
String logtpath = "/root/1.log";String filename = request.getParameter("filename");
String time= request.getParameter("time");
StringBuffer context = new StringBuffer();
context.append(filename);
context.append(":");
context.append(time); File outFile = new File(logtpath);
try{
if(!outFile.exists()){
//如果没有这个文件 先创建
outFile.createNewFile();
PrintWriter ow = new PrintWriter(new FileOutputStream(outFile));
ow.write(context.toString());
ow.close();
}else{
//如果有这个文件,则追加内容至结尾
FileWriter writer = new FileWriter(outFile, true);
writer.write("\n"+context);
writer.close();
}
}catch(IOException e){
e.printStackTrace();
}
%>
/n在windows下显示是不换行,但是是linux下的换行符,用windows下的写字板打开1.log可以实现换行的效果