File upload
In general file upload means to receive data from remote system to a local system. So using JSP we can upload file on server by using HTML form tag. An uploaded file could be image file, text file or any document file.
There are many ways to upload the file to the server. One of the way is by MultiPartRequest class and it can be used only when the cos.jar file is attached with the system.
MultiPartRequest class is a utlity class to handle the MultiPart/form data request. There are many constructors defined in the MultiPartRequest class.
If we upload large files like of Mimi=type or content-type then JSP does not process the request. So we should maximise the file size in the file property.To upload single file we use single tag with attribute type="file" and for multiple files we should use more than one tag with different values and name attribute.
Creating file upload form
To upload the file we have to make a form by using HTML tag <form> in JSP and the file name is index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP INDEX</title>
</head>
<body>
<form action="upload.jsp" method="post"enctype="multipart/form-data">
Select File:<input type="file" name="fname"/><br>
<input type="image" src="imgupload.png"/>
</form>
</body>
</html>
Output
Writing backend JSP script
This file name is upload.jsp. And we are uploading the file to the location c:/new, you can change of your choice.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP UPLOAD</title>
</head>
<body>
<%@page import="com.oreilly.servlet.MultiPartRequest" %>
<% MultiPartRequest m=new MultiPartRequest(request,"c:/new");
out.print("Successfully Uploaded");
%>
</body>
</html>
The above code will help to upload your file what you want to and if the file size is more than 1MB than you have to specify in the post size.