File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes upload file in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "upload file in jsp" Watch "upload file in jsp" New topic
Author

upload file in jsp

santosh billal
Greenhorn

Joined: Dec 15, 2006
Posts: 18
HI any can give me code to upload a file from client and store it
in the server disk in jsp
Rao Raghu
Ranch Hand

Joined: Jan 05, 2007
Posts: 100
You can download File Upload Bean and configure it to your application.



RAGHU<br /> <br />"When the going gets tough, the tough get going"
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

have you seen FAQs:FileUpload in javaranch..


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Naveen Kumar
Ranch Hand

Joined: Dec 14, 2006
Posts: 35
Hi Santosh,

Try the Below Code...

FileUpload.jsp
--------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<TITLE>File Upload</TITLE>
<STYLE>
table {
border:1px solid #000;
border-collapse:collapse;
font-family:arial,sans-serif;
font-size:80%;
}
td,th{
border:1px solid #000;
border-collapse:collapse;
padding:5px;
}
tbody td{
background:#999;
}
tbody th{
text-align:left;
background:#69c;
}
</STYLE>
</HEAD>
<BODY>
<form method="post" ACTION="FileUploadProcess.jsp" name="uploadForm" ENCTYPE='multipart/form-data'>
<table align="center" width="50%" height="25%">
<tbody>
<tr>
<th>File Upload:</th>
<td><input type="file" name="uploadFile" size="40" style="border:1px solid black;"></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" name="Submit" style="border:1px solid black;" value="Submit">
          
<input type="reset" name="Reset" style="border:1px solid black;" value="Reset">
</td>
</tr>
</tbody>
</table>
</form>
</BODY>
</HTML>


----------------------
FileUploadProcess.jsp
----------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page import="java.io.*" %>
<%
String contentType = request.getContentType();
System.out.println("Content type is :: " + contentType);
String imageSave=null;
byte dataBytes[]=null;
String saveFile=null;
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))
{
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
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("\""));
// out.print(dataBytes);
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1, contentType.length());
// out.println(boundary);
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;
try
{
FileOutputStream fileOut = new FileOutputStream("c://"+saveFile);
// fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
imageSave="Success";
}catch (Exception e)
{
System.err.println ("Error writing to file");
imageSave="Failure";
}
}
%>
<html>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<TITLE>File Upload Success/Failure</TITLE>
<STYLE>
table {
border:1px solid #000;
border-collapse:collapse;
font-family:arial,sans-serif;
font-size:80%;
}
td,th{
border:1px solid #000;
border-collapse:collapse;
}
tbody td{
background:#999;
}
tbody th{
text-align:left;
background:#69c;
}
tfoot td{
text-align:center;
font-weight:bold;
background:#FFFFFF;
}
</STYLE>
</HEAD>
<body>
<table align="center" width="300" height="100">
<tbody>
<tr>
<th width="85">Image Name:</th>
<td width="60"><%=saveFile%></td>
</tr>
<tr>
<th width="85">No Of Bytes:</th>
<td width="60"><%=dataBytes.length%></td>
</tr>
<tr>
<th width="85">Image Save:</th>
<td width="60"><%=imageSave%></td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="2">
<a style="color:#FF0000;" href="FileUpload.jsp" title="File Upload">File Upload</a>
  
<a style="color:#FF0000;" href="javascript:window.close();" title="Close Upload Window">
Close Window
</a>
</td>
</tr>
</tfoot>
</table>
</body>
</html>


Regards
kumar
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61006
    
  65

Using a JSP to handle the upload is a very poor practice. Please see the JSP FAQ entry mentioned earlier for proper ways to achieve this.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
Consider Paul's rocket mass heater.
 
subject: upload file in jsp