"request.getParameter()" handles your request and retrieves the parameter value for the given parameter name. But when sending/uploading a file from client side to the server, request.getParameter() doesn't work, and that's where we get stuck!!..But that's quite obvious, because, a file is not like any other formal parameter. A file needs to be transmitted as a MIME stream (Multipurpose Internet Mail Extensions stream) from the client side to the server side and request.getParameter() doesn't work with MIME streams!!Now how to solve this....
There are lots of example out there on the internet, but its very hard to choose which one will work, specially if you don't have any clue, how these things work! So first let me warn you, most of the examples that you'll find over the internet used to work in prehistoric dinosaur era.
Now the internet and web-browsers are pretty much secure and to bring about the security, they have changed the way the old-prehistoric-codes used to handle file uploads! Previously, the entire client path used to get parsed on submit to the servlet (for eg.) providing a scope of security breaching! But now, in all modern browsers, the entire class path doesn't get submitted when the form is serialized, but a fake client path followed by the file name is parsed!..So if you find a code that plays with client side path of the file to upload, dump it straight away!!
The second type of examples that successfully upload the files to the web-server are available on the internet by parsing the entire form-data along with the file as MIME stream and retrieves the file name from the boundaries of the stream. But this is pretty ineffective in the sense that, if you have other parameters like input type text/password or anything, you cannot retrieve them using
request.getParameter() as I mentioned, the entire thing gets parsed as MIME stream and it doesn't work with MIMEs. So if you want to upload a single file from a single form, it works fine. But my suggestion is, try to avoid those codes!Servlet 3.0 to the rescue...
So if you have an application server that supports servlet 3.0(usually recent versions like APACHE TOMCAT v7.0,GLASSFISH v3) you're lucky!..Thanx to my friend Soumyajyoti Bhadra for suggesting this to me!Something similar to
request.getParameter() works wonder!..It's request.getPart(). Using request.getPart() you can easily retrieve your file from the client end along with other parameters.So, let's look into the code - You'll need two files-
1. An HTML file
2. A servlet (and not JSP, tired of telling people, JSPs should be used for presentation logic, while servlets as backend logic, it's a better practice).
First, create an HTML file - Do not forget to use
method="post" and enctype="multipart/form-data", otherwise your file won't get uploaded!
HTML:
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form name="file_upload" method="post" enctype="multipart/form-data" action="UploadFile">
Select File : <input name="file" type="file"/>
<br>
<input type="submit" value="Upload File"/>
</form>
</body>
</html>
Next, create the servlet - UploadFile.java
SERVLET:
package com.example.servlets;
+ import ........; // package imports
public class UploadFile extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, AddressException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
Part myfile = request.getPart("file");
Scanner scanner = new Scanner(myfile.getInputStream()); // upload the file
String filename = s.nextLine(); // read the filename
//get the server file path
String path = this.getServletContext().getRealPath(filename);
out.println("File Upload Successful");
}
catch (Exception e){
e.printStackTrace();}
finally {
out.close();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
try
{
processRequest(request, response);
}catch (AddressException ex) {
Logger.getLogger(UploadFile.class.getName()).log(Level.SEVERE, null, ex);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
try {
processRequest(request, response);
} catch (AddressException ex) {
Logger.getLogger(addemployee.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
That's it...Your file will get uploaded to the web server. Now use the file name to access the file!..cheers..!! Hope you liked it.
No comments:
Post a Comment
Everybody needs feedback, and it's a heck of a lot cheaper than paying a trainer. So Feel free to comment..