62,612
社区成员
发帖
与我相关
我的任务
分享
package socket;
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String args[]) throws Exception{
ServerSocket serverSocket = new ServerSocket(8080);
System.out.println("server is ok.");
while(true){
Socket socket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = in.readLine();
while(line!=null){
System.out.println("Client: " + line);
line = in.readLine();
}
System.out.println("current user close the session.");
in.close();
socket.close();
}
}
}
<head>
<title>test my server</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<p>upload</p>
<form name="UploadForm" method="post" action="http://localhost:8080/1.jsp">
<input type="text" name="myname" /><br>
<select name="myage">
<option value="18">18</option>
<option value="20">20</option>
<option value="22">22</option>
</select><br>
<input type="submit"value="Sutmit">
</form>
</body>
</html>
import java.io.File;
public class msbTree {
public static void main(String[] args){
File file = new File("C:/Documents and Settings/Admini" +
"strator/workspace/FileTree/src/a");
System.out.println(file.getName());
tree(file,1);
}
public static void tree(File file,int level){
String fr = "";
for(int k=0;k<level;k++){
fr += " ";
}
File[] children = file.listFiles();
for(int i=0;i<children.length ;i++){
System.out.println(fr+"/"+children[i].getName());
if(children[i].isDirectory()&&children[i].listFiles()!=null){
tree(children[i],++level);
}
}
}
}
//这里出问题了:如果提交普通的post(不带附件),程序走到这里不走了;就是说没有读到数据,
//但是当我按浏览器ESC之后提交的数据正文就读出来了。
//dataLine = reader.readLine();
//我改为一个字符一个字符的读取也不可以。
char[] buf = {};
if (this.contentLength != 0) {
buf = new char[this.contentLength];
int size = 0;
char c = reader.readChar();
while(c!='\n'){
buf[size++] = c;
c = reader.readChar();
}
System.out.println("The data user posted: " + new String(buf, 0, size));
}
byte[] buf = {};
if (this.contentLength != 0) {
buf = new byte[this.contentLength];
int size = 0;
while(size<this.contentLength){
int c = reader.read();
buf[size++] = (byte)c;
}
System.out.println("The data user posted: " + new String(buf, 0, size));
}