老师看看哪儿出错了?
<%@page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE"/> <title></title> <link rel="stylesheet" type="text/css" href="css/all.css"/> <link rel="stylesheet" type="text/css" href="css/pop.css"/> <link rel="stylesheet" type="text/css" href="css/main.css"/> <script type="text/javascript"> function bodyInit() { if('${result.msg}'){ alert('${result.msg}'); } } </script> </head> <body style="background: #e1e9eb;" onload="bodyInit()"> <form action="" id="mainForm" method="post"> <div class="right"> <div class="current"> 当前位置:<a href="#">导入/导出</a> > 导入结果 </div> <div class="rightCont"> <p class="g_title fix">导入结果展示</p> <table class="tab1"> <tbody> <tr> <td align="right" width="80">标题:</td> <td>${result.title}</td> </tr> </tbody> </table> <div class="zixun fix"> <table class="tab2" width="100%"> <tbody> <tr> <th>序号</th> <th>姓名</th> <th>年龄</th> <th>出生日期</th> </tr> <c:forEach items="result.studentList" var="item" varStatus="s"> <tr> <td>${s.count}</td> <td>${item.name}</td> <td>${item.age}</td> <td><fmt:formatDate value="${item.time}" pattern="yyyy-MM-dd"/></td> </tr> </c:forEach> </tbody> </table> </div> </div> </div> </form> </body> </html>
package org.imocc.service; 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.imocc.dto.ImportExcelParamDto; import org.imocc.dto.ImportExcelResultDto; import org.imocc.entity.Student; import org.imocc.util.FileUtil; import javax.resource.spi.work.Work; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class ExcelService { public ImportExcelResultDto imp(ImportExcelParamDto dto){ ImportExcelResultDto result = new ImportExcelResultDto(); result.setTitle(dto.getTitle()); List<Student> studentList = new ArrayList<>(); // String fileName = null; // try { // fileName = FileUtil.save(dto.getExcel(),FileUtil.SAVE_PATH); // } catch (Exception e) { // e.printStackTrace(); // result.setMsg("保存上传文件失败了"); // } Workbook workbook = null; try { workbook = WorkbookFactory.create(dto.getExcel().getInputStream()); Sheet sheet = workbook.getSheetAt(0); int rowNum = sheet.getLastRowNum(); for (int i = 1; i <=rowNum ; i++) { Row row = sheet.getRow(i); // System.out.println(); // System.out.println("姓名:" + row.getCell(0).getStringCellValue()); // System.out.println("年龄:" + row.getCell(1).getNumericCellValue()); // System.out.println("时间:" + row.getCell(2).getDateCellValue()); Student student = new Student(); student.setName(row.getCell(0).getStringCellValue()); student.setAge((int)row.getCell(1).getNumericCellValue()); student.setTime(row.getCell(2).getDateCellValue()); studentList.add(student); } } catch (Exception e) { e.printStackTrace(); result.setMsg("解析Excel失败了"); } finally { if(workbook!=null){ try { workbook.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } }
package org.imocc.entity; import java.util.Date; public class Student { private String name; private int age; private Date time; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getTime() { return time; } public void setTime(Date time) { this.time = time; } }
package org.imocc.dto; import org.apache.commons.fileupload.FileItem; public class ImportExcelParamDto { private String title; //解析好的FileItem private FileItem excel; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public FileItem getExcel() { return excel; } public void setExcel(FileItem excel) { this.excel = excel; } }
package org.imocc.dto; import org.imocc.entity.Student; import java.util.List; public class ImportExcelResultDto { private String title; private List<Student> studentList; private String msg; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public List<Student> getStudentList() { return studentList; } public void setStudentList(List<Student> studentList) { this.studentList = studentList; } }
package org.imocc.dto; import org.apache.commons.fileupload.FileItem; import java.util.HashMap; import java.util.Map; public class ParamDto { private Map<String,String> paramMap; private Map<String, FileItem> fileMap; public ParamDto(){ paramMap = new HashMap<String,String>(); fileMap = new HashMap<String, FileItem>(); } public Map<String, String> getParamMap() { return paramMap; } public void setParamMap(Map<String, String> paramMap) { this.paramMap = paramMap; } public Map<String, FileItem> getFileMap() { return fileMap; } public void setFileMap(Map<String, FileItem> fileMap) { this.fileMap = fileMap; } }
package org.imocc.servlet; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.imocc.dto.ImportExcelParamDto; import org.imocc.dto.ImportExcelResultDto; import org.imocc.dto.ParamDto; import org.imocc.service.ExcelService; import org.imocc.util.RequestUtil; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.util.List; public class ImportExcelServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if(ServletFileUpload.isMultipartContent(req)){ ParamDto dto = RequestUtil.parseParam(req); ImportExcelParamDto paramDto = new ImportExcelParamDto(); paramDto.setTitle(dto.getParamMap().get("title")); paramDto.setExcel(dto.getFileMap().get("excel")); ExcelService service = new ExcelService(); ImportExcelResultDto resultDto =service.imp(paramDto); req.setAttribute("result",resultDto); }else { // req.getParameter(); } //知道表单是上传文件表单还是普通表单? req.getRequestDispatcher("WEB-INF/jsp/importExcelResult.jsp").forward(req,resp); } }
package org.imocc.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class ImportExcelInitServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.getRequestDispatcher("/WEB-INF/jsp/importExcel.jsp").forward(req,resp); } }
package org.imocc.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class IndexServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(req,resp); } }
package org.imocc.util; import org.apache.commons.fileupload.FileItem; import java.io.File; public class FileUtil { //上传文件的保存路径 public static final String SAVE_PATH ="d:/JAVA/JavaWeb/web/upload/"; //保存上传的文件 public static String save(FileItem fileItem,String path) throws Exception{ String fileName = System.currentTimeMillis() + "_" + fileItem.getName(); fileItem.write(new File(path+fileName)); return fileName; } }
package org.imocc.util; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.imocc.dto.ParamDto; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.List; public class RequestUtil { /**\ * 从request流中解析参数与上传的文件 * @param request */ public static ParamDto parseParam(HttpServletRequest request){ ParamDto result = new ParamDto(); ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory()); upload.setHeaderEncoding("UTF-8"); //解析request流 try { //把整个表单中的文件全部拿进list List<FileItem> fileItemList = upload.parseRequest(request); for (FileItem item:fileItemList){ if(item.isFormField()){ try { result.getParamMap().put(item.getFieldName(),item.getString("UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }else { result.getFileMap().put(item.getFieldName(),item); } } } catch (FileUploadException e) { e.printStackTrace(); } return result; } }
0
收起
正在回答 回答被采纳积分+1
4回答
chrismorgen
2019-04-04 17:55:34
你好同学,请问你引入的jstl的jar包是什么版本呢?根据报错提示来看,问题出在了jstl上,建议你使用如下导包代码试试,看是否可以运行成功,祝学习愉快~
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星