簡體   English   中英

如何逐行讀取文件並使用帶有jsonp的java和ajax發送對ajax請求的響應?

[英]How to read file line by line and send response to ajax request using java and ajax with jsonp?

我想逐行讀取位於不同域上的文件,並在讀取每一行時將響應發送給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請求的響應?

    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();

“但是我想一次將文件的所有內容讀取為一行。”

是否要將整個文件讀取為字符串?如果是,則使用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文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM