0

I have a program that will upload the excel file and then get it's content to be put in a database.

This part of the code works when I try to upload an excel file, here is my code in uploading excel file:

public class uploadFile extends FileUploadAction
{
public boolean processRequest(HttpServlet servlet, HttpServletRequest request,  HttpServletResponse response)
{
if ((request.getContentType() != null)){
  try {
    Properties appProperties = getAppProperties();
    String dbMap = appProperties.getProperty("dbMap");
    DB db = getDBConnection(dbMap);
    String fileSavePathTemp = appProperties.getProperty("filesavepathtemp");
    MultipartParser parser = new MultipartParser(request, 10485760);
    Part _part = null;

    String urlVars = "";
    String fname = "";

    while ((_part = parser.readNextPart()) != null)
    {
      if (_part.isFile())
      {
        FilePart fPart = (FilePart)_part;

        long fileSize = 0L;
        fname = request.getParameter("filename");

        if (fname != null)
        {
         fileSize = fPart.writeTo(new File(fileSavePathTemp + System.getProperty("file.separator") + fname));
          urlVars = urlVars + "fileName=" + URLEncoder.encode(fname, "UTF-8") + "&";         
        }  
        continue;

      }
      ParamPart pPart = (ParamPart)_part;

      urlVars = urlVars + pPart.getName() + "=" + URLEncoder.encode(pPart.getStringValue(), "UTF-8") + "&";
    }

    response.sendRedirect("AppServlet?" + urlVars);
    return false;
  } catch (Exception e) {
    System.out.println("Error on uploadFile class: " + e.toString());
    request.setAttribute("msgTitle", "File upload failed");
    request.setAttribute("message", e.toString());
  }
}

return true;
}
}

Here is the code getting the contents of the excel file:

 File srcFile = new File(fname);
 try{
          Workbook workbook = Workbook.getWorkbook(srcFile); //new File(dirname+fileName));
          Sheet mySheet = workbook.getSheet(0);
          int p = mySheet.getRows();
          for (int row = 0; row <= mySheet.getRows()-1; row++)
          {
           String material = mySheet.getCell(0, row).getContents();
           double price = Double.parseDouble(mySheet.getCell(1,row).getContents());
          //Insert the contents of the excelfile to the temporary table   
           db.sqlExecute("insert into TEMP_4252013 (material,price)  values ('" +material+"','"+price+"')");     
           db.sqlExecute("commit");    
          }
          }catch(Exception e){
              e.printStackTrace();
          }

but when I put the code above for getting the contents of the excel file I get this error:

java.io.FileNotFoundException: test.xls (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at sources.UploadAIP.actions.uploadFile.processRequest(uploadFile.java:71)
at com.ti.ffw.Libs.WebApp.AppServlet.doAction(AppServlet.java:368)
at com.ti.ffw.Libs.WebApp.AppServlet.processRequest(AppServlet.java:251)
at com.ti.ffw.Libs.WebApp.AppServlet.doPost(AppServlet.java:206)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:304)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:662)

What is wrong or what I need to do to make it work?

4

2 回答 2

0

这是什么 ?..

}catch(Exception e){
          e.printStackTrace();
}

双抓?

} catch (Exception e) {
System.out.println("Error on uploadFile class: " + e.toString());
}
于 2013-05-06T01:13:11.013 回答
0

您将文件写入到,fileSavePathTemp + System.getProperty("file.separator") + fname但您正在从fname.

这就是为什么有一个java.io.FileNotFoundException.

尝试:

File srcFile = new File(fileSavePathTemp + System.getProperty("file.separator") + fname);
于 2013-05-06T01:13:48.413 回答