如何逐行读取文件并使用java和ajax与jsonp发送对ajax请求的响应?

时间:2014-05-28 04:43:59

标签: java javascript jquery ajax

我想逐行读取不同域上的文件,并在读取每一行时向ajax请求发送响应。

我编写的代码读取文件并将最后一行作为响应发送。

这是我的jquery-ajax代码:

 $.ajax({
 url: 'http://192.168.xx.xx:8080/myproject/readFile',
 dataType: 'jsonp',
 jsonp: 'callback',
 jsonpCallback: 'parseResponse',
 success: function(data,textStatus, jqXHR) {
        console.log(data);
    },
 error: function(jqXHR, textStatus, errorThrown){
        console.log(errorThrown);
        console.log("ok");
        console.log(jqXHR);
        console.log("Something really bad happened " + textStatus);
  }
});

这是我的servlet代码:

public class readFile extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException{
    // The name of the file to open.
    System.out.println("in read file");
    String fileName = "myfile,txt";
    PrintWriter out = response.getWriter();
    // This will reference one line at a time
    String line = null;

    try {
        // FileReader reads text files in the default encoding.
        FileReader fileReader = 
            new FileReader(fileName);

        // Always wrap FileReader in BufferedReader.
        BufferedReader bufferedReader = 
            new BufferedReader(fileReader);
        BasicDBObject oneLine = new BasicDBObject();
        String set_data = "";
        StringBuilder  stringBuilder = new StringBuilder();
        while((line = bufferedReader.readLine()) != null) {

            line = line.replaceAll("\\{","");
            line = line.replaceAll("\\}","");
            line = line.replaceAll("\\*","");
            line = line.replaceAll("\\:","");
            line = line.replaceAll("\\|","");
           // stringBuilder.append(line);

         }  
  /* out.println("parseResponse("+JSON.parse("{'Name':'"+stringBuilder.toString()+"'}")+")");*/
        out.flush();
   out.println("parseResponse("+JSON.parse("{'Name':'"+line+"'}")+")");
    out.flush(); 
        // Always close files.
        bufferedReader.close();         
    }
    catch(FileNotFoundException ex) {
        System.out.println(
            "Unable to open file '" + 
            fileName + "'");                
    }
    catch(IOException ex) {
        System.out.println("Error reading file '" 
            + fileName + "'");      
    }

}
public void set_print(String name){

}

}

此代码读取文件并在每个请求中输出最后一行文件。 但是我想一次读取文件的所有内容。

如何逐行读取文件并将响应发送到ajax请求?

2 个答案:

答案 0 :(得分:0)

    out.print("parseResponse("+JSON.parse("{'Name':'");
    while((line = bufferedReader.readLine()) != null) {

        line = line.replaceAll("\\{","");
        line = line.replaceAll("\\}","");
        line = line.replaceAll("\\*","");
        line = line.replaceAll("\\:","");
        line = line.replaceAll("\\|","");
        out.print(line);
     }  
     out.print("'}")+")");
     out.flash();

答案 1 :(得分:0)

“但我想一次读取文件的所有内容。”

您想将整个文件读取为字符串吗?如果是,则使用Apache Commons FileUtils

它有以下方法:

static String readFileToString(File file); // Reads the contents of a file into a String using the default encoding for the VM.
static String readFileToString(File file, String encoding); //Reads the contents of a file into a String.

Apache Commons IO Docs