aspose file tools*
The moose likes JSP and the fly likes File Upload Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "File Upload" Watch "File Upload" New topic
Author

File Upload

N Nanda
Ranch Hand

Joined: Feb 09, 2004
Posts: 62
Hi all...
I am working on a project namely Web Based Content Distribution System and I am doing it in JSP. I need to put a file upload utility in my development. Can any1 tell me how I can do that in JSP ? Its an urgency.
Thanks in advance.
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
You can use APIs available on internet. Following are the URLs. You can use any of them.
http://www.oop-reserch.com/mime_prop.html
http://www.javazoom.net/jzservlets/uploadbean/uploadbean.html
http://www.servlets.com/cos/index.html
http://www.objectsdevelopment.com/portal/modules/mydownloads/singlefile.php?cid=50&lid=1
http://jakarta.apache.org/commons/fileupload/index.html
Rajaguru Ramalingam
Greenhorn

Joined: Apr 12, 2004
Posts: 11
<%@ page errorPage="error.jsp" %>
<jsp:useBean id="prop" scope="page" class="java.util.Properties" />
<%@ page import="java.io.*,java.util.*,javax.servlet.*" %>
<%!
public String getBoundary(HttpServletRequest request,Properties prop) throws ServletException,IOException{
String boundary = null;
Enumeration enum = request.getHeaderNames();
while(enum.hasMoreElements()){
String header = (String)enum.nextElement();
String hvalue = request.getHeader(header);
prop.setProperty((header).toLowerCase(),hvalue);
if( "content-type".equalsIgnoreCase(header) ){
int idx = hvalue.lastIndexOf("boundary=");
if( idx != -1 ){
boundary= hvalue.substring( idx+9 , hvalue.length());
}
}
}
return boundary;
}
public String getFileName(String secondline){
int len = secondline.length();
int idx = secondline.lastIndexOf("filename=");
if( idx == -1 ) return null;
String filename = secondline.substring( idx+10 , len-1);
filename = "null"; //filename.replace('\\','/');
idx = filename.lastIndexOf("/");
idx = idx + 1;
filename = filename.substring( idx );
return filename;
}
%>
<%
String DPATH = "C:\\temp\\file\\";
int ROUGHSIZE=1024;
int MAXSIZE = 10; // Mega Byte
String boundary = getBoundary(request,prop);
if( boundary == null ){
boundary = prop.getProperty("boundary");// This statement line for HOTJAVABROWSER 3.0
}else{
boundary = "--"+boundary;
}
if( boundary == null ){
throw new Exception("BNF");
}
Long contentsize = new Long(prop.getProperty("content-length","0"));
int c;
StringWriter st = new StringWriter();
if( contentsize.longValue() < 1L ){
throw new Exception("ZERO");
}
long l = contentsize.longValue() - ROUGHSIZE;
/*
You can restrict upload file size in the following way
1. after uploaded u cannt find out file size then display error message
2. from contentsize it will give approximate size of file in bytes.
based on this u can restrict uploading.
*/
int KB = 1024;
int MB = 1024 * KB;
int csize = (int)(l / MB);
if( csize > MAXSIZE ){
throw new Exception("EL");
}
ServletInputStream fin = request.getInputStream();
//Getting Start Boundary Line
int cn;
int count=0;
while( (c=fin.read()) != -1 ){
if( c == '\r') break;
st.write(c);
count++;
}
c=fin.read();//read new line
String tboundary = st.getBuffer().toString();
tboundary=tboundary.trim();
if( ! tboundary.equalsIgnoreCase( boundary) ){
throw new Exception("BBNF");
}
st.close();
st = null;
st = new StringWriter();
//Getting File Information
while( (c=fin.read()) != -1 ){
if( c == '\r' ) break;
st.write(c);
}
c=fin.read();//read new line
String secondline = st.getBuffer().toString();
String filename = getFileName(secondline);
st.close();
st = null;
st = new StringWriter();
//Read Content Type of File
while( (c=fin.read()) != -1 ){
if( c == '\r' ) break;
st.write( c );
}
c=fin.read();//read new line
//out.println( st.getBuffer().toString() );
//Read \r and \n character
fin.read();
fin.read();
File newfile = null;
FileOutputStream fout =null;
try{
if( filename == null ) throw new FileNotFoundException("File Name not found");
newfile = new File(DPATH+filename);
fout = new FileOutputStream( newfile );
}catch(FileNotFoundException fnexp){
fin.close();
throw new Exception("FNF");
}
byte b[] = null;
while( l > 1024L ){
b = new byte[1024];
fin.read(b,0,1024);
fout.write(b);
b=null;
l -= 1024L;
}
if( l > 0 ){
b = new byte[(int)l];
fin.read(b,0,(int)l);
fout.write(b);
}

ByteArrayOutputStream baos = new ByteArrayOutputStream();
while( (c = fin.read()) != -1 ){
baos.write(c);
}
String laststring = baos.toString();
int idx = laststring.indexOf(boundary);
b = baos.toByteArray();
if( idx > 2 ){
fout.write(b,0,idx-2);
}else
{
fout.close();
newfile.delete();
throw new Exception("EBNF");
}
fout.flush();
fout.close();
fin.close();
%>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File Upload