How To Upload Files Using JavaServer Pages

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

How to upload files using JavaServer Pages

Uploading files to a web server in JavaServer pages proves to be sometimes technical. This is why I
came up with these simple JavaServer pages in order to achieve this. There is a Windows and a Linux
version, though the procedures involved are more or less the same. The Windows version was tested on
a Microsoft Windows 2000 Professional system whereas the Linux companion was done on Red Hat 9.0.
The web server of service was Apache Tomcat 5.5 in both cases.

This is the HTML upload page


Code:

<html>
<head>
<title>File Upload in JSP</title>
</head>
<body>
<FORM ACTION="upload.jsp" ENCTYPE="multipart/form-data" METHOD=POST>
Which file do you want to upload? <INPUT TYPE=FILE NAME=file> <BR>
<INPUT TYPE=SUBMIT>
</FORM>
</body>
</html>

This is the JavaServer page, Linux version


Code:

<!-- upload.jsp -->


<%@ page import="java.io.*" %>
<%
String contentType = request.getContentType();
String file = "";
String saveFile = "";
FileOutputStream fileOut = null;
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))
{
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength)
{
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}

try
{
file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("/") +
1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex +
1,contentType.length());
int pos;
pos = file.indexOf("filename=/" + 1);
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;
String folder = "/tmp/uploads/";
fileOut = new FileOutputStream(folder + saveFile);
fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
}
catch(Exception e)
{
out.print(e);
}
finally
{
try{fileOut.close();}catch(Exception err){}
}
}
%>

This is the JavaServer page, Windows version


Code:

<%@ page import="java.io.*" %>


<%
String saveFile = "";
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))
{
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength)
{
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") +
1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.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;
String folder = "C:/Program Files/Apache Software Foundation/Tomcat
5.5/webapps/upload/images/";
FileOutputStream fileOut = new FileOutputStream(folder + saveFile);
//out.print("Saved here: " + saveFile);
//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
out.println("Uploading your file. Please wait...");
}
%>

Please note that this is not a final thing and is subject to revision. Feel free to do so.

Thank you

You might also like