> i m not able to store the file in a particular directory on disk
Particular directory? Just define it in the File object.
File file = new File("/path/to/file", "filename.ext");
// or if you are sure that slashes are OK.
File file = new File("/path/to/file/filename.ext");
> is it possible to store files of any size and type.?
It just depends on authorization and the filesystem used. As long as the JVM has access to the specified location and the size does not exceed the limitations of the filesystem, you can store any kind of byte on the given location. The type and name doesn't matter.
i m using the following code:
index.jsp
<html>
<head>
<title>File Post</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<form action="post.jsp" method="post"
enctype="multipart/form-data" name="form1" id="form1">
<tr>
<td width="25%"><strong>File</strong></td>
<td><input type="file" name="file" /></td>
</tr>
<tr>
<td width="25%"> </td>
<td><input type="submit" name="Submit" value="Upload" /></td>
</tr>
</form>
</body>
</html>
post.jsp
<%@ page import="org.apache.commons.fileupload" %>
<%@ page import="org.apache.*"%>
<%@ page import="java.io.File"%>
<%@ page import="java.util.*"%>
<%@ page import="javax.servlet.http.*"%>
<%
boolean isMultipart=ServletFileUpload.isMultipartContent(request);
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(100000);
factory.setRepository("/");
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Set overall request size constraint
upload.setSizeMax(100000);
// Parse the request
List items = upload.parseRequest(request);
File uploadedFile = new File("C://Documents and Settings/trainee/Desktop/xyz");
item.write(uploadedFile);
%>
and i m getting following error:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP
Generated servlet error:
C:\Documents and Settings\trainee\.netbeans\5.5\apache-tomcat-5.5.17_base\work\Catalina\localhost\FileUpload\org\apache\jsp\post_jsp.java:6: package org.apache.commons does not exist
import org.apache.commons.fileupload;
^
An error occurred at line: 7 in the jsp file: /post.jsp
Generated servlet error:
C:\Documents and Settings\trainee\.netbeans\5.5\apache-tomcat-5.5.17_base\work\Catalina\localhost\FileUpload\org\apache\jsp\post_jsp.java:55: cannot find symbol
symbol : variable ServletFileUpload
location: class org.apache.jsp.post_jsp
boolean isMultipart=ServletFileUpload.isMultipartContent(request);
^
An error occurred at line: 7 in the jsp file: /post.jsp
Generated servlet error:
C:\Documents and Settings\trainee\.netbeans\5.5\apache-tomcat-5.5.17_base\work\Catalina\localhost\FileUpload\org\apache\jsp\post_jsp.java:57: cannot find symbol
symbol : class DiskFileItemFactory
location: class org.apache.jsp.post_jsp
DiskFileItemFactory factory = new DiskFileItemFactory();
^
An error occurred at line: 7 in the jsp file: /post.jsp
Generated servlet error:
C:\Documents and Settings\trainee\.netbeans\5.5\apache-tomcat-5.5.17_base\work\Catalina\localhost\FileUpload\org\apache\jsp\post_jsp.java:57: cannot find symbol
symbol : class DiskFileItemFactory
location: class org.apache.jsp.post_jsp
DiskFileItemFactory factory = new DiskFileItemFactory();
^
An error occurred at line: 7 in the jsp file: /post.jsp
Generated servlet error:
C:\Documents and Settings\trainee\.netbeans\5.5\apache-tomcat-5.5.17_base\work\Catalina\localhost\FileUpload\org\apache\jsp\post_jsp.java:64: cannot find symbol
symbol : class ServletFileUpload
location: class org.apache.jsp.post_jsp
ServletFileUpload upload = new ServletFileUpload(factory);
^
An error occurred at line: 7 in the jsp file: /post.jsp
Generated servlet error:
C:\Documents and Settings\trainee\.netbeans\5.5\apache-tomcat-5.5.17_base\work\Catalina\localhost\FileUpload\org\apache\jsp\post_jsp.java:64: cannot find symbol
symbol : class ServletFileUpload
location: class org.apache.jsp.post_jsp
ServletFileUpload upload = new ServletFileUpload(factory);
^
An error occurred at line: 7 in the jsp file: /post.jsp
Generated servlet error:
C:\Documents and Settings\trainee\.netbeans\5.5\apache-tomcat-5.5.17_base\work\Catalina\localhost\FileUpload\org\apache\jsp\post_jsp.java:73: cannot find symbol
symbol : variable item
location: class org.apache.jsp.post_jsp
item.write(uploadedFile);
^
7 errors
thanks