点击导入excel,文件没上传,后台没反应
index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!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>JavaWeb实现导入导出</title> <link href="css/all.css" rel="stylesheet" type="text/css" /> <link href="css/pop.css" rel="stylesheet" type="text/css" /> <link href="css/index.css" rel="stylesheet" type="text/css" /> <script src="js/index.js" type="text/javascript"></script> </head> <body> <!-- 蒙版DIV --> <div id="mengban" style="display: none"></div> <form method="post"> <div id="header"> <div class="iheader"> <div class="logo"> <a href="#"><img src="" alt="" height="88px" width="99px" /></a> </div> <div style="height: 44px;"></div> <ul class="nav" id="mainMenuUl"> <li class="on"><a><span>导入/导出</span></a></li> </ul> </div> </div> <div id="container"> <table style="vertical-align: top" cellspacing="0" cellpadding="0" bgcolor="#e1e9eb" border="0"> <tbody> <tr> <td class="leftTd" style="vertical-align: top" width="150"> <div class="left"> <div class="ileft" id="menuDiv"> <h3 onclick="clickSecondMenu(this,'${basePath}/importExcelInitServlet')"> <a>导入Excel</a> </h3> <h3 onclick="clickSecondMenu(this,'${basePath}/exportExcelInit')"> <a>导出Excel</a> </h3> <h3 onclick="clickSecondMenu(this,'${basePath}/importWordInit')"> <a>导入Word</a> </h3> <h3 onclick="clickSecondMenu(this,'${basePath}/exportWordInit')"> <a>导出Word</a> </h3> </div> </div> </td> <td width="7"> <div class="pointer"></div> </td> <td style="vertical-align: top" height="600px" width="100%"> <br/><iframe id="mainPage" src="" frameborder="0" height="580px" width="100%"></iframe><br /> </td> </tr> </tbody> </table> </div> <div id="footer"> <div class="copyright">慕课网</div> <div class="flr">copyright ©</div> </div> </form> </body> </html> importExcel.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!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"/> </head> <body style="background: #e1e9eb;"> <form id="mainForm" method="post" enctype="multipart/form-data" action="${basePath}/importExcelServlet"> <div class="right"> <div class="current">当前位置:<a href="###">导入/导出</a> > 导入Excel</div> <div class="rightCont"> <p class="g_title fix">导入</p> <table class="tab1" width="100%"> <tbody> <tr> <td align="right" width="10%">标题<font color="red">*</font>:</td> <td width="30%"> <input id="title" name="title" class="allInput" style="width:100%;" type="text"/> </td> <td align="right" width="10%">选择文件<font color="red">*</font>:</td> <td width="30%"> <input type="file" name="excel" style="width:100%;"/> </td> </tr> </tbody></table> <div style="text-align: center; margin-top: 30px;"> <input class="tabSub" value="导 入" onclick="document.getElementById('mainForm').submit();" type="button" /> </div> </div> </div> </form> </body> </html>
package org.imooc.util;
import org.apache.commons.fileupload.FileItem;
import java.io.File;
public class FileUtil {
public static final String SAVE_PATH = "D:/java/Excel/upload/";
public static String save(FileItem fileItem,String path){
String fileName = System.currentTimeMillis()+"_"+ fileItem.getName();
try {
fileItem.write(new File(path + fileName));
} catch (Exception e) {
e.printStackTrace();
}
return fileName;
}
}
package org.imooc.util;
import com.sun.deploy.net.HttpRequest;
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.imooc.dto.ParamDto;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;
public class RequestUtil {
public static ParamDto parseParam(HttpServletRequest request) throws UnsupportedEncodingException {
ParamDto result = new ParamDto();
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
upload.setHeaderEncoding("UTF-8");
try {
List<FileItem> fileItems = upload.parseRequest(request);
for (FileItem fileItem : fileItems) {
if (fileItem.isFormField()) {
result.getParamMap().put(fileItem.getFieldName(),fileItem.getString("UTF-8"));
} else {
result.getFileMap().put(fileItem.getFieldName(),fileItem);
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
return result;
}
}
package org.imooc.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.imooc.dto.ImportExcelParamDto;
import org.imooc.dto.ImportExcelResultDto;
import org.imooc.dto.ParamDto;
import org.imooc.service.ExcelService;
import org.imooc.util.RequestUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet(name = "ImportExcelServlet")
public class ImportExcelServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (ServletFileUpload.isMultipartContent(request)) {
ParamDto dto = RequestUtil.parseParam(request);
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);
request.setAttribute("result", resultDto);
}
request.getRequestDispatcher("/WEB-INF/jsp/importExcelResult.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
package org.imooc.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ImportExcelInitServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/jsp/importExcel.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
正在回答
你的上传界面是没有问题的吧?就是上传Excel到后台时提交后没有反应吧?
建议同学通过F12看一下浏览器控制台报的什么错误,你根据错误修改一下~
另外,你没有贴出来的代码是不是跟老师的源码一致,如果不一致的话,建议同学把你的相关文件贴全,方便我们运行你的代码帮助你调试。
祝学习愉快!
- 参与学习 716 人
- 提交作业 185 份
- 解答问题 1363 个
会Java?懂前端基础?想学后台开发?那么,赶快来学习《Java Web入门》路径吧。本路径主要介绍Java Web的基础知识,并配有大量案例,定会让你收获多多!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星