<%@ page language="java" import="java.io.*" %>
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.net.*" %>
<%
String result = "";
String temp = "";
String object_url_str = "http://www.sina.com.cn"; //目标地址
URL object_url = new URL(object_url_str);
URLConnection object_conn = object_url.openConnection();
HttpURLConnection object_http_conn = (HttpURLConnection)object_conn;
object_http_conn.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(object_conn.getInputStream()));
while((temp = br.readLine())!=null){
result += temp+"\r\n";
}
out.print(result);
%>
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.net.*" %>
<%
String result = "";
String temp = "";
String object_url_str = "http://www.sina.com.cn"; //目标地址
URL object_url = new URL(object_url_str);
URLConnection object_conn = object_url.openConnection();
HttpURLConnection object_http_conn = (HttpURLConnection)object_conn;
object_http_conn.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(object_conn.getInputStream()));
while((temp = br.readLine())!=null){
result += temp+"\r\n";
}
out.print(result);
%>
比如远程文件内容是abcd
显示为a b c d
而且无法处理中文。
楼主试试。这如同获取网页。
1.从远程拷贝过来的文件第一个字符是个黑块, 我想这和while(( n = in.read() ) != -1)
有关系,但不知道怎么解决
2.中文的问题,我把byte[]用gb2312重新构造String也不行,只能显示部分中文,后面的是乱码
如何解决?
测试代码如下:
...
FileWriter fw = new FileWriter( "c:\\a.txt" );
byte[] bytes = new byte[ 1024 ];
int tmp = -1;
String chinese = "";
URL object_url = new URL("http://domain/test.txt");
URLConnection object_conn = object_url.openConnection();
InputStream in = object_conn.getInputStream();
while( ( tmp = in.read( bytes ) )!= -1 )
{
chinese = new String( bytes, "GB2312" );
fw.write( chinese );
}
fw.close();
...
import java.io.*;
import java.net.*;
public class MyUrl
{
public static void main(String[] args)
{
byte[] bytes = new byte[ 1024 ];
int tmp = 1;
try{
String chinese = "";
URL object_url = new URL("http://localhost:1500/ddd.txt");
URLConnection object_conn = object_url.openConnection();
InputStream in = object_conn.getInputStream();
while((tmp = in.read())!= -1)
{
chinese = new String( bytes, "GB2312" );
System.out.println((char)tmp); }
in.close();
}
catch(IOException e)
{
}
}
}这样没错