But starting from Servlet Specification 3.0 you dont need external library instead there is a native support. I wanted to try this so i followed these steps
- First i did create a index.jsp file like this
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page
language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>index</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<form action="hellofileupload" method="post" enctype="multipart/form-data" >
<table>
<tr>
<td>File</td>
<td><input type="file" name="samplefile" /></td>
</tr>
<tr><td><input type="submit"></td></tr>
</table>
</form>
</body>
</html>
If you want to upload a file to the server you will have to set form encoding type tomultipart/form-data
. The index.jsp will submit form to thehellofileupload
url and thats where ourHelloFileUploadAnnotationServlet
will come in - Next create a
HelloFileUploadAnnotationServlet
like this
package com.webspherenotes.j2ee6.servlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.Iterator;
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;
/**
* Servlet implementation class HelloFileUploadAnnotationServlet
*/
@WebServlet("/hellofileupload")
@MultipartConfig
public class HelloFileUploadAnnotationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Entering HelloFileUploadAnnotationServlet.doPost()");
response.setContentType("text/html");
response.getWriter().println("Hello from FileUploadServlet");
CollectionfileParts = request.getParts();
IteratorfilePartIt = fileParts.iterator();
while(filePartIt.hasNext()){
Part filePart = filePartIt.next();
System.out.println("File Name " + filePart.getName());
System.out.println("File Size " + filePart.getSize());
System.out.println("File Content ");
BufferedReader fileReader =
new BufferedReader(new InputStreamReader(filePart.getInputStream()));
String line = null;
while(( line = fileReader.readLine()) != null){
System.out.println(line);
}
}
System.out.println("Exiting HelloFileUploadAnnotationServlet.doPost()");
}
}
In order to use the file upload first you will have to mark the Servlet usingMultipartConfig
annotation, indicating that instances of the Servlet expect requests that conform to themultipart/form-data
MIME type.
Then inside the doPost() method you can use the method of HttpServletRequest to get access to the uploaded file.
Thanks for info
ReplyDeleteWeb Design Company in Bangalore
Website development in Bangalore