老师,我又有新问题了
导入07版Word时,发生错误:
代码如下:
package com.imooc.service; import com.imooc.dto.ImportWordParamDto; import com.imooc.dto.ImportWordResultDto; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.poifs.filesystem.OfficeXmlFileException; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import java.io.IOException; import java.util.List; public class WordService { public ImportWordResultDto redWord(ImportWordParamDto wordDto) { ImportWordResultDto resultDto = new ImportWordResultDto(); HWPFDocument doc = null; try { //创建word文件对象 doc = new HWPFDocument(wordDto.getWord().getInputStream()); resultDto.setTitle(wordDto.getTitle()); resultDto.setContent(doc.getDocumentText().replace("/r", "<br/>")); }catch (OfficeXmlFileException oe){ System.out.println("这可能是一个07版的Word"); resultDto.setErrorMsg("这可能是一个07版的Word"); } catch (IOException e) { resultDto.setErrorMsg("这可能不是一个Word"); return resultDto; } finally { if (doc != null) { try { doc.close(); return resultDto; } catch (IOException e) { e.printStackTrace(); } } } XWPFDocument docx = null; try { docx = new XWPFDocument(wordDto.getWord().getInputStream()); //获取word文件的每个段落 List<XWPFParagraph> paragraphs = docx.getParagraphs(); //通过for循环得到每个段落中的内容 StringBuilder sb = new StringBuilder(); for (int i = 0; i < paragraphs.size(); i++) { if (i != 0) { sb.append("<br/>"); } sb.append(paragraphs.get(i)); } resultDto.setContent(sb.toString()); } catch (IOException e) { e.printStackTrace(); } finally { if (docx != null) { try { docx.close(); } catch (IOException e) { e.printStackTrace(); } } } return resultDto; } } 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 { //判断请求类型是否为MultipartContent if (ServletFileUpload.isMultipartContent(request)) { ParamDto dto = RequestUtil.parsRequest(request); ImportWordParamDto wordDto = new ImportWordParamDto(); wordDto.setTitle(dto.getParamMap().get("title")); wordDto.setWord(dto.getFileMap().get("word")); WordService word = new WordService(); ImportWordResultDto resultDto = word.redWord(wordDto); request.setAttribute("result", resultDto); request.getRequestDispatcher("/importWordResult.jsp").forward(request, response); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
导入03版时正常显示。07版报错!
0
收起
正在回答
3回答
同学的代码在老师这里是可以正常运行的,建议同学将所有的包替换和老师一样的版本,仔细对比与导入相关的代码是否和老师的代码相同。祝学习愉快~
China_Wang
2019-06-03 18:23:17
老师,我的包是没问题的,文件也是用的老师给我的文件呢!
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星