日期:2014-05-18  浏览次数:20741 次

JSP的文件操作。~
比如想读取网络上某一页面的内容~HTML代码~
写入文件,该如何?
FileReader   Fonline=new   FileReader(url);
地址直接为url可以吗?或者有什么好的办法?
谢谢指教!40分~按时结束~

------解决方案--------------------
用本地实际地址,并且要有权去修改
------解决方案--------------------
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String txt=getRemoteInfo( "http://community.csdn.net/Expert/topic/5588/5588150.xml?temp=.7669184 ", "utf-8 ");
writeToFile( "c:/1.txt ",txt, "utf-8 ");
}

public static String getRemoteInfo(String uri,String encode){
StringBuffer result=new StringBuffer( " ");
try {
URL url = new URL(uri);
HttpURLConnection conn =(HttpURLConnection) url.openConnection();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(),encode));
int count = 0;
char[] b = new char[1];
while ((count = rd.read(b)) != -1)result.append(b);
rd.close();
} catch(Exception ex){}
return result.toString();
}

public static void writeToFile(String filepath,String fileInfo,String encode) throws IOException{
File file=new File(filepath);
java.io.BufferedWriter rd = new BufferedWriter(new java.io.OutputStreamWriter(new java.io.FileOutputStream(file),encode));
rd.write(fileInfo);
rd.close();
}
------解决方案--------------------
import java.io.*;
import java.net.*;
import java.util.regex.*;

public class savehtml

{
public static void main(String[]args)
{
try
{
String urlString;
urlString = "http://www.163.com ";
InputStreamReader in = new InputStreamReader(new URL(urlString).openStream());
StringBuffer input = new StringBuffer();
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream( "html.txt "));

int ch;
while((ch = in.read())!=-1)
out.append((char)ch);

}
catch(IOException e)
{
e.printStackTrace();
}
}
}