|
I'm using the com.oreilly.servlet package in my project to upload a file thru servlets.
It's working just fine.
Here's the full code in the doPost method of the servlet :
String fname="";
String server_path=""; // Specifies the server's root folder.
String upload_directory=""; // Specifies the path of the directory on the server where the file will be saved.
String complete_path=""; // specifies the complete path.
String encoded_file_name=""; // to encode the file name in UTF-8 format.
res.setContentType("text/html");
PrintWriter out = res.getWriter();
server_path = getServletContext().getRealPath("/index.html"); // getting the server's root directory.
upload_directory = "\\sherbir\\uploaded_files"; // path of the uploaded files' directory.
complete_path = server_path + upload_directory; // complete server path for uploading the file.
MultipartRequest mpr = new MultipartRequest(req,complete_path); // upload the file.
Enumeration files = mpr.getFileNames(); // obtaining names of all uploaded files.
out.println("<br><br>");
out.println("<font face=verdana size=3>");
out.println("<center>");
out.println("<font color=red><b><u>CONGRATULATIONS !!</u></b></font>");
String name = (String) files.nextElement();
fname = mpr.getFilesystemName(name); // getting the name of the uploaded file.
out.println("Your file <b><i>" + fname + "</i></b> was successfully uploaded.");
fname=URLEncoder.encode(fname,"UTF-8");
out.println("<br>");
out.println("fname = " + fname);
out.println("<br>");
out.println("<hr>");
out.println("<br>");
out.println("What would you like to do next ?");
out.println("<br>");
out.println("<br>");
out.println("<font size=1>");
out.println("<b><a href=http://localhost:8080/examples/sherbir/uploaded_files/" + fname + ">View .RTF file</a></b>");
out.println(" ");
out.println("<b><a href=http://localhost:8080/examples/upload_file_html.html>Upload another file</a></b>");
out.println(" ");
out.println("<b><a href=javascript:window.close()>Close this Window</a></b>");
out.println("</font>");
out.println("</center>");
}
catch (Exception e) {}
I have placed client-side validations to upload only .DOC/.RTF files.
The files are uploading successfully.
The problem is that if I upload a file names "Java Security.doc" and then when I place the cursor on the link (View .RTF File), it shows the file name as "Java+Security" and gives the following error :
message :- /examples/sherbir/uploaded_files/Java+Security.doc
description :- The requested resource (/examples/sherbir/uploaded_files/Java+Security.doc) is not available.
If I specify the file name as a URLEncoded one, then the file name I get on placing the cursor on the link is simply "Java"...the rest of the file name is missing.
How do I get around this problem ? Please advise.
|
|
|
Encoding a url replaces spaces with +.
The easiest solution might be to just remove the spaces in filenames.
|
|
|
|
|
|
|
|