0

请原谅我的无知我是 JSF 和 Primeface 的新手。我用 Primeface 上传了一个文件,然后我需要阅读它。你可以看到我的代码,然后我会问我的问题。

发送.java 类

private UploadedFile file;

public UploadedFile getFile() {
    return file;
}

public void setFile(UploadedFile file) {
    this.file = file;
}

public void handleFileUpload(FileUploadEvent event) {
    try {
        File targetFolder = new File("C:\\Users\\Fatih\\Desktop\\DONE PNS\\pns_text");
        InputStream inputStream =  event.getFile().getInputstream();
        OutputStream out = new FileOutputStream(new File(targetFolder,
                event.getFile().getFileName()));

        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = inputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        inputStream.close();
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

BUFFEREDREADERPNS.java 类

  private BufferedReader br = null;
  private String tokenString = "";


    //SETTERS GETTERS



    public void read(){
    try {

        String sCurrentLine;

        br = new BufferedReader(new FileReader(??????????????));

        while ((sCurrentLine = br.readLine()) != null) {
            System.out.println(sCurrentLine);
            tokenString  = tokenString + sCurrentLine;
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    }

发送.xhtml 文件

 <h:form enctype="multipart/form-data">

<p:fileUpload mode="advanced"
fileUploadListener="#{send.handleFileUpload}"
allowTypes="/(\.|\/)(txt)$/"
auto="true" />
<p:growl id="messages" showDetail="true"/>

</h:form>


<p:commandButton  actionListener="#{bufferedReaderPNS.read}" action="send" 
value="Send" ajax="false" />

我的问题是我不知道如何处理我上传的文件。在 read() 方法的 BUFFEREDREADERPNS.java 类中,我需要用问号写什么?我希望我能清楚地告诉我我的问题,谢谢你的回答。

4

0 回答 0