求大侠们帮帮小女子,线程run方法里面总是出现错误,局部变量filepathString的值运行一次后指向为空了,有什么方法改
package com.proxy;import java.net.*;
import java.io.*;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.DomSerializer;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.PrettyHtmlSerializer;
import org.htmlcleaner.TagNode;
import org.w3c.dom.Document;
public class MyProxyServer
{
public static void main(String args[])
{
try
{
ServerSocket ss=new ServerSocket(81);
System.out.println("正在等待连接......");
while (true)
{
Socket s=ss.accept(); process p=new process(s);
System.out.println("连接成功");
System.out.println("********************************");
System.out.println(" ");
Thread t=new Thread(p);
t.start();
}
}
catch (Exception e)
{
System.out.println("Error:"+e);
}
}
}
class process implements Runnable
{
Socket s;
HtmlCleaner htmlCleaner;
private String toPath = "F:/";
private static int count = 0;
private String filepathString ;
int size=10;
public process(Socket s1)
{
s=s1;
}
public void run()
{
String content=" ";
String debugerinforesult = null;
String debugers = null;
int chByte = 0;
try
{
PrintStream out=new PrintStream(s.getOutputStream());
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
String info=in.readLine();
System.out.println(info);
debugerinforesult=in.readLine();
debugerinforesult+=in.readLine();
debugerinforesult+=in.readLine();
debugerinforesult+=in.readLine();
debugerinforesult+=in.readLine();
debugerinforesult+=in.readLine();
debugerinforesult+=in.readLine();
debugerinforesult+=in.readLine();
System.out.println("debugerinforesult "+debugerinforesult);
System.out.println("now got "+info);
int sp1=info.indexOf(' ');
int sp2=info.indexOf(' ',sp1+1);
String gotourl=info.substring(sp1,sp2); //提取URL地址
System.out.println("now connecting "+gotourl);
//URL con=new URL(gotourl);
count++;
if(true)
{
URL rootcon=new URL(gotourl);
System.out.println(gotourl);
htmlCleaner = new HtmlCleaner();
CleanerProperties props = htmlCleaner.getProperties();
TagNode tagNode = htmlCleaner.clean(rootcon);
Document document = new DomSerializer(props,true).createDOM(tagNode);
if(null == document)
{
System.out.println("Something is error!");
}
else {
System.out.println("Create Dom is Ok!");
}
// 去除文字样式
Object[] nstitle = tagNode.evaluateXPath("//title");
for (Object tt : nstitle)
{
filepathString = toPath + ((TagNode) tt).getText().toString().trim() + ".xml";
}
Object[] nsscript = tagNode.evaluateXPath("//script");
for (Object tt : nsscript)
{
((TagNode) tt).removeFromTree();
}
Object[] nsimg = tagNode.evaluateXPath("//img");
for (Object tt : nsimg)
{
((TagNode) tt).removeFromTree();
}
Object[] nsstyle = tagNode.evaluateXPath("//style");
for (Object tt : nsstyle)
{
((TagNode) tt).removeFromTree();
}
Object[] nsobject = tagNode.evaluateXPath("//object");
for (Object tt : nsobject)
{
((TagNode) tt).removeFromTree();
}
Object[] nfont = tagNode.evaluateXPath("//font");
for (Object tt : nfont)
{
((TagNode) tt).removeFromTree();
}
Object[] nbody = tagNode.evaluateXPath("//body");
for (Object tt : nbody)
{
((TagNode) tt).setAttribute("style", "font-size:"+size+"pt;color:red");
}
Object[] nsdiv = tagNode.evaluateXPath("//div");
for (Object tt : nsdiv)
{
((TagNode) tt).removeFromTree();
}
//文件保存成XML格式
new PrettyHtmlSerializer(props).writeToFile(tagNode, filepathString);
}
//InputStream gotoin=con.openStream(); //数据保存在这里
FileInputStream fin= new FileInputStream(filepathString);
int n=fin.available();
byte buf[]=new byte[1024];
out.println("HTTP/1.1 200 OK");
out.println("MIME_version:1.1");
out.println("Content_Type:text/html");
out.println("Content_Length:"+n);
out.println(" ");
while ((n=fin.read(buf))>=0)
{
out.write(buf,0,n);
//outputstream.write(buf);
}
out.close();
//outputstream.close();
s.close();
count=0;
}
catch (IOException e)
{
System.out.println("Exception:"+e);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
错误提示:java.lang.NullPointerException
at java.io.FileOutputStream.<init>(FileOutputStream.java:172)
at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
at org.htmlcleaner.Serializer.writeToFile(Serializer.java:131)
at org.htmlcleaner.Serializer.writeToFile(Serializer.java:154)
at org.htmlcleaner.Serializer.writeToFile(Serializer.java:164)
at com.proxy.PhoneBeanDo.createDom(PhoneBeanDo.java:137)
at com.proxy.process.run(process.java:137)
at java.lang.Thread.run(Thread.java:619)
java.lang.NullPointerException
at java.io.FileInputStream.<init>(FileInputStream.java:103)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at com.proxy.process.run(process.java:141)
at java.lang.Thread.run(Thread.java:619)
跪求大侠救救小女子,困扰了好几天了,我要把局部变量filepathString(其实是本地生成的.xml地址)的值传到移动设备端,run方法里面,第一次是成功的,以后就不行了,出现上面的错误,再次跪拜各位大侠,谢谢咯 --------------------编程问答-------------------- at com.proxy.process.run(process.java:141)
按照你贴上去的代码的格式拷贝下来,141行不可能报错,141行是个{
需要结合客户端查找原因。
补充:Java , Java相关