日期:2014-05-17 浏览次数:20829 次
import java.io.BufferedInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class Lianxi1 { final int BUFFER_SIZE=1024; public void saveToFile(String downUrl,String filePath) { HttpURLConnection connect=null; BufferedInputStream in=null; FileOutputStream file=null; byte[] buf=new byte[BUFFER_SIZE]; int size=0; try { URL url=new URL(downUrl); connect=(HttpURLConnection) url.openConnection(); connect.connect(); in=new BufferedInputStream(connect.getInputStream()); file=new FileOutputStream(filePath); while((size=in.read(buf))!=-1) { file.write(buf,0,size); } } catch (MalformedURLException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); }finally { try { file.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } connect.disconnect(); } } public static void main(String[]args){ Lianxi1 d= new Lianxi1(); d.saveToFile("http://news.cn.yahoo.com/08-07-/1028/2j9qn.html", "c:\\abc.html"); } }
------解决方案--------------------
我有代码可以帮帮LZ,LZ用javascript
function onSave(){
var val=document.body.innerHTML;//取body里Html代码,提交到servlet,用下的方法解析
}
有不明白的可以联系我
public void startParse(String sHtml,HttpServletRequest request) { Document doc=null; Element root = null; try { Reader in = new StringReader("<?xml version=\"1.0\" encoding=\"utf-8\"?><root></root>"); doc = new SAXBuilder().build(in); root=doc.getRootElement(); ParserDelegator parser = new ParserDelegator(); HTMLEditorKit.ParserCallback callback = new Callback(root); parser.parse(new StringReader(sHtml), callback, true); XMLOutputter outp = new XMLOutputter();//用于输出jdom 文档 Format format=Format.getPrettyFormat(); //格式化文档 format.setEncoding("GBK"); //由于默认的编码是utf-8,中文将显示为乱码,所以设为gbk outp.setFormat(format); outp.output(doc,System.out); } } public class Callback extends HTMLEditorKit.ParserCallback { private Document doc; private Element root; public Callback(Document _doc){ this.doc=_doc; } public Callback(Element _root){ this.root=_root; } public Document getDoc(){ return doc; } public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) { if (t.equals(HTML.Tag.INPUT)) { for(int r=0;r<root.getContentSize();r++){ String type = (String) a.getAttribute(HTML.Attribute.TYPE); Element table=(Element)root.getChildren().get(r); String id = (String) a.getAttribute(HTML.Attribute.ID); for(int i=0;i<