gpt4 book ai didi

java - 如何创建显示带有延迟加载的文本日志文件的 JSF 页面

转载 作者:行者123 更新时间:2023-11-30 09:33:35 24 4
gpt4 key购买 nike

我想显示显示 Glassfish 日志文件内容的 JSF 页面。到目前为止,我只做了这个:

    import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;

@Named("GlassfishLogFileController")
@ViewScoped
public class GlassfishLogFile implements Serializable
{

String id;
String fileData;

// Constructor
public GlassfishLogFile()
{
// Get the ID value
try
{
this.id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id");
}
catch (Exception e)
{
this.id = null;
}
}
// Path for Glassfish directory with log files
String path = "/opt/glassfish3/glassfish/domains/domain1/logs/" + id;

@PostConstruct
public void redFile()
{
File file = new File(path);

try (FileInputStream fis = new FileInputStream(file))
{

int content;
while ((content = fis.read()) != -1)
{
// convert to char and display it
fileData = fileData + (char)content;
}

}
catch (IOException e)
{
e.printStackTrace();
}
}

public String getdata()
{
return fileData;
}
}

查看:

<div id="settingsdiv" style="width:350px; height:400px; position:absolute;  background-color:r; top:20px; left:1px">

<div id="settingsHashMap" style="width:1050px; height:400px; position:absolute; background-color:r; top:20px; left:1px">

<h:form id="form" >
<p:growl id="growl" showDetail="true" sticky="true" />

<h:inputTextarea rows="30" cols="50" value="#{GlassfishLogFileController.data}" />

</h:form>
</div>

</div>

我感兴趣的是有没有任何可能的方法来实现延迟加载到我将用来显示文本文件内容的文本区域。我想这样做是因为我想节省内存。日志文件有时会非常消耗内存。

而且我还找到了很多如何将 .pdf 文档显示到 JSF 页面中的示例,但没有找到如何将文本文档显示到文本区域中的示例。

祝福

P.S 我更新了代码。现在我得到空的输入字段。不显示日志文件中的数据。

最佳答案

试试这个,我刚刚测试过:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

@Named("GlassfishLogFileController")
@ViewScoped
public class GlassfishLogFile implements Serializable {

String data;

String id;

private int offset = 0;
private int pageSize = 30;
// Path for Glassfish directory with log files
String path = "/opt/glassfish3/glassfish/domains/domain1/logs/" + this.id;

// Constructor
public GlassfishLogFile() {
// Get the ID value
try {
this.id = FacesContext.getCurrentInstance().getExternalContext()
.getRequestParameterMap().get("id");
// We load the first page initially!
this.actionNextPage();
} catch (Exception e) {
this.id = null;
}
}

public String actionClearOffset() {
this.offset = 0;
this.actionNextPage();
return "SUCCESS";
}

public String actionNextPage() {
StringBuilder page = new StringBuilder();

for (int i = 0; i < this.pageSize; i++) {
String line = this.readLine(this.offset);
if (line == null) {
break;
}
System.out.println(this.offset);
this.offset += line.length() + 2;
page.append(line).append(System.getProperty("line.separator"));
}
this.data = page.toString();
return "SUCCESS";
}

public String getData() {
return this.data;
}

public int getPageSize() {
return this.pageSize;
}

public String readLine(long offset) {
String strLine = null;
DataInputStream in = null;
try {
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream(new File(this.path));
// Get the object of DataInputStream
in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));

br.skip(offset);

strLine = br.readLine();
// Close the input stream
} catch (Exception e) {// Catch exception if any
System.err.println("Error: " + e.getMessage());
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return strLine;
}

public void setData(String data) {

}
}

查看:

<div id="settingsdiv" style="width:350px; height:400px; position:absolute;  background-color:r; top:20px; left:1px">

<div id="settingsHashMap" style="width:1050px; height:400px; position:absolute; background-color:r; top:20px; left:1px">

<h:form id="form" >
<p:growl id="growl" showDetail="true" sticky="true" />

<h:commandButton value="Show next #{GlassfishLogFileController.pageSize} lines"
action="#{GlassfishLogFileController.actionNextPage}" />
<h:commandButton value="Clear offset"
action="#{GlassfishLogFileController.actionClearOffset}" />
<h:inputTextarea rows="30" cols="1000" value="#{GlassfishLogFileController.data}" />
</h:form>
</div>

</div>

关于java - 如何创建显示带有延迟加载的文本日志文件的 JSF 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12091269/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com