The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
March 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

excel file upload in unix environment

Posted by Baig on September 13, 2001 at 5:48 AM

I have an excel file and i would like to upload it using servlet.
the following is the code which i have written it runs perfect when the servlet is on windows platform . but when it is on unix server then it doesnt upload. Who do u connect or drivers to be given while uploading an excel file from a user machine when the servlet is running on unix platform??
--------here is the code----------->>>>>
private void doUpload(PrintWriter writer,HttpServletRequest request, HttpServletResponse response) {

//log("entering doUpload");
String pname = "";
String CarrierId = "";
String ProReqHdrId = "";
String FileName = "";

try {
MultipartRequest multi = new MultipartRequest(request,config.getServletContext().getRealPath(File.separator+"WEB-INF"+File.separator+uploaddir+File.separator),5*1024*1024);

Enumeration params = multi.getParameterNames();
while (params.hasMoreElements()) {
pname = (String)params.nextElement();
//log("pname = " + pname);
if (pname.equals("carrierid")) {
CarrierId = (String) multi.getParameter(pname);
//log("Carrier di = " + CarrierId);
}
if (pname.equals("proreqid")) {
ProReqHdrId = (String) multi.getParameter(pname);
//log("proreqid = " + ProReqHdrId);
}
}

Enumeration fnames = multi.getFileNames();
if (fnames.hasMoreElements()) {
FileName = (String) multi.getFilesystemName(String.valueOf(fnames.nextElement()));
}

} catch (IOException ioe) { log("doUpload: upload file error" + ioe.getMessage()); return; }
//log("finished uploading");

String sql = "";

//create HashMap for CarrierRouteId Rate Volume Quality taken from excel file
final int volumeindex = 0;
final int rateindex = 1;
final int qualityindex = 2;

//TODO: add code that would read this data from uploaded excel file
HashMap FileRoute = new HashMap();
Vector offerData = null;
log("BEFORE readign file");
try {
BufferedReader ufile = new BufferedReader(new FileReader(config.getServletContext().getRealPath(File.separator
+"WEB-INF"+File.separator+uploaddir+File.separator+FileName)));

if (ufile !=null) {
String ss;
while ((ss=ufile.readLine())!=null) {

StringTokenizer tz = new StringTokenizer(ss,",");
log("count = " + tz.countTokens());
if (tz.countTokens()==5) {
while (tz.hasMoreTokens()) {
offerData = new Vector();
String carriercode = tz.nextToken().trim();
log("carrier code = " + carriercode);
String routedesc = tz.nextToken();
log(routedesc);
offerData.add(tz.nextToken().trim());
offerData.add(tz.nextToken().trim());
offerData.add(tz.nextToken().trim());
FileRoute.put(carriercode,offerData);
}
}
}
}
ufile.close();
ufile = null;
} catch (IOException ioe) { log("Error reading file: " + ioe.getMessage()); }

//create HashMap for RouteId CarrierRouteId
HashMap RouteConv = new HashMap();

Connection conn = null;

try {
conn = getConnection();
} catch (SQLException sqle) { log("getConnection: " + sqle.getMessage()); }

Statement stm = null;
ResultSet rs = null;

try {
stm = conn.createStatement();
sql = "SELECT PC_Route_Id, Carrier_Route_Id FROM ROUTE_CONVERSION WHERE Carrier_Id = " + CarrierId;
rs = stm.executeQuery(sql);

String RouteId = "";
String CarrierRouteId = "";

while (rs.next()) {
RouteId = rs.getString("PC_Route_Id").trim();
CarrierRouteId = rs.getString("Carrier_Route_Id").trim();
RouteConv.put(RouteId,CarrierRouteId);
}
rs.close();

} catch (SQLException sqle) { log("doUpload: RouteConv error " + sqle.getMessage()); }

String data = "";
int color = -1;
String AvailableCapacity = "0";
String QualityList = "";
double RateOfExchange = 0;
HashMap QualityConv = new HashMap();

try {
stm = conn.createStatement();

sql = "SELECT Rate_of_Exch FROM Carrier WHERE Carrier_Id = " + CarrierId;
try {
rs = stm.executeQuery(sql);
if (rs.next()) {
RateOfExchange = Double.valueOf(rs.getString("Rate_of_Exch")).doubleValue();
}
rs.close();
} catch (SQLException sqle) {log("doUpload: Error getting RateOfExchange" + sqle.getMessage()); }
rs = null;

Vector v = getQualityVector(stm,true);
QualityList = (String) v.elementAt(0);
QualityConv = (HashMap) v.elementAt(1);
log("QualityList now = " + QualityList);

AvailableCapacity = getAvailableCapacity(ProReqHdrId, CarrierId, stm);

sql = getUploadSQL(CarrierId, ProReqHdrId);

rs = stm.executeQuery(sql);

//prepare data string
try {
String bgcolor = "#cccccc";
String RouteId = "";
String ProReqItmId = "";
String ReqVolume = "";
String TargetRate = "";
String RouteDesc = "";
String QualityIndex = "";
String ProReqOfferId = "";
String OfferRateHC = "";
String OfferRateSC = "";
String OfferVolume = "";
String OfferQualityId = "";
String OfferQualityIndex = "";

data = "";

data = data + createRoundOneFormHeader(ProReqHdrId,CarrierId);

while (rs.next()) {
color = -1-color;
if (color==0) { bgcolor="#cccccc"; } else { bgcolor = "#efefef"; }

RouteId = (String) rs.getString("Route_Id");
if (RouteId == null) { RouteId = ""; };

offerData = (Vector) FileRoute.get(RouteConv.get(RouteId));

ProReqItmId = (String) rs.getString("Pro_Req_Itm_Id");
if (ProReqItmId == null) { ProReqItmId = ""; };

ReqVolume = (String) rs.getString("Req_Volume");
if (ReqVolume == null) { ReqVolume = ""; };

TargetRate = (String) rs.getString("Target_Rate");
//TargetRate = String.valueOf(Math.round(Double.doubleValue(rs.getString("Target_Rate")) * 1000) / 1000);
if (TargetRate == null) {
TargetRate = "";
} else {
TargetRate = String.valueOf(roundDouble(Double.parseDouble(TargetRate),4));
}

RouteDesc = (String) rs.getString("Route_Desc");
if (RouteDesc == null) { RouteDesc = ""; };

QualityIndex = (String) rs.getString("Quality_Index");
if (QualityIndex == null) { QualityIndex = ""; };

ProReqOfferId = (String) rs.getString("Pro_Req_Offer_Id");
if (ProReqOfferId == null) { ProReqOfferId = ""; };

//OfferRateHC = String.valueOf(rs.getDouble("Offer_Rate_HC"));
OfferRateHC = (String) rs.getString("Offer_Rate_HC");
if (offerData != null) {
OfferRateHC = String.valueOf(Double.valueOf(String.valueOf(offerData.elementAt(rateindex))).doubleValue()* RateOfExchange);
}
if (OfferRateHC == null) { OfferRateHC = ""; };

OfferRateSC = (String) rs.getString("Offer_Rate_SC");
if (OfferRateSC == null) { OfferRateSC = ""; };

OfferVolume = (String) rs.getString("Offer_Volume");
if (offerData != null) {
OfferVolume = (String) offerData.elementAt(volumeindex);
}
if (OfferVolume == null) { OfferVolume = ""; };
if (OfferVolume.trim().equals("") && defaultVolume) {
OfferVolume = ReqVolume;
}

OfferQualityId = (String) rs.getString("Offer_Quality_Id");
if (OfferQualityId == null) { OfferQualityId = ""; };
if (offerData != null) {
OfferQualityIndex = (String) offerData.elementAt(qualityindex);
} else {
OfferQualityIndex = QualityConv.get(OfferQualityId)==null ? "" : (String) QualityConv.get(OfferQualityId);
}

data = data + createRoundOneRow(bgcolor,ProReqOfferId,ProReqItmId,RouteDesc,ReqVolume,OfferVolume,TargetRate,OfferRateHC,QualityIndex,OfferQualityIndex);

}

//upload and submit buttons
data = data + createRoundOneFormFooter(ProReqHdrId, CarrierId);

//show html template with data string
BufferedReader reader = getBufferedReader("round1.htm");
try {
//showHtmlTemplate(writer,html,"<>",data);
if (reader !=null) {
String ss;
while ((ss=reader.readLine())!=null) {
ss=myreplace("<>",AvailableCapacity,ss);
ss=myreplace("<>",QualityList,ss);
ss=myreplace("<>",data,ss);
//ss=myreplace("$EXAMPLES",examples,ss);
writer.println(ss);
}
reader.close();
}
reader = null;
} catch (IOException ioe) { log("doUpload: Error returning html"+ioe.getMessage()); }

} catch (SQLException sqle) { log("doUpload: ResultSet error "+sqle.getMessage()); }

} catch (SQLException sqle) { log("doUpload: Error executing query "+sql+sqle.getMessage()); }

//clean up recordset
try {
if (rs!=null) { rs.close(); }
} catch (SQLException sqle) { log("doUpload: Error closing resultset"); }
try {
if (rs!=null) { stm.close(); }
} catch (SQLException sqle) { log("doUpload: Error closing statement"); }

rs = null;
stm = null;

free(conn);

} //doUpload



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us