Sunday, November 25, 2012
Everyone of you know that files are being uploaded to the server and you can easily download them. You must have thought of how these files are being uploaded to server. Today I am going to post how you can very easily create your own file upload server; Previously before Servlet3.0 technology you might have seen that it was a bit difficult process and you had to use third-party library like Apache-commons. But now it has become very easy.
--------------------------------------------------------------------------------------------------------------------------- The first thing you will have to do is to create the upload-form using HTML. In the form you will have to mention the enctype="multipart/form-data" .
- In servlet there is an annotation called MultipartConfig . It has different params like location, maxFileSize and so on. The location should be a valid folder path on server where the file will be uploaded. Now write the part and also do string processing to get the name of the file ro use the same name while uploading it.
- Now display the details of the file uploaded by request forwarding to a JSP.
HTML form and code
--------------------------------------------------------------------------------------------------------------------------
Select a file to upload
<html>
<body>
<h3>File Upload Form</h3>
Select a file to upload
<form action="./upload.html" method="post"
enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br/>
<input type="submit" value="Upload File" />
</form>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------
<body>
<h3>File Upload Form</h3>
Select a file to upload
<form action="./upload.html" method="post"
enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br/>
<input type="submit" value="Upload File" />
</form>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------
Servlet and JSP Code
--------------------------------------------------------------------------------------------------------------------------
/* Servlet Code */
import java.io.IOException;
import java.util.Collection;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
@WebServlet("/upload.html")
@MultipartConfig(location="D:/ Documents",fileSizeThreshold=1024*1024,
maxFileSize=50*1024*1024,maxRequestSize=5*1024*1024*1024)
public final class Uploader extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Collection<Part> parts=request.getParts();
StringBuffer sb=new StringBuffer();
sb.append("<h2>Total "+parts.size()+" parts uploaded</h2>");
for(Part p: parts){
getDetails(sb, p);
p.write(getFileName(p)); //writing file to server
}
request.setAttribute("partDetails",sb);
RequestDispatcher view=request.getRequestDispatcher("result.jsp");
view.forward(request, response); //forwarding to JSP
}
private String getFileName(Part p){
String header=p.getHeader("content-disposition");
String filename = header.substring(header.indexOf("filename=\"")).split("\"")[1]; //getting filename
return filename;
}
private void getDetails(StringBuffer sb,Part p){
sb.append("<p></p>");
for(String header:p.getHeaderNames()){
sb.append("<b>"+header+" : </b>");
sb.append(p.getHeader(header)+"</br>");
}
}
}
import java.util.Collection;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
@WebServlet("/upload.html")
@MultipartConfig(location="D:/ Documents",fileSizeThreshold=1024*1024,
maxFileSize=50*1024*1024,maxRequestSize=5*1024*1024*1024)
public final class Uploader extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Collection<Part> parts=request.getParts();
StringBuffer sb=new StringBuffer();
sb.append("<h2>Total "+parts.size()+" parts uploaded</h2>");
for(Part p: parts){
getDetails(sb, p);
p.write(getFileName(p)); //writing file to server
}
request.setAttribute("partDetails",sb);
RequestDispatcher view=request.getRequestDispatcher("result.jsp");
view.forward(request, response); //forwarding to JSP
}
private String getFileName(Part p){
String header=p.getHeader("content-disposition");
String filename = header.substring(header.indexOf("filename=\"")).split("\"")[1]; //getting filename
return filename;
}
private void getDetails(StringBuffer sb,Part p){
sb.append("<p></p>");
for(String header:p.getHeaderNames()){
sb.append("<b>"+header+" : </b>");
sb.append(p.getHeader(header)+"</br>");
}
}
}
/* JSP Code */
<html>
<head><title>Upload Details</title></head>
<% StringBuffer result=(StringBuffer)request.getAttribute("partDetails");
%>
<body>
<%= result %>
</body>
</html>
<head><title>Upload Details</title></head>
<% StringBuffer result=(StringBuffer)request.getAttribute("partDetails");
Output as seen in Chrome |
<body>
<%= result %>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------
DOWNLOAD LINKS
--------------------------------------------------------------------------------------------------------------------------
Labels:J2EE | 0
comments
Subscribe to:
Posts
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...