Sivakumar Sankarasubramanian wrote:
>
> Hi ,
>
> I'm presently working on MultipartRequest in my JSP page. I'm able to
> retrieve the parameter values and also the File and save it in the default
> directory. The only problem is that I want it to save in a particular
> directory which i specify. Can somebody throw light on this, with snippets.
>
> My code goes like this
>
> -----------
> <%@ page language="java" import="java.util.Vector, proposal.*,
> com.oreilly.servlet.*;"%>
> <jsp:useBean id="prop" class ="proposal.ProposalItemBean" scope="session"/>
> <%
>
> MultipartRequest multi = new MultipartRequest(request, ".",5
> * 1024 * 1024 ) ;
>
> prop.propUser.setFname(multi.getParameter("FirstName")) ;
> prop.propUser.setLname(multi.getParameter("LastName")) ;
> prop.propUser.setAdd1(multi.getParameter("Add1")) ;
> prop.propUser.setAdd2(multi.getParameter("Add2")) ;
> prop.propUser.setCity(multi.getParameter("City")) ;
> prop.propUser.setState(multi.getParameter("State")) ;
> prop.propUser.setCountry(multi.getParameter("Country")) ;
> prop.propUser.setZip(multi.getParameter("Zip")) ;
> prop.propUser.setPhone(multi.getParameter("Phone")) ;
> prop.propUser.setFax(multi.getParameter("Fax")) ;
> prop.propUser.setEmail(multi.getParameter("Email")) ;
> prop.propUser.setUrl(multi.getParameter("Url"));
> prop.propUser.setExtn(multi.getParameter("Extn"));
> prop.propUser.setZip4(multi.getParameter("Zip4")) ;
> %>
>
> This automatically saves the file/files in the default root directory.
> "." is the root and 5*1024*1024 indicates the max size of file.
Well, as you say, "." tells MultipartRequest to save the file in the
current directory, which can be pretty much any directory depending on
which servlet container you're running and how it was started. If you
want it saved somewhere else, replace "." with the absolute path for
the directory where it should be saved.
Typically you upload to a temporary directory and then move the file
to its real location after you have checked that "it's okay" (whatever that
means for you application). You probably don't want to let your users
specify the directory name, since they could them upload files to any
directory your servlet container has write access to. Even though you
can do all of this in a JSP page, I still suggest that you use a servlet.
You can use an init param to specify both the temporary directory name
and the name of final file destination, let it do whatever security
checks you need, create and initialize your ProposalItemBean, and
forward to a JSP page for displaying a confirmation page (or whatever
you do after successful upload). Putting all this code in a JSP page
makes it harder to debug and maintain.
Hans
--
Hans Bergsten [EMAIL PROTECTED]
Gefion Software http://www.gefionsoftware.com
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets