我可以正常下载的阿,你用URLConnection方法看看
下载程序:
public class aaa {
public static void main(String[] args) throws Exception{
String param = "test";
URL u = new URL("http://dict.cn/search/?q="+param);
URLConnection urlc = u.openConnection();
InputStream in = urlc.getInputStream();
FileOutputStream out = new FileOutputStream("f:/tmp.txt");
byte[] bf = new byte[512];
int b = 0;
while(true){
b = in.read(bf);
if(b<0) break;
out.write(bf,0,b);
}
out.close();
in.close();
}}
下载程序:
public class aaa {
public static void main(String[] args) throws Exception{
String param = "test";
URL u = new URL("http://dict.cn/search/?q="+param);
URLConnection urlc = u.openConnection();
InputStream in = urlc.getInputStream();
FileOutputStream out = new FileOutputStream("f:/tmp.txt");
byte[] bf = new byte[512];
int b = 0;
while(true){
b = in.read(bf);
if(b<0) break;
out.write(bf,0,b);
}
out.close();
in.close();
}}
http://dict.cn/search/?q=测试
好像报错了
import java.io.*; class untitled1 {
public static void main(String[] args) throws IOException { URL url = new URL("http://sports.163.com/");
URLConnection conn = url.openConnection(); InputStream is = conn.getInputStream();
InputStreamReader sr = new InputStreamReader(is);
int BUFFER = 1024;
char[] cs = new char[BUFFER];
int code = 0;
StringBuffer sb = new StringBuffer();
while((code = sr.read(cs,0,BUFFER))!=-1){
sb.append(cs,0,code);
}
System.out.println(sb);
}
}
那个程序在java application中不会有乱码问题
如果改成bean在jsp中调用的话需要把中文编码为urf-8然后再发送过去
一个urf-8转码的方法
public String getStrByUTF8(String arg0){
if(arg0 == null)
return null;
byte[] str = arg0.getBytes();
StringBuffer sb = new StringBuffer();
for(int i=0;i<str.length;i++){
sb.append("%"+Integer.toHexString(str[i] & 0xFF));
}
return sb.toString();
}
http请求头里有多框架设置么 jdk1.4支持么
----------------------------------------------