请问老师这个为什么不能导入03word?
<form action="http://localhost:8080/context/servler3" method="post" enctype="multipart/form-data"> <input type="text" name="name"/> </br> <input type="file" name="file"/> </br> <input type="submit" value="导入word"/> </form>
package sc; 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.apache.poi.hwpf.HWPFDocument; 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.*; import java.util.List; @WebServlet("/Servlet3") public class Servlet3 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { DiskFileItemFactory disk = new DiskFileItemFactory(); ServletFileUpload servletFileUpload = new ServletFileUpload(disk); //这个载入会有流异常 List<FileItem> fileItemList = servletFileUpload.parseRequest(request); for (FileItem item : fileItemList) if (item.isFormField()) { String name = item.getFieldName(); String value = item.getString("UTF-8"); System.out.println("name:" + name); System.out.println("value:" + value); } else { HWPFDocument doc = new HWPFDocument(item.getInputStream()); System.out.println(doc.getDocumentText()); doc.close(); } } catch (FileUploadException e) { e.printStackTrace(); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }
0
收起
正在回答 回答被采纳积分+1
3回答
好帮手慕珊
2018-09-11 14:04:10
最终尝试的结果发现,在控制台是没有内容,但是如果把文档内容存储到request中,然后转发到jsp页面显示是可以的。代码如下:
Servlet,只贴了doPost的代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { DiskFileItemFactory disk = new DiskFileItemFactory(); ServletFileUpload servletFileUpload = new ServletFileUpload(disk); //这个载入会有流异常 List<FileItem> fileItemList = servletFileUpload.parseRequest(request); for (FileItem item : fileItemList) if (item.isFormField()) { String name = item.getFieldName(); String value = item.getString("UTF-8"); System.out.println("name:" + name); System.out.println("value:" + value); } else { InputStream is=item.getInputStream(); HWPFDocument doc = new HWPFDocument(is); request.setAttribute("doc",doc.getDocumentText()); doc.close(); } } catch (FileUploadException e) { e.printStackTrace(); } request.getRequestDispatcher("demo.jsp").forward(request,response); }
转发后的demo.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% String s=(String)request.getAttribute("doc"); out.println(s); %> </body> </html>
运行效果如下:
祝学习愉快!
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星