Actually when i try to upload a file by using apache  poi library i get this
error.When i used request.getParameter i was not able to get complete path
in mozilla so i have written a code as



package org.ofbiz.product.product;

//~--- non-JDK imports
--------------------------------------------------------

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.util.EntityUtil;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.fileupload.*; 
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.io.FilenameUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.ofbiz.base.util.string.FlexibleStringExpander;


//~--- JDK imports
------------------------------------------------------------
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.ofbiz.base.util.HttpRequestFileUpload;

import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;

import org.ofbiz.base.util.Debug;
/**
* This java program is used to read the data from a Excel file and display
them
* on the console output.
*
* @author dhanago
*/
public class POIExcelReader
{
           public static String module = POIExcelReader.class.getName();
           
/** Creates a new instance of POIExcelReader */
public POIExcelReader ()
{}

/**
* This method is used to display the Excel content to command line.
*
* @param xlsPath
*/
//@SuppressWarnings ("unchecked")
public static String displayFromExcel (HttpServletRequest
request,HttpServletResponse response)
{
        
  Debug.logInfo("################## Inside method displayFromExcel",module);
  //String xlsPath = "C:\\harish.xls";
  //String path = System.getProperty("user.dir") + "\\framework";  
  //String path = request.getParameter("product");
  String imageFilenameFormat = UtilProperties.getPropertyValue("catalog",
"image.filename.format");
  String imageServerPath = UtilProperties.getPropertyValue("catalog",
"image.server.path");
  String imageUrlPrefix = UtilProperties.getPropertyValue("catalog",
"image.url.prefix");

  FlexibleStringExpander filenameExpander = new
FlexibleStringExpander(imageFilenameFormat);
  
  String contentType = null;
  String path = request.getParameter("product");
  if (path != null) {
    
         
      String fileLocation =
filenameExpander.expandString(UtilMisc.toMap("location",
"productsspreadsheet", "type", "spreadsheet", "id", "productexecl"));
      String filePathPrefix = "";
      String filenameToUse = fileLocation;
      if (fileLocation.lastIndexOf("/") != -1) {
          filePathPrefix = fileLocation.substring(0,
fileLocation.lastIndexOf("/") + 1); // adding 1 to include the trailing
slash
          filenameToUse =
fileLocation.substring(fileLocation.lastIndexOf("/") + 1);
      }
      
      int i1;
      if (contentType != null && (i1 = contentType.indexOf("boundary=")) !=
-1) {
          contentType = contentType.substring(i1 + 9);
          contentType = "--" + contentType;
      }
          
      String defaultFileName = filenameToUse + "_temp";
      HttpRequestFileUpload uploadObject = new HttpRequestFileUpload();
      uploadObject.setOverrideFilename(defaultFileName);
      uploadObject.setSavePath(imageServerPath + "/" + filePathPrefix);
      try{
      uploadObject.doUpload(request);
      }
      catch(Exception e)
      {
          e.printStackTrace(); 
      }
      
      String clientFileName = uploadObject.getFilename();
     
      if (clientFileName != null && clientFileName.length() > 0) {
          if (clientFileName.lastIndexOf(".") > 0 &&
clientFileName.lastIndexOf(".") < clientFileName.length()) {
              filenameToUse +=
clientFileName.substring(clientFileName.lastIndexOf("."));
          } else {
              filenameToUse += ".xls";
          }
      
          
          String characterEncoding = request.getCharacterEncoding();
          try{
          String imageUrl = imageUrlPrefix + "/" + filePathPrefix +
java.net.URLEncoder.encode(filenameToUse, characterEncoding);
          }
          catch(Exception e) { 
              e.printStackTrace();
          }
  

          
          try {
              File file = new File(imageServerPath + "/" + filePathPrefix,
defaultFileName);
              File file1 = new File(imageServerPath + "/" + filePathPrefix,
filenameToUse);
              try {
                  file1.delete();
              } catch(Exception e) { 
                  Debug.logInfo("error deleting existing file (not
neccessarily a problem)",module);
              }
              file.renameTo(file1);
          } catch(Exception e) { 
              e.printStackTrace();
          }
  

  Debug.logInfo("################## xlsPath"+path,module);
  GenericDelegator delegator = (GenericDelegator)
request.getAttribute("delegator");
        InputStream inputStream = null;

try
{
inputStream = new FileInputStream (path);
}
catch (FileNotFoundException e)
{
Debug.logInfo("################## File not found in the specified
path"+path,module);
e.printStackTrace ();
}

POIFSFileSystem fileSystem = null;

try
{
fileSystem = new POIFSFileSystem (inputStream);
Debug.logInfo("################## fileSystem"+fileSystem,module);
HSSFWorkbook      workBook = new HSSFWorkbook (fileSystem);
HSSFSheet         sheet    = workBook.getSheetAt (0);
Iterator rows     = sheet.rowIterator();
rows.next ();


//String productId = null;
//String productTypeId = null;
//String internalName = null;
//String productName = null;
//String description = null;
//String is_Virtual = null;
//String is_Variant = null;
//String on_Ebay = null;
//String on_Amazon = null;
List products = new ArrayList(); 
List finalresult = new ArrayList();
while(rows.hasNext()){
    HSSFRow row=(HSSFRow)rows.next();
   
    Iterator cells=row.cellIterator();
    Debug.logInfo("cells="+cells,module);
    while(cells.hasNext()){
        HSSFCell cell=(HSSFCell)cells.next();
        int cellType=cell.getCellType();
       Debug.logInfo("cells="+cellType,module);
                        if(cellType==HSSFCell.CELL_TYPE_STRING){
                            HSSFRichTextString 
strData=cell.getRichStringCellValue();
                            int strData1=cell.getColumnIndex();
                            Debug.logInfo("################## productId"+ 
strData,module);
                            List result = new ArrayList();
                                 if (row != null) {
                                            // read productId from first column 
"sheet column
index
                                            // starts from 0"
                                                    HSSFCell cell0 = 
row.getCell((short) 0);
                                            
cell0.setCellType(HSSFCell.CELL_TYPE_STRING);
                                            String productId = 
cell0.getStringCellValue();
                                            // read QOH from ninth column
                                            Debug.logInfo("##################
productId"+productId,module);
                                            
                                            boolean productExists =
FileImportHelper.checkProductExists(productId, delegator); 
                                            if (productId != null &&
!productId.trim().equalsIgnoreCase("")) 
                                            {
                                                
products.add(FileImportHelper.prepareProduct(productId)); 
                                            }
                                            HSSFCell cell1 = 
row.getCell((short) 1);
                                            
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
                                            String productTypeId = 
cell1.getStringCellValue();
                                            Debug.logInfo("##################
productTypeId"+productTypeId,module);
                                            HSSFCell cell2 = 
row.getCell((short) 2);
                                            
cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
                                            String internalName = 
cell2.getStringCellValue();
                                            Debug.logInfo("##################
internalName"+internalName,module);
                                            HSSFCell cell3 = 
row.getCell((short) 3);
                                            
cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
                                            String productName = 
cell3.getStringCellValue();
                                            Debug.logInfo("##################
productName"+productName,module);
                                            HSSFCell cell4 = 
row.getCell((short) 4);
                                            
cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
                                            String description = 
cell4.getStringCellValue();
                                            Debug.logInfo("##################
description"+description,module);
                                            HSSFCell cell5 = 
row.getCell((short) 5);
                                            
cell5.setCellType(HSSFCell.CELL_TYPE_STRING);
                                            String isVirtual =
cell5.getStringCellValue().toString();
                                            Debug.logInfo("##################
is_Virtual"+isVirtual,module);
                                            HSSFCell cell6 = 
row.getCell((short) 6);
                                            
cell6.setCellType(HSSFCell.CELL_TYPE_STRING);
                                            String isVariant =
cell6.getStringCellValue().toString();
                                            Debug.logInfo("##################
is_Variant"+isVariant,module);
                                            HSSFCell cell7 = 
row.getCell((short) 7);
                                            
cell7.setCellType(HSSFCell.CELL_TYPE_STRING);
                                            String onEbay =
cell7.getStringCellValue().toString();
                                            Debug.logInfo("##################
on_Ebay"+onEbay,module);
                                            HSSFCell cell8 = 
row.getCell((short) 8);
                                            
cell8.setCellType(HSSFCell.CELL_TYPE_STRING);
                                            String onAmazon =
cell8.getStringCellValue().toString();
                                            Debug.logInfo("##################
on_Amazon"+onAmazon,module);
                                            Map abs=new HashMap();
                                abs.put("productId",productId);
                                abs.put("productTypeId",productTypeId);
                                abs.put("internalName",internalName);
                                abs.put("productName",productName);
                                abs.put("description",description);
                                abs.put("isvirtual",isVirtual);
                                abs.put("isvariant",isVariant);
                                abs.put("onEbay",onEbay);
                                abs.put("onAmazon",onAmazon);
                                finalresult.add(abs);
                                            //HSSFCell cell8 = 
row.getCell((short) 8); 
                               // Debug.logInfo("################## setting
productId"+ productId,module);
                                                        
//Debug.logInfo("################## setting productTypeId"+
productTypeId,module);
                                                        
//Debug.logInfo("################## setting internalname"+
internalName,module);
                                                        
//Debug.logInfo("################## setting productname"+
productName,module);
                                                        
//Debug.logInfo("################## setting
productTypeId"+productTypeId,module);
                                                        
//Debug.logInfo("################## setting internalname"+
onEbay,module);
                                                        
//Debug.logInfo("################## setting internalname"+
onAmazon,module);
                                                        
//Debug.logInfo("################## setting internalname"+
isVirtual,module);
                                                        
//Debug.logInfo("################## setting internalname"+
isVariant,module);
                                        //GenericDelegator delegator = 
(GenericDelegator)
request.getAttribute("delegator");
                                        GenericValue Prod =
delegator.findByPrimaryKey("Product",UtilMisc.toMap("productId",
productId));
                                        Debug.logInfo("################## Prod" 
+Prod,module);
                                            if (Prod==null)
                                            {

                                                Prod = 
delegator.makeValue("Product", null);
                        Prod.set("productId",productId);
                                                Prod.set("productTypeId", 
productTypeId);
                                                Prod.set("internalName", 
internalName);
                                                Prod.set("productName", 
productName);
                                                Prod.set("description", 
description);
                                        Prod.set("isVariant", isVariant);
                                        Prod.set("isVirtual", isVirtual);
                                Prod.set("onEbay", onEbay);
                                            Prod.set("onAmazon", onAmazon);
                                            Prod.create();
                                                //Prod.set("productId", 
productId);
                                            }
                                             
                            }
                        }
                            
                                        
                        
                                        
                                         
                        else if(cellType==HSSFCell.CELL_TYPE_NUMERIC){
                            double data=cell.getNumericCellValue();
                            Debug.logInfo("################## else"+ 
data,module);
                        
                        }
    
    //Debug.logInfo("################## finalresult" +finalresult,module);
}
//Debug.logInfo("################## after whiles finalresult"
+finalresult,module);
    }
}



catch(Exception e)
{
        return "error"; 
}

return "success";       
}
  }
return "success";
}
}



###############################################################

Getting error as


##################################################################

waitingwaited 21 times, bailing out while still expecting 18 bytes.
java.io.IOException: waited 21 times, bailing out while still expecting 18
bytes.
        at
org.ofbiz.base.util.HttpRequestFileUpload.waitingReadLine(HttpRequestFileUpload.java:280)
        at
org.ofbiz.base.util.HttpRequestFileUpload.doUpload(HttpRequestFileUpload.java:134)
        at
org.ofbiz.product.product.POIExcelReader.displayFromExcel(POIExcelReader.java:100)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at
org.ofbiz.webapp.event.JavaEventHandler.invoke(JavaEventHandler.java:88)
        at
org.ofbiz.webapp.event.JavaEventHandler.invoke(JavaEventHandler.java:74)
        at
org.ofbiz.webapp.control.RequestHandler.runEvent(RequestHandler.java:471)
        at
org.ofbiz.webapp.control.RequestHandler.doRequest(RequestHandler.java:304)
        at
org.ofbiz.webapp.control.ControlServlet.doGet(ControlServlet.java:189)
        at
org.ofbiz.webapp.control.ControlServlet.doPost(ControlServlet.java:77)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:615)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:688)
        at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at
org.ofbiz.webapp.control.ContextFilter.doFilter(ContextFilter.java:248)
        at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
        at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
        at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:568)
        at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
        at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
        at
org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
        at
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
        at
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
        at
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)
        at java.lang.Thread.run(Unknown Source)




                

-- 
View this message in context: 
http://www.nabble.com/IO-exception-tp23036760p23037133.html
Sent from the OFBiz - User mailing list archive at Nabble.com.

Reply via email to