我想访问保存在我的某个文件夹中的.doc文件。我尝试使用其中一个网站提供的代码。
代码:
package com.read.ws;
import java.io.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import com.sun.jersey.spi.resource.Singleton;
@Path("read")
@Singleton
public class readresource {
@GET
public String fun()throws IOException{
File file = null;
String [] fileData = null;
WordExtractor extractor = null;
try {
file = new File("B:\\ex1.doc");
FileInputStream fis=new FileInputStream(file.getAbsolutePath());
HWPFDocument document=new HWPFDocument(fis);
extractor = new WordExtractor(document);
fileData = extractor.getParagraphText();
// for(int i=0;i<fileData.length;i++){
// if(fileData[i] != null)
// System.out.println(fileData[i]);
// }
// Exception exepe = null;
return "hello";
}
catch(FileNotFoundException exep){return "catch";}}}
但是我遇到了以下错误:
类型:异常报告
消息说明:服务器遇到内部错误(),导致无法完成此请求。
异常:
javax.servlet.ServletException: com.sun.jersey.api.container.MappableContainerException: java.io.IOException: Unable to read entire header; 11 bytes read; expected 512 bytes
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:311)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:425)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:590)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
根本原因
com.sun.jersey.api.container.MappableContainerException: java.io.IOException: Unable to read entire header; 11 bytes read; expected 512 bytes
com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:74)
com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:163)
com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:71)
com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:111)
com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:63)
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:689)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:647)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:638)
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:309)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:425)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:590)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.io.IOException: Unable to read entire header; 11 bytes read; expected 512 bytes
org.apache.poi.poifs.storage.HeaderBlockReader.alertShortRead(HeaderBlockReader.java:130)
org.apache.poi.poifs.storage.HeaderBlockReader.<init>(HeaderBlockReader.java:81)
org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:151)
org.apache.poi.hwpf.HWPFDocument.verifyAndBuildPOIFS(HWPFDocument.java:121)
org.apache.poi.hwpf.HWPFDocument.<init>(HWPFDocument.java:134)
com.read.ws.readresource.fun(readresource.java:26)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:156)
com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:163)
com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:71)
com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:111)
com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:63)
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:689)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:647)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:638)
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:309)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:425)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:590)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.
Apache Tomcat/6.0.13