黑马程序员技术交流社区
标题:
关于URL.openStream()
[打印本页]
作者:
daividtu
时间:
2015-12-10 17:59
标题:
关于URL.openStream()
URLDemo
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class URLDemo {
public static void main(String[] args) throws IOException {
String str_url = "http://127.0.0.1:9090/myweb/1.html?name=lisi";
URL url = new URL(str_url);
System.out.println("getProtocol:"+url.getProtocol());
System.out.println("getHost:"+url.getHost());
System.out.println("getPort:"+url.getPort());
System.out.println("getFile:"+url.getFile());
System.out.println("getPath:"+url.getPath());
System.out.println("getQuery:"+url.getQuery());
/*InputStream in = url.openStream();//相当于url.openConnection().getInjputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf,0,len);
System.out.println(text);
in.close();*/
BufferedReader bfrd = new BufferedReader(new InputStreamReader(url.openStream()));
String line = null;
while((line=bfrd.readLine())!=null)
{
System.out.println(line);
}
bfrd.close();
}
}
复制代码
服务端代码
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class MyTomcat {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(9090);
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress()+"....connected");
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf,0,len);
System.out.println(text);
//给客户端一个反馈信息。
File directory = new File("");
String urlstr = text.split("\r\n")[0].split(" ")[1];
String urlstr2 =null;
if(urlstr.contains("?"))
urlstr2 = urlstr.substring(0, urlstr.lastIndexOf('?'));
else
urlstr2 = urlstr;
String path =directory.getAbsolutePath()+urlstr2;
BufferedReader bfrd = new BufferedReader(new InputStreamReader(new FileInputStream(new File(path))));
BufferedWriter bfwr = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line = null;
while((line=bfrd.readLine())!=null)
{
bfwr.write(line);
bfwr.newLine();
bfwr.flush();
}
//PrintWriter out = new PrintWriter(s.getOutputStream(),true);
//out.println("<font color='red' size='7'>欢迎光临</font>");
s.close();
ss.close();
}
}
复制代码
用浏览器访问正常
C:\Users\windows\Desktop\11 (2).png
urlDemo 访问 抛IO异常 ?? 不知怎么造成的
getProtocol:http
getHost:127.0.0.1
getPort:9090
getFile:/myweb/1.html?name=lisi
getPath:/myweb/1.html
getQuery:name=lisi
Exception in thread "main" java.io.IOException: Invalid Http response
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1555)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at java.net.URL.openStream(URL.java:1038)
at URLDemo.main(URLDemo.java:21)
作者:
daividtu
时间:
2015-12-10 18:01
注: 浏览器直接访问这个地址http://127.0.0.1:9090/myweb/1.html?name=lisi 是正常的
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2