请问这样的写法是哪里错了,不能导入,也不报错!
<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(name = "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);
}
}
正在回答 回答被采纳积分+1
同学是说跳转有问题吗?还是指的什么问题?
同学的Servlet确实可以出现结果,只是jsp页面中的跳转路径没有和Servlet对应起来,这是我帮助你修改以后的代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title></title> </head> <body> <form action="http://localhost:8080/Servlet3" method="post" enctype="multipart/form-data"> <input type="text" name="name"/> </br> <input type="file" name="file"/> </br> <input type="submit" value="导入word"/> </form> </body> </html>
Servlet:
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); } }
建议同学根据每位老师的建议修改一下自己的代码~
如果还有问题,可以再次提问,祝学习愉快!
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星