Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Servlet MySQL Slash Issue

Status
Not open for further replies.

Ciarrai

Technical User
Jun 27, 2003
17
US
Hi,
I can upload a file to my Tomcat directory nicely. I tested the filepath and its printing "C:\Program Files\Apache Tomcat\Webapps\...etc". I then pass the doc name and filepath to a placeholder in MySQL. BUT, when I retrive the filepath it is minus any slashes. I suppose this is a MySQL issue but I thought maybe someone might have come across it before. Any thoughts would be greatly appreciated.
Heres the code:
***********************
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import java.util.*;
import java.sql.*;
import java.sql.Blob;

public class FileUploadCommons extends HttpServlet {

// The database connection
Connection con=null;

Statement stmt=null;
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/database");
stmt = con.createStatement();
}
catch (Exception e) {
throw new ServletException("Can't init Counter servlet: " +
e.getMessage(), e);
}
}

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();


out.println(&quot;<html>&quot;);
out.print(&quot;File upload success. <a href=\&quot;/servlet/files/&quot;);
out.print(&quot;\&quot;>Click here to browse through all uploaded &quot;);
out.println(&quot;files.</a><br>&quot;);



ServletContext sc = getServletContext();
String path = sc.getRealPath(&quot;/files&quot;);

org.apache.commons.fileupload.DiskFileUpload fu = new
org.apache.commons.fileupload.DiskFileUpload();
fu.setSizeMax(-1);
fu.setRepositoryPath(path);
try {
List l = fu.parseRequest(request);
Iterator i = l.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem)i.next();

File f = new File(path, basename(fi.getName()));
fi.write(f);

String title = f.getName();
String filepath = f.getPath();
System.out.println(filepath);
stmt.executeUpdate(&quot;INSERT INTO files_table (doctitle,filepath) VALUES ('&quot;+title+&quot;','&quot;+filepath+&quot;')&quot;);
}
}
catch (Exception e) {
throw new ServletException(e);

}
out.println(&quot;</html>&quot;);
}

public static String basename(String filename){

int slash = filename.lastIndexOf(&quot;\\&quot;);
if (slash != -1){
filename = filename.substring(slash + 1);

slash = filename.lastIndexOf(&quot;/&quot;);}
if (slash != -1){
filename = filename.substring(slash + 1);

slash = filename.lastIndexOf(&quot;:&quot;);}
if (slash != -1){
filename = filename.substring(slash + 1);}
return filename;
}

public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException {
doPost(request, response);
}
}
 
This fails (ie doesn't preserve back-slashes) as you have seen :

insert into test values ('&quot;C:\Program Files\Apache ');

but this works :

insert into test values ('&quot;C:\\Program Files\\Apache ');

So just replace your single back-slash with a double one.
 
Thanks sedj,i replaced the &quot;\&quot; with &quot;\\&quot; and it works perfectly and, seperately to that I went with the Apache Commons FileUpload package.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top