package com.lovo.servlet;import java.io.IOException;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.lovo.util.io.CreateHtml;
/**
 * 功能:处理创建问题和答案
 * @author zuoli
 * version v1.0
 * date 08/05/27
 *
 */
public class Test extends HttpServlet{

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("gbk");
response.setContentType("text/html;charset=gbk");
CreateHtml html = CreateHtml.getInstance();
html.setFileName("className.html");
                /*取得当前的直实路径*/
String filePathtemp = this.getServletContext().getRealPath("\\");  
/*路径转换为正常格式*/
String filePath = filePathtemp.replace('\\', '/')+"questionnaire/fabu/";
html.setFilePath(filePath);
String content="dsfdsfdfsdfsdfsd";
/*生成文件*/
html.create();
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
package com.lovo.util.io;import java.io.FileNotFoundException;
import java.io.PrintWriter;/**
 * 功能:在硬盘上生成一个html文件
 * @author zuoli
 * version v1.0
 * date 08/05/30
 *
 */
public class CreateHtml {
private String filePath;/*需创建部件的目录*/
private String fileName;/*需创建的文件名*/
private String content;
private static CreateHtml single;
private  CreateHtml(){

}
public static CreateHtml getInstance(){
System.out.println("this is createHtml");
if(single==null){
single = new CreateHtml();
}
return single;
}

/*创建文件到指定目录*/
public void create(){
PrintWriter out = null;
String realPath = filePath+fileName;
try {
out = new PrintWriter(realPath);
if(out==null){
System.out.println("this is null");
}
out.write(content);
} catch (FileNotFoundException e) {
System.out.println("this is exception");
e.printStackTrace();
}finally{
if(out!=null){
out.flush();
out.close();
}
}
}

public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
}
如上,正常是想在目录下生成一个html文件,但是直接运行时会出空指针错误,如果在打断点测式时则会成功生成,我个人决得是路径的获得执行在.creat()方法的后面了。。请大家帮帮忙啊

解决方案 »

  1.   

    public class Test extends HttpServlet{     protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {         request.setCharacterEncoding("gbk"); 
            response.setContentType("text/html;charset=gbk"); 
            CreateHtml html = CreateHtml.getInstance(); 
            html.setFileName("className.html"); 
            String filePathtemp = this.getServletContext().getRealPath("\\");        // replace('\\', '/')  这个替换没有必要
             String filePath = filePathtemp.replace('\\', '/')+"questionnaire/fabu/"; 
            html.setFilePath(filePath); 
            String content="dsfdsfdfsdfsdfsd"; 
            html.setContent(content);   // 这一句难道不要?
             html.create(); 
        }
    }这样改一下,我都试验过了,都能生成正常的文件了。PS:建议在 create() 方法开始处加上这么一句:File file = new File(filePath);
    if(!file.exists()) {
        file.mkdirs();
    }
      

  2.   

    5楼正确,
    只是File file = new File(filePath);
    if(!file.exists()) {
        file.mkdirs();
    }
    个人觉得不应该加上去,加上了以后我这个文件没生成转而生成了路径,我要的是每次用户发布一个问卷后都会生成一个对应的.html文件,
    5楼强人,
      

  3.   

    哦,如果 filePath 是预先建好的话,那是不用加了,如果没有建好的话,
    第一次保存文件时,这个路径没有啊,在写文件时会产生错误的。加上去的话,就算没有这个路径,也会新建一个,然后再把文件建在这个路
    径下,嘿嘿,实际上也没什么意思,只是预防一下 :)