当前位置: 技术问答>java相关
为什么我编的http服务器不能得到图片?
来源: 互联网 发布时间:2015-02-02
本文导语: 源文件如下: import java.net.*; import java.io.*; import java.util.*; class ServerOne extends Thread{ private Socket client; private BufferedReader in; private PrintWriter out; public ServerOne(Socket client) throws IOException{ this.client = client; i...
源文件如下:
import java.net.*;
import java.io.*;
import java.util.*;
class ServerOne extends Thread{
private Socket client;
private BufferedReader in;
private PrintWriter out;
public ServerOne(Socket client) throws IOException{
this.client = client;
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())),true);
start();
}
public void run(){
try{
while(true){
String askLine = in.readLine();
if(askLine.indexOf("GET") != -1){
String fileName = getFileName(askLine);
File file =new File(fileName);
if(file.exists()){
System.out.println("gett" + file);
HttpServer.resultLog.println("gett" + file);
out.println("http/1.1 200 ok");
out.println("mime_version:1.0");
out.println("content_type:text/htm1");
out.println("content_length:" + file.length());
out.println("nn");
sendFile(out,file);
}
}
System.out.println("askLinet" + askLine);
HttpServer.resultLog.println("User ask t" +askLine);
}
}catch(IOException e){
}finally{
try{
client.close();
}catch(IOException e){
}
}
}
String getFileName(String askLine){
askLine = askLine.substring(askLine.indexOf(' ') + 1);
askLine = askLine.substring(0,askLine.indexOf(' '));
System.out.println("asdf" + askLine);
try{
if(askLine.indexOf('/') == (askLine.length() - 1)){
askLine = askLine.substring(1);
askLine = askLine + "index.html";
System.out.println("User ask t" + askLine);
HttpServer.resultLog.println("User ask t" + askLine);
}
else{
askLine = askLine.substring(1);
System.out.println("User view t" + askLine);
HttpServer.resultLog.println("User ask t" + askLine);
}
}catch(Exception e){}
return askLine;
}//end getFileName
void sendFile(PrintWriter out,File file) throws IOException{
BufferedReader readFile = new BufferedReader(new FileReader(file));
String s1,s2 = new String();
while((s1 = readFile.readLine()) != null){
s2= s2 + s1 + "n";
}
out.write(s2);
readFile.close();
out.flush();
out.close();
System.out.println(file + "tOK");
}
}//end ServerOne
public class HttpServer{
static PrintWriter resultLog;//log file
static final int PORT = 80;
public static void main(String[] args) throws IOException{
resultLog = new PrintWriter(new BufferedWriter(new FileWriter("httpLog.log")),true);
ServerSocket httpOne = new ServerSocket(PORT);
System.out.println("Server Started");
resultLog.println("Server Started");
try{
while(true){
Socket client = httpOne.accept();
System.out.println(client);
resultLog.println("n" + client);
try{
new ServerOne(client);
}catch(IOException e){
client.close();
e.printStackTrace();
resultLog.println(e + "nCloseed" + "n");
}
}
}finally{
httpOne.close();
System.out.println("Server Closed");
resultLog.println("Server Closed");
}
}
}///:~
import java.net.*;
import java.io.*;
import java.util.*;
class ServerOne extends Thread{
private Socket client;
private BufferedReader in;
private PrintWriter out;
public ServerOne(Socket client) throws IOException{
this.client = client;
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())),true);
start();
}
public void run(){
try{
while(true){
String askLine = in.readLine();
if(askLine.indexOf("GET") != -1){
String fileName = getFileName(askLine);
File file =new File(fileName);
if(file.exists()){
System.out.println("gett" + file);
HttpServer.resultLog.println("gett" + file);
out.println("http/1.1 200 ok");
out.println("mime_version:1.0");
out.println("content_type:text/htm1");
out.println("content_length:" + file.length());
out.println("nn");
sendFile(out,file);
}
}
System.out.println("askLinet" + askLine);
HttpServer.resultLog.println("User ask t" +askLine);
}
}catch(IOException e){
}finally{
try{
client.close();
}catch(IOException e){
}
}
}
String getFileName(String askLine){
askLine = askLine.substring(askLine.indexOf(' ') + 1);
askLine = askLine.substring(0,askLine.indexOf(' '));
System.out.println("asdf" + askLine);
try{
if(askLine.indexOf('/') == (askLine.length() - 1)){
askLine = askLine.substring(1);
askLine = askLine + "index.html";
System.out.println("User ask t" + askLine);
HttpServer.resultLog.println("User ask t" + askLine);
}
else{
askLine = askLine.substring(1);
System.out.println("User view t" + askLine);
HttpServer.resultLog.println("User ask t" + askLine);
}
}catch(Exception e){}
return askLine;
}//end getFileName
void sendFile(PrintWriter out,File file) throws IOException{
BufferedReader readFile = new BufferedReader(new FileReader(file));
String s1,s2 = new String();
while((s1 = readFile.readLine()) != null){
s2= s2 + s1 + "n";
}
out.write(s2);
readFile.close();
out.flush();
out.close();
System.out.println(file + "tOK");
}
}//end ServerOne
public class HttpServer{
static PrintWriter resultLog;//log file
static final int PORT = 80;
public static void main(String[] args) throws IOException{
resultLog = new PrintWriter(new BufferedWriter(new FileWriter("httpLog.log")),true);
ServerSocket httpOne = new ServerSocket(PORT);
System.out.println("Server Started");
resultLog.println("Server Started");
try{
while(true){
Socket client = httpOne.accept();
System.out.println(client);
resultLog.println("n" + client);
try{
new ServerOne(client);
}catch(IOException e){
client.close();
e.printStackTrace();
resultLog.println(e + "nCloseed" + "n");
}
}
}finally{
httpOne.close();
System.out.println("Server Closed");
resultLog.println("Server Closed");
}
}
}///:~
|
...
HttpServer.resultLog.println("gett" + file);
out.println("http/1.1 200 ok");
out.println("mime_version:1.0");
// 需要注意一下mime :p
// 下面这句话有点儿武断了^^
out.println("content_type:text/htm1");
out.println("content_length:" + file.length());
out.println("nn");
sendFile(out,file);
...
HttpServer.resultLog.println("gett" + file);
out.println("http/1.1 200 ok");
out.println("mime_version:1.0");
// 需要注意一下mime :p
// 下面这句话有点儿武断了^^
out.println("content_type:text/htm1");
out.println("content_length:" + file.length());
out.println("nn");
sendFile(out,file);
...
|
你写的程序和sun上面的sample挺像, hehehe ... 你对照下面的程序看一下吧 :)
http://developer.java.sun.com/developer/technicalArticles/Networking/Webserver/WebServercode.html
http://developer.java.sun.com/developer/technicalArticles/Networking/Webserver/WebServercode.html