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("<html>"
out.print("File upload success. <a href=\"/servlet/files/"
out.print("\">Click here to browse through all uploaded "
out.println("files.</a><br>"
ServletContext sc = getServletContext();
String path = sc.getRealPath("/files"
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("INSERT INTO files_table (doctitle,filepath) VALUES ('"+title+"','"+filepath+"')"
}
}
catch (Exception e) {
throw new ServletException(e);
}
out.println("</html>"
}
public static String basename(String filename){
int slash = filename.lastIndexOf("\\"
if (slash != -1){
filename = filename.substring(slash + 1);
slash = filename.lastIndexOf("/"}
if (slash != -1){
filename = filename.substring(slash + 1);
slash = filename.lastIndexOf(":"}
if (slash != -1){
filename = filename.substring(slash + 1);}
return filename;
}
public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException {
doPost(request, response);
}
}
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("<html>"
out.print("File upload success. <a href=\"/servlet/files/"
out.print("\">Click here to browse through all uploaded "
out.println("files.</a><br>"
ServletContext sc = getServletContext();
String path = sc.getRealPath("/files"
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("INSERT INTO files_table (doctitle,filepath) VALUES ('"+title+"','"+filepath+"')"
}
}
catch (Exception e) {
throw new ServletException(e);
}
out.println("</html>"
}
public static String basename(String filename){
int slash = filename.lastIndexOf("\\"
if (slash != -1){
filename = filename.substring(slash + 1);
slash = filename.lastIndexOf("/"}
if (slash != -1){
filename = filename.substring(slash + 1);
slash = filename.lastIndexOf(":"}
if (slash != -1){
filename = filename.substring(slash + 1);}
return filename;
}
public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException {
doPost(request, response);
}
}