Meaningless Drivel is fun!*
The moose likes JSP and the fly likes replacing user generated file path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "replacing user generated file path "\" with "\\"" Watch "replacing user generated file path "\" with "\\"" New topic
Author

replacing user generated file path "\" with "\\"

alan rubin
Greenhorn

Joined: Nov 15, 2012
Posts: 4
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>
Ankit Dan
Ranch Hand

Joined: Aug 31, 2012
Posts: 47

You can use "File.separator" as someday your program might run on a platform developed in a far-off land, a land of strange things and stranger people, where horses cry and cows operate all the elevators. In this land, people have traditionally used the ":" character as a file separator, and so dutifully the JVM obeys their wishes.
alan rubin
Greenhorn

Joined: Nov 15, 2012
Posts: 4
thanks Ankit I will check out the File.seperator. But I do have a more pressing question. If cows aren't already operating all elevators...who the heck is in charge of it now?? ;)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

Welcome to the Ranch.

Backing up a light year or so, if you are trying to do a file upload, this isn't going to work. This approach will only work when the browser is running on the same machine as the server and that's not how it's gonna be, is it?

See the JspFaq on the methods that need to be used to effect a file upload.

And you won't be using a JSP to do it -- this sort of thing should be done in a servlet.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
alan rubin
Greenhorn

Joined: Nov 15, 2012
Posts: 4
Hi Bear - thanks for the welcome and your reply. I'm not actually trying to upload the file. I set up the form as an upload so I can obtain the file path and use it to look at the file and create the email responses in a loop. Here's a little more detail. This is for a non profit org that I volunteer for. I'm not a software engineer (software quality actually) but I have a growing interest in java and wanted to create some tools to make the volunteers' life a little easier. You can donate to this non profit via paypal and every month a volunteer will download the donation report in excel and sends out a thank you email to each person that donates. But if the list is really long it takes the volunteer a really long time to copy and paste and email.

So what I did was created a program, initially in java, that will read the excel file for email address, name, donation amount, etc. and plugs those values into a thank you email template (via for loop) for easy copy and pasting. I was having some trouble with hyperlinks in java so I migrated the program over to jsp so users can go thru a browser to get the desired result. If I manually add the file path and drop the escape \ into the path I can run the program no problem, even in jsp. But, the volunteers that will be using this aren't tech savvy and I don't want them messing with the code. So I wanted to create a UI to go along with it. I have no problem replacing values that don't require escape assistance. But that pesky single \ isn't playing nice.

I looked into Ankit's file.separator suggestion but it's unclear how that escapes the \. Thanks again to you both. I'll eventually get to the bottom of this!!!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

Is the file to be read on the client machine (where the browser is) or on the server (where Tomcat or whatever you are using) is running?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

P.S. I suspect that the whole backslash thing is going to become moot once we get down to brass tacks, but as an aside, you can save yourself tons of headaches when dealing with file separators by using forward slashes instead of Windows' brain-dead back-slashes. Java will understand the forward slashes and do the right thing.
alan rubin
Greenhorn

Joined: Nov 15, 2012
Posts: 4
The file will be read on the users machine I believe because when it was in plain java I instructed the software to read the excel file based on the folder/file path. I provided the path manually and did not move it anywhere or actually upload. And I didnt really change much when I switched over to jsp, beyond some code clean up. I only checked out the file separator briefly while at work and it didnt immediately jump out to me. But I will check it out tonight when I get home. So file separator converts \ to /? I know I will have to dig in myself (which I like so I will learn it) but thought I would throw that ? out there. Thanks!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

alan rubin wrote:The file will be read on the users machine

Then, as I said before, your approach is not going to work. The server code cannot read files on the client machine. Period.

You need to perform a file upload, which is a much more complicated endeavor than just using the java File API. See the HTTP portion of this FAQ entry

I believe because when it was in plain java I instructed the software to read the excel file based on the folder/file path.

Web apps are not plain Java. You are in a new world where there is a server and there is a client, and HTTP is means of communicating between them.

Just copying code from a Java program into a JSP isn't going to work. In fact, putting Java code into a JSP at all is an egregious violation of modern best practices, even if the approach would work (which it will not).

So file separator converts \ to /?

No. Do not use \ at all. In file paths, just use / like real operating systems do. Java will do the right thing when accessing the disk. (But as I said, this is moot. File I/O is not what you need to be using.)
 
GeeCON Prague 2014
 
subject: replacing user generated file path "\" with "\\"