Hi All-
I'm running into trouble with a windows filepath
string that is user generated. The user clicks browse, selects the excel file and then clicks Submit and the program reads the excel file and grabs some info for an email template. Prior to setting it up as user defined I could easily manually escape out the \ in the code like so:
C:\\Users\\Documents\\excel\\Donations9.xls
But now when the user browses for the file, the path doesn’t contain the escape characters and just contains the single \. Which obviously doesn't work (File Not Found error). I tried replace() and replaceAll() but when I define the characters to be replaced it’s gets weird because I want to find \ in the string and then change to \\. But I get syntax errors and it recommends adding ) and/or ; to complete. I tried creating variables singSlash = "\" and doubSlash = "\\" but the same thing happened (syntax errors). I even tried to convert \ to /. That single \ is haunting me! Quick example:
File f = new File(excel.replaceAll("\", "\\"));
I even tried:
File f = new File(excel.replaceAll("\\", "\\\\"));
I feel like I've tried every conceivable way to replace those single slashes with no luck. Does anyone have any suggestions or know of a tutorial to direct me to? I've been working on this off and on for a couple days and I feel like I’m spinning my wheels at this point. Any direction would be greatly appreciated. Here's the code in the
JSP (this might be an I/O issue but thought I'd start here):
<%@ page import="java.io.*" %>
<%@ page import="java.io.File" %>
<%@ page import="java.io.IOException" %>
<%@ page import="jxl.Cell" %>
<%@ page import="jxl.Sheet" %>
<%@ page import="jxl.Workbook" %>
<%@ page import="jxl.read.biff.BiffException" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> Donation Thank You Email Template</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<input type="file" name="file" id="file" value="Get File" />
<br />
<br />
<input type="submit" name="button" id="button" value="Create Emails" />
</form>
<%
if (request.getMethod().equals("POST")) {
String excel = request.getParameter("file");
File f = new File(excel.replaceAll("\","\\"));
Workbook workbook = Workbook.getWorkbook(f);
Sheet sheet = workbook.getSheet(0);
int rowCount = sheet.getRows();
for (int i = 1; i < rowCount; i++) {
Cell a1 = sheet.getCell(3,i);
Cell b2 = sheet.getCell(6,i);
Cell c3 = sheet.getCell(0,i);
Cell d4 = sheet.getCell(9,i);
String stringa1 = a1.getContents();
String stringb2 = b2.getContents();
String stringc3 = c3.getContents();
String stringd4 = d4.getContents();
out.println(stringd4 + stringb2 + stringc3 + stringa1) //this will also have some text included but I removed to clean up and I get the same issue anyway
}
}
%>
</body>
</html>