Uploading a file to a server using JSP

By: Ramesh Kumar Swarnkar Emailed: 1728 times Printed: 2338 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

<!-- upload.jsp -->
<%@ page import="java.io.*" %>

<%
String contentType = request.getContentType();
System.out.println("Content type is :: " +contentType);
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);
String 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;

FileOutputStream fileOut = new FileOutputStream(saveFile);


//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();

out.println("File saved as " +saveFile);

}
%>


JSP Home | All JSP Tutorials | Latest JSP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(4)


1. View Comment

i m novice for jsp.
i enjoy to test jsp code.
so, I test this upload.jsp using tomcat.
I got good result so thanks all.
but i see it not upload file size 100mb or large file.

if u r able,pls help me.


View Tutorial          By: thanhtunaung at 2011-08-18 07:44:10
2. View Comment

hi
this is helpful, but file store it in my tomcat folder not goes to specific folder.


View Tutorial          By: Mahesh at 2011-11-10 06:52:39
3. View Comment

heyy dis is not working with me .. plz suggest
nothing open when click to upload.jsp


View Tutorial          By: sahil at 2012-10-26 11:03:41
4. View Comment

hi,
thier is an error coming with me when i am uploading file as that
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
at java.lang.String.substring(Unknown Source)
at org.apache.jsp.upload_005fread_005ffile_jsp._jspService(upload_005fread_005ffile_jsp.java:79)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
can u suggest me out of this..


View Tutorial          By: Tarun at 2013-02-17 03:13:21

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Ramesh Kumar Swarnkar
Uploading a file to a server using JSP

More Tutorials in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
JSP Alert Example
JSP CheckBox Example
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
A JSP page that gets properties from a bean
The page Directive in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Tag Libraries in JSP
The Request Object in JSP
The Response Object in JSP
The Out Object in JSP
The Session Object in JSP

More Latest News
Most Viewed Articles (in JSP )
JSP Example to connect to MS SQL database and retrieve records
What are the different scopes in JSP?
JSP CheckBox Example
Sending Email using JSP
JSP Alert Example
The JSP Program running first Time.
Syntax For JSP Declaratives
Tags using in jsp
JSP pages in servlet
Form processing in JSP
JSP Program for display Date
A Simple Servlet Generating Plain Text
HttpClient
Working with Status Bar Messages
Introduction to JSP expression language
Most Emailed Articles (in JSP)
Sending Email using JSP
The Servlet Life Cycle
Select Object
Embedding a QuickTime movie in a JSP
Embedding MP3 audio file in a JSP
Querying Data with the JSTL in JSP
Combining Scriptlets with HTML
JSP Directives
Form processing in JSP
JSP Program for display Date
An Example Using Servlet Initialization and Page Modification Dates
WebClient: Talking to Web Servers Interactively
Checkbox Object
Handling Events
NotifyUtil::java.net.ConnectException: Connection refused: connect