为什么我得到文件未找到异常?

时间:2014-07-25 09:18:13

标签: java struts2 jxl

我正面临着struts2框架的问题。我想从html表单中获取Action类中excel文件的数据。我得到例外 -

jxl.read.biff.BiffException: The input file was not found

我的Action类是 -

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class UpdateZflexRecordsAction extends ActionSupport {
/**
 * 
 */
private static final long serialVersionUID = 1L;
private boolean result = false;
private String status = null;
private String msg = null;

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getMsg() {
    return msg;
}

public void setMsg(String msg) {
    this.msg = msg;
}

public String execute() {
    result = new sadagi.ericsson.softhuman.model.UpdateZflexRecordsModel()
            .UpdateZflexRecords();

    if (result) {
        this.setStatus("Success");
        this.setMsg("Congratulation ! Your request has been accepted.");
    } else {
        this.setStatus("Failled");
        this.setMsg("Sorry ! Your request has not been accepted.");
    }
    return Action.SUCCESS;
}

}

我的模型类是 -

import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletInputStream;

import jxl.*;
import jxl.read.biff.BiffException;

import org.apache.struts2.ServletActionContext;

public class UpdateZflexRecordsModel {
private Boolean result = false;
private ServletInputStream inputStream = null; 
public Boolean UpdateZflexRecords() {
    HttpServletRequest request = ServletActionContext.getRequest();
    System.out.println(request);//////////////////////////////
    try {
    inputStream = request.getInputStream();
    System.out.println(inputStream);//////////////////////////
    byte[] junk = new byte[1024];
    int bytesRead = 0;
    // strip off the HTTP information from input stream
    // the first four lines are request junk
    bytesRead = inputStream.readLine(junk, 0, junk.length);
    bytesRead = inputStream.readLine(junk, 0, junk.length);
    bytesRead = inputStream.readLine(junk, 0, junk.length);
    bytesRead = inputStream.readLine(junk, 0, junk.length);

    // create the workbook object from the ServletInputStream
    Workbook workbook = Workbook.getWorkbook(inputStream);
    Sheet sheet = workbook.getSheet(0);
    Cell cell = null;
    System.out.println(sheet.getName());

      workbook.close();

} catch (Exception e) {  System.out.println("exce-1");
    // TODO Auto-generated catch block
    e.printStackTrace();
} 

    return result;
}
}

我的struts.xml是 -

 <?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
<struts>  
<constant name="struts.devMode" value="true" /> 
<constant name="struts.multipart.maxSize" value="10000000" />

<package name="default" extends="struts-default, json-default" namespace="/">

<action name="downloadZflexFormat" class="sadagi.my.pro.action.RootMapper" method="downloadZflexFormatAction">  
<interceptor-ref name="accessRequired"/>
<interceptor-ref name="scope" />
<result name="login" type="redirect">/</result>  

    

我的控制台显示异常,例如 -

org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper@51c2e8a4
org.apache.catalina.connector.CoyoteInputStream@533790eb
exce-1
jxl.read.biff.BiffException: The input file was not found
at jxl.read.biff.File.<init>(File.java:124)
at jxl.Workbook.getWorkbook(Workbook.java:268)
at jxl.Workbook.getWorkbook(Workbook.java:253)
at  sadagi.pro.softhuman.model.UpdateZflexRecordsModel.UpdateZflexRecords(UpdateZflexRecordsModel.java:30)
at sadagi.pro.softhuman.action.UpdateZflexRecordsAction.execute(UpdateZflexRecordsAction.java:33)

请帮帮我 感谢

1 个答案:

答案 0 :(得分:0)

您将获得jxl.read.biff.BiffException: The input file was not found

getWorkbook() allowed only File(Excel,etc),InputStream但您尝试提供输入类型ServletInputStream type it worng type.,以便该类通过Exception of FileNotFound

 private ServletInputStream inputStream = null;   // ServletInputStream Types
 Workbook workbook = Workbook.getWorkbook(inputStream);  

检查您的资料库jxl.Workbook.getWorkbook()

public static Workbook getWorkbook(File file) throws IOException, BiffException {....  }
public static Workbook getWorkbook(File file, WorkbookSettings ws) throws IOException, BiffException {  .....   }
public static Workbook getWorkbook(InputStream in) throws IOException, BiffException { ....    }
public static Workbook getWorkbook(InputStream in, WorkbookSettings ws) throws IOException, BiffException {   .......  }