Java下载Word文件内容丢失

博主遇到在Java中下载Word文件时,文件内容丢失的问题。下载后的文件与源文件内容格式不一致,并且出现错误提示。寻求解决方案,期望能保持与源文件格式的一致性。
摘要由CSDN通过智能技术生成

源文件:

 

下载后的Word文件:

代码:

public static void downloadDoc(HttpServletRequest request, HttpServletResponse response,String fileName) throws Exception{
		byte[] buff = readWordByType(fileName);
		int length = buff.length;
		fileName = fileName.replaceAll("\\\\", "/");
		fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
		
		//解决中文文件名乱码问题
		if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0){
			fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");//firefox浏览器
		} else{
			fileName = URLEncoder.encode(fileName, "UTF-8");//IE浏览器
		}
		// 设置response的Header
		response.addHeader("Content-Length", "" + length);
		response.addHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
		response.setContentType("application/octet-stream");
		OutputStream output = null;
		try {
			output = new BufferedOutputStream(response.getOutputStream());
			output.write(buff);
			output.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(null != output)
				try {
					output.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
	}
	

错误提示:

org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
	at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:131)
	at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:104)
	at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:138)
	at org.apache.poi.hwpf.HWPFDocumentCore.verifyAndBuildPOIFS(HWPFDocumentCore.java:106)
	at org.apache.poi.hwpf.extractor.WordExtractor.<init>(WordExtractor.java:53)
	at com.nrietcoud.hyqx.server.util.FileUtil.readWordByType(FileUtil.java:615)
	at com.nrietcoud.hyqx.server.util.FileUtil.downloadDoc(FileUtil.java:570)
	at com.nrietcoud.hyqx.server.controller.SlhxController.downloadFile(SlhxController.java:325)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:645)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
	at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
	at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
	at com.nrietcoud.hyqx.server.util.CorsFilter.doFilter(CorsFilter.java:25)
	at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
	at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
	at com.nrietcloud.common.filter.XssFilter.doFilter(XssFilter.java:22)
	at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
	at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
	at org.springframework.boot.actuate.web.trace.servle
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值