commandButton未在jsf中调用操作方法 FileUpload.java package com.primefaces.sample; 导入java.io.BufferedOutputStream; 导入java.io.BufferedReade

commandButton未在jsf中调用操作方法 FileUpload.java package com.primefaces.sample; 导入java.io.BufferedOutputStream; 导入java.io.BufferedReade,jsf,Jsf,commandButton未在jsf中调用操作方法 FileUpload.java package com.primefaces.sample; 导入java.io.BufferedOutputStream; 导入java.io.BufferedReader; 导入java.io.File; 导入java.io.FileInputStream; 导入java.io.FileNotFoundException; 导入java.io.FileOutputStream; 导入java.io.IOExc

commandButton未在jsf中调用操作方法 FileUpload.java
package com.primefaces.sample;
导入java.io.BufferedOutputStream;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.io.Serializable;
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.PreparedStatement;
导入java.sql.SQLException;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Map;
导入javax.faces.bean.ManagedBean;
导入javax.faces.bean.RequestScope;
导入javax.faces.bean.SessionScoped;
导入javax.faces.context.FacesContext;
导入org.apache.myfaces.custom.fileupload.UploadedFile;
@ManagedBean
@请求范围
公共类文件上载{
私人上传文件上传;
私有字符串上传文件名;
私有字符串路径;
国际货币基金组织;
公共字符串getImf(){
返回国际货币基金组织;
}
公共无效setImf(字符串imf){
this.imf=imf;
}
公共字符串getUploadFileName(){
返回上传文件名;
}
public void setUploadFileName(字符串上传文件名){
this.uploadFileName=uploadFileName;
}
公共字符串getDirPath(){
返回路径;
}
公共void setDirPath(字符串dirPath){
this.dirPath=dirPath;
}
公共上载文件getUpload(){
返回上传;
}
公共无效设置加载(上载文件上载){
this.upload=上传;
}
公共文件上载(){}
公共字符串上载文件(){
文件savedFileName;
dirPath=“D:\\testfile\\”;
InputStream fileContent=null;
如果(上传==null){
返回“成功”;
}
试一试{
fileContent=upload.getInputStream();
}捕获(IOE异常){
e、 printStackTrace();
}
uploadFileName=trimFilePath(upload.getName());
savedFileName=新文件(dirPath+uploadFileName);
BufferedOutputStream bos=null;
试一试{
bos=新的BufferedOutputStream(新的FileOutputStream(savedFileName));
}catch(filenotfounde异常){
}
字节[]缓冲区=新字节[1024];
内伦;
试一试{
而((len=fileContent.read(buffer))>=0){
写入(缓冲区,0,len);
}
}捕获(IOE异常){
}
试一试{
fileContent.close();
bos.close();
}捕获(IOE异常){
}
//splitingData.insertData(dirPath,uploadFileName);
返回“家”;
}
公共字符串trimFilePath(字符串文件名){
返回fileName.substring(fileName.lastIndexOf(“/”)+1)。substring(fileName.lastIndexOf(“\\”+1);
}
公共列表getDisplayData(){
List uploadBeans=new ArrayList();
试一试{
FileInputStream fin=newFileInputStream(新文件(dirPath+uploadFileName));
BufferedReader br=新的BufferedReader(新的InputStreamReader(fin));
//从文件中读取记录
br.readLine();//out.println(列);
字符串行=null;
而((row=br.readLine())!=null){
//out.println(世界其他地区);
字符串[]rowtemp=row.split(“,”);
UploadBean UploadBean=新的UploadBean();
uploadBean.setHtno(rowtemp[0]);
uploadBean.setEcode(rowtemp[1]);
uploadBean.setSubcode(rowtemp[2]);
setImf(rowtemp[3]);
setHmfm(rowtemp[4]);
uploadBean.setCredits(rowtemp[5]);
uploadBean.setSubname(rowtemp[6]);
添加(uploadBean);
}
//st2.executeUpdate(“从ROWID不在的resultsdatas中删除(按HTNO、ECODE、SUBCODE、IMF、HMFM、CREDITS、SUBNAME从resultsdatas组中选择MIN(ROWID)”);
fin.close();
}//试一试
catch(filenotfounde异常){
系统输出打印ln(e);
}
捕获(例外e){
系统输出打印ln(e);
}
返回上传的bean;
}
公共字符串uploadAll(){
System.out.println(“chek11---------”+dirPath);
试一试{
System.out.println(“chek1---------”+dirPath);
FileInputStream fin=newFileInputStream(新文件(dirPath+uploadFileName));
System.out.println(“chek1---------1”);
BufferedReader br=新的BufferedReader(新的InputStreamReader(fin));
连接con=null;
//语句st=null;
PreparedStatement pst=null;
String classname=“oracle.jdbc.driver.OracleDriver”;
String driver=“jdbc:oracle:thin:@localhost”;
字符串port=“1521”;
String database=“m6db”;
字符串user=“尽快”;
字符串pass=“尽快”;
Class.forName(classname).newInstance();
con=DriverManager.getConnection(驱动程序+“:“+端口+”/“+数据库,用户,通行证);
//st=con.createStatement();
String column=br.readLine();//out.println(列);
uploadFileName=uploadFileName.substring(0,uploadFileName.length()-4);//从文件名中剪切扩展名“.txt”
//从文件中读取列名
字符串columntemp[]=column.split(“,”);
对于(int i=0;i”+columnttemp[i]);
}
//准备插入查询
String insertquery=“插入”+上传文件名+”值(“;

对于(int i=0;我可以将
process=“testForm”
添加到单个上载按钮中。代码与问题描述不匹配。请发布真实代码。这只是真实代码,i
package com.primefaces.sample;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.apache.myfaces.custom.fileupload.UploadedFile;

@ManagedBean
@RequestScoped
public class FileUpload {

    private UploadedFile upload;
    private String uploadFileName;
    private String dirPath;
    private String imf;

    public String getImf() {
        return imf;
    }

    public void setImf(String imf) {
        this.imf = imf;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getDirPath() {
        return dirPath;
    }

    public void setDirPath(String dirPath) {
        this.dirPath = dirPath;
    }

    public UploadedFile getUpload() {
        return upload;
    }

    public void setUpload(UploadedFile upload) {
        this.upload = upload;
    }

    public FileUpload() {}

    public String uploadFile() {
        File savedFileName;
        dirPath = "D:\\testfile\\";
        InputStream fileContent = null;
        if (upload == null) {
            return "success";
        }

        try {
            fileContent = upload.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        uploadFileName = trimFilePath(upload.getName());
        savedFileName = new File(dirPath + uploadFileName);
        BufferedOutputStream bos = null;

        try {
            bos = new BufferedOutputStream(new FileOutputStream(savedFileName));
        } catch (FileNotFoundException e) {
        }
        byte[] buffer = new byte[1024];
        int len;
        try {
            while ((len = fileContent.read(buffer)) >= 0) {
                bos.write(buffer, 0, len);
            }
        } catch (IOException e) {
        }
        try {
            fileContent.close();
            bos.close();
        } catch (IOException e) {
        }

        // splitingData.insertData(dirPath, uploadFileName);
        return "home";
    }

    public String trimFilePath(String fileName) {
        return fileName.substring(fileName.lastIndexOf("/") + 1).substring(fileName.lastIndexOf("\\") + 1);
    }

    public List<UploadBean> getDisplayData() {
        List<UploadBean> uploadBeans = new ArrayList<UploadBean>();
        try {
            FileInputStream fin = new FileInputStream(new File(dirPath+ uploadFileName));
            BufferedReader br = new BufferedReader(new InputStreamReader(fin));

            // Reading records from file
            br.readLine();// out.println(column);
            String row = null;
            while ((row = br.readLine()) != null) {
                // out.println(row);
                String[] rowtemp = row.split(",");
                UploadBean uploadBean = new UploadBean();
                uploadBean.setHtno(rowtemp[0]);
                uploadBean.setEcode(rowtemp[1]);
                uploadBean.setSubcode(rowtemp[2]);
                uploadBean.setImf(rowtemp[3]);
                uploadBean.setHmfm(rowtemp[4]);
                uploadBean.setCredits(rowtemp[5]);
                uploadBean.setSubname(rowtemp[6]);

                uploadBeans.add(uploadBean);
            }
            // st2.executeUpdate("DELETE FROM resultsdatas WHERE ROWID not in (SELECT MIN(ROWID) FROM resultsdatas GROUP BY HTNO,ECODE,SUBCODE,IMF,HMFM,CREDITS,SUBNAME)");

            fin.close();
        }// try

        catch (FileNotFoundException e) {
            System.out.println(e);
        }
        catch (Exception e) {
            System.out.println(e);
        }
        return uploadBeans;
    }

    public String uploadAll() {
        System.out.println("chek11-----------------"+dirPath);
        try {
            System.out.println("chek1-----------------"+dirPath);
            FileInputStream fin = new FileInputStream(new File(dirPath + uploadFileName));
            System.out.println("chek1-----------------1");
            BufferedReader br = new BufferedReader(new InputStreamReader(fin));

            Connection con = null;
//          Statement st = null;
            PreparedStatement pst = null;

            String classname = "oracle.jdbc.driver.OracleDriver";
            String driver = "jdbc:oracle:thin:@localhost";
            String port = "1521";
            String database = "m6db";
            String user = "asap";
            String pass = "asap";
            Class.forName(classname).newInstance();
            con = DriverManager.getConnection(driver + ":" + port + "/" + database, user, pass);
//          st = con.createStatement();

            String column = br.readLine();// out.println(column);
            uploadFileName = uploadFileName.substring(0, uploadFileName.length() - 4);// to cut the extention ".txt" from the filename

            // Reading column names from file
            String columntemp[] = column.split(",");
            for (int i = 0; i < columntemp.length; i++) {
                System.out.println("columns---------->" + columntemp[i]);
            }

            // Preparing insert query
            String insertquery="insert into "+uploadFileName+" values("; 
            for(int i=0;i<columntemp.length;i++) { 
                insertquery=insertquery+"?";
                if((i+1)<columntemp.length)//to cut the last comma
                    insertquery=insertquery+","; 
            } 
            insertquery=insertquery+")";
            pst=con.prepareStatement(insertquery);

            // Reading records from file
            String row = null;
            while ((row = br.readLine()) != null) {
                String[] rowtemp = row.split(",");
                for(int i=0; i<rowtemp.length; i++) {
                    pst.setString(i+1,rowtemp[i]); 
                }
                pst.executeUpdate();//inserting into database s=s+r;
            }

            // st.executeUpdate("DELETE FROM resultsdatas WHERE ROWID not in (SELECT MIN(ROWID) FROM resultsdatas GROUP BY HTNO,ECODE,SUBCODE,IMF,HMFM,CREDITS,SUBNAME)");
            fin.close();
        }// try

        catch (FileNotFoundException e) {
            System.out.println(e);
        } catch (SQLException e) {
            System.out.println(e);
        } catch (Exception e) {
            System.out.println(e);
        }
        return "login";
    }

    public String single() {
        System.out.println("-----------**---d---");
        /*FacesContext Ctx = FacesContext.getCurrentInstance();
        Map Params = Ctx.getExternalContext().getRequestParameterMap();
        String temp = (String) Params.get("imf");
        System.out.println("-----------**---d---"+temp);*/
        return "login";
    }
}
<html xmlns="http://www.w3c.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <link type="text/css" rel="stylesheet" href="skin.css" />
</h:head>
<h:body>
<h:form id="testForm"> 
    <p:fieldset legend="data">  
        <p:dataTable id="fileData" var="dataList" value="#{fileUpload.displayData}">  
            <p:column headerText="Htno">  
                <h:outputText value="#{dataList.htno}" />  
            </p:column>  

            <p:column headerText="Ecode">  
                <h:outputText value="#{dataList.ecode}" />  
            </p:column>  

            <p:column headerText="Subcode">  
                <h:outputText value="#{dataList.subcode}" />  
            </p:column>  

            <p:column headerText="Imf">  
                <h:outputText value="#{dataList.imf}" />  
            </p:column> 

            <p:column headerText="Action"> 
            <p:commandButton image="Upload" ajax="false" style="margin-right:20px;" action="#{fileUpload.single}" ></p:commandButton>
            </p:column>
        </p:dataTable>
    </p:fieldset> 
</h:form>
<h:form id="UploadAll"> 
<h:inputHidden value="#{fileUpload.uploadFileName}" id="uploadFileName" />
<h:inputHidden value="#{fileUpload.dirPath}" id="dirPath" />
<h:commandButton type="submit" value="Upload All" action="#{fileUpload.uploadAll}"></h:commandButton>
</h:form> 
</h:body>

</html>