关于提示不是文件上传的问题
package com.imooc.servlet; import com.imooc.dto.ImportWordParamDto; import com.imooc.dto.ImportWordResultDto; import com.imooc.dto.ParamDto; import com.imooc.service.WordService; import com.imooc.util.RequestUtil; import org.apache.commons.fileupload.servlet.ServletFileUpload; 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; @WebServlet(name = "ImportWordServlet",urlPatterns = "/importWord") public class ImportWordServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //如果有文件上传请求 if (ServletFileUpload.isMultipartContent(request)){ //调用解析request的方法获得通用参数 ParamDto dto= RequestUtil.parseRequest(request); //通过通用参数的值得到真正的参数 ImportWordParamDto paramDto = new ImportWordParamDto(); paramDto.setTitle(dto.getParamMap().get("title")); paramDto.setWord(dto.getFileMap().get("word")); //调用service的方法得到结果返回给显示结果的页面 WordService service=new WordService(); ImportWordResultDto resultDto=service.imp(paramDto); request.setAttribute("result",resultDto); System.out.println("title="+resultDto.getTitle()); System.out.println("content="+resultDto.getContent()); }else { } request.getRequestDispatcher("/WEB-INF/jsp/importWordResult.jsp").forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } }
<%@page contentType="text/html; utf-8" language="java" 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" action="${basepath}/importWord"> <div class="right"> <div class="current">当前位置:<a href="###">导入/导出</a> > 导入Word</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="word" 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>
调试的时候走到这里,结果为false。导致importWordResult.jsp没有显示。文件是doc文档呀。怎么就不是文件了
0
收起
正在回答 回答被采纳积分+1
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星