丿傲娇 2016-05-13 06:06 采纳率: 0%
浏览 855

strus2 上传xls文件 当文件中的数据超过200条的时候就找不到Action

下面是代码

package com.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.struts2.ServletActionContext;
import com.beans.TjbryyBean;
import com.opensymphony.xwork2.ActionContext;
import com.util.ConnUtil;

/**

  • 团体导入
  • @author Administrator
  • */
    public class SaveXlsFileAction {
    private File[] newsfile;
    private String gzdw;
    private String[] newsfileFileName;
    ResultSet rs = null;
    PreparedStatement pst = null;
    TjbryyBean tb = new TjbryyBean();

    public TjbryyBean getTb() {
    return this.tb;
    }

    public void setTb(TjbryyBean tb) {
    this.tb = tb;
    }

    public String tjttDr() {
    String path1 = "";
    HttpServletRequest request = ServletActionContext.getRequest();
    for (int i = 0; i < this.newsfile.length; i++) {
    File zl = this.newsfile[i];
    String realname = this.newsfileFileName[i];
    if ((zl != null) && (zl.exists()) && (zl.length() > 0L)) {
    SimpleDateFormat f = new SimpleDateFormat("yyyyMMddhhmmss");
    String time = f.format(Calendar.getInstance().getTime());
    String str = realname.substring(realname.lastIndexOf("."),
    realname.length());

            File destFile = new File(ServletActionContext
                    .getServletContext().getRealPath(
                            "\\upload\\" + this.gzdw + "\\" + time + str));
            path1 = path1 + "\\upload\\" + this.gzdw + "\\" + time + str;
            this.tb = new TjbryyBean();
            Workbook book = null;
            int totalRow = 0;
            String errorRows = "";
            Connection conn = new ConnUtil().getConn();
            String sql = "select harm_name from zy_harm";
            try {
                this.pst = conn.prepareStatement(sql);
                this.rs = this.pst.executeQuery();
                List<String> list = new ArrayList();
                while (this.rs.next()) {
                    list.add(this.rs.getString(1));
                }
                InputStream is = new FileInputStream(zl);
                book = WorkbookFactory.create(is);
                Sheet hssfSheet = book.getSheetAt(0);
                totalRow = hssfSheet.getLastRowNum();
                if(totalRow>200){
                    request.setAttribute("errors", "为了保证文件传输速度数据,数据量最大为200条!超过请分多次上传!");
                    return "success";
                }
                int num=0;
                for (int ii = 1; ii <= hssfSheet.getLastRowNum(); ii++) {
                    num=ii;
                    Row row = hssfSheet.getRow(ii);
                    if (row != null) {
                        if (row.getCell(5) != null) {
                            if ("职业".equals(row.getCell(5).getStringCellValue())) {
                                System.out.println(" 姓名"+row.getCell(0));
                                String jhyy = "";
                                if ((row.getCell(3) != null)&& (row.getCell(3).getCellType() == 1)) {
                                    jhyy = row.getCell(3).getStringCellValue();
                                    if (!checkJhyy(jhyy, list)) {
                                        errorRows = errorRows + (ii + 1)+ "、";
                                    }
                                }
                            }
                        }else{
                            request.setAttribute("zt", "表格中第 "+ii+"行 ,第5列未获取到数据!请检查!");
                        }
                    }
                }
                if (!"".equals(errorRows)) {
                    request.setAttribute("errors", "表格中第 " + errorRows
                            + " 行中的危害因素名称与数据库中不相符,无法保存表格。"
                            + "\\n请重新填写标准的危害因素名称!\\各危害因素之间请用中文输入法下的顿号隔开!");
                } else {
                    FileUtils.copyFile(zl, destFile);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return "success";
    

    }

    public boolean checkJhyy(String jhyy, List list) {
    boolean flag = true;
    boolean temp = true;

    String[] jhyys = jhyy.split("、");
    for (int i = 0; i < jhyys.length; i++) {
        for (int j = 0; j < list.size(); j++) {
            if (jhyys[i].equals(list.get(j))) {
                temp = true;
                break;
            }
            temp = false;
        }
        if (!temp) {
            break;
        }
    }
    if (!temp) {
        flag = false;
    }
    return flag;
    

    }

    public File[] getNewsfile() {
    return this.newsfile;
    }

    public void setNewsfile(File[] newsfile) {
    this.newsfile = newsfile;
    }

    public String[] getNewsfileFileName() {
    return this.newsfileFileName;
    }

    public void setNewsfileFileName(String[] newsfileFileName) {
    this.newsfileFileName = newsfileFileName;
    }

    public String getGzdw() {
    return this.gzdw;
    }

    public void setGzdw(String gzdw) {
    this.gzdw = gzdw;
    }
    }

  • 写回答

1条回答 默认 最新

  • 普通网友 2016-10-07 17:13
    关注

    package com.actions;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.List;
    import java.util.Map;
    import javax.servlet.http.HttpServletRequest;
    import org.apache.commons.io.FileUtils;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.ss.usermodel.WorkbookFactory;
    import org.apache.struts2.Servlet

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型