❮ Previous Next ❯

JSP FileUpload Example

Required Files:

1.Page.jsp:

2.pageupload.jsp:

Page.jsp

<html>  
<head>  
<title>JSP FileUpload Example</title>  
</head>  
<body>  
<Form ENCTYPE="multipart/form-data"ACTION="pageupload.jsp"METHOD=POST>  
<br><br><br>  
<center><table border="2">  
<tr><center><td colspan="2"><p align="center"><B>PROGRAM FOR UPLOADING THE FILE</B><center></td></tr>
<tr><td><b>Choose the file To Upload:</b>
</td>
    <td><INPUT NAME="F1"TYPE="file"></td><tr>
<tr><td colspan="2">
<p align="right"><INPUT TYPE="submit" VALUE="Send File" ></p></td></tr>
<table>
</center>
</FORM>
</body>  
</html>

pageupload.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<html>  
<head>  
<title>JSP File Upload</title>  
</head>  
<body>  
<%
String s = request.getContentType();
if ((s != null) && (s.indexOf("multipart/form-data") >= 0))
{
DataInputStream in = new DataInputStream(request.getInputStream());
int length = request.getContentLength();
byte dataBytes[] = new byte[length];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < length)
{
byteRead = in.read(dataBytes, totalBytesRead,length);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = s.lastIndexOf("=");
String boundary = s.substring(lastIndex + 1,s.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
File f = new File("F:\\"+saveFile);
FileOutputStream fileOut = new FileOutputStream(f);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><Br><table border="2"><tr><<td><b>You have successfully upload the file by the name of:</b>
<% out.println(saveFile);%></td></tr></table>
<%
}
%>

Out Put

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<html>  
<head>  
<title>JSP File Upload</title>  
</head>  
<body>  
<%
String s = request.getContentType();
if ((s != null) && (s.indexOf("multipart/form-data") >= 0))
{
DataInputStream in = new DataInputStream(request.getInputStream());
int length = request.getContentLength();
byte dataBytes[] = new byte[length];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < length)
{
byteRead = in.read(dataBytes, totalBytesRead,length);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = s.lastIndexOf("=");
String boundary = s.substring(lastIndex + 1,s.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
File f = new File("F:\\"+saveFile);
FileOutputStream fileOut = new FileOutputStream(f);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><Br><table border="2"><tr><<td><b>You have successfully upload the file by the name of:</b>
<% out.println(saveFile);%></td></tr></table>
<%
}
%>
</body>  
</html>


javaserver page
❮ Previous Next ❯