Link to home
Start Free TrialLog in
Avatar of basirana
basirana

asked on

File upload path

Hi

Below program is used for file upload. The "File savedFile = new File(getServletContext().getRealPath("/"), fullFile.getName());"  lines in the code tell where we can save the file.
Problem is file is getting uploaded to temp folder.
I need to change the line so that file will be uploaded to c:\. Can you please help me to change code.

<%@ page import="org.apache.commons.fileupload.*, java.util.List, java.io.File, java.util.Iterator" %>
<%
      
      // first check if the upload request coming in is a multipart request
      boolean isMultipart = FileUpload.isMultipartContent(request);
      
      // if not, send to message page with the error message
      if(!isMultipart){
            request.setAttribute("msg", "Request was not multipart!");
            request.getRequestDispatcher("msg.jsp").forward(request, response);
            return;
      }
      
      // now lets create a handler for the upload request.
      DiskFileUpload upload = new DiskFileUpload();
      
      // parse this request by the handler
      // this gives us a list of items from the request
      List items = upload.parseRequest(request);
      
      // now iterate over this list
      Iterator itr = items.iterator();
      
      while(itr.hasNext()){
      
            FileItem item = (FileItem) itr.next();
            
            // check if the current item is a form field or an uploaded file
            if(item.isFormField()){
                  
                  // get the name of the field
                  String fieldName = item.getFieldName();
                  
                  // if it is name, we can set it in request to thank the user
                  if(fieldName.equals("name"))
                        request.setAttribute("msg", "Thank You: " + item.getString());
                        
            } else {
            
                  // the item must be an uploaded file
                  // save it to disk                  
                  File fullFile = new File(item.getName());                  
                  File savedFile = new File(getServletContext().getRealPath("/"), fullFile.getName());
                  item.write(savedFile);
            }
      }
      
      // finally send to the msg page
      request.getRequestDispatcher("msg.jsp").forward(request, response);

      
%>
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of basirana
basirana

ASKER

I have change the line but I am getting error as below.

type Status report

message C:\jboss-4.0.4.GA\server\default\.\tmp\deploy\tmp17003AppWeb-exp.war (Access is denied)

description The requested resource (C:\jboss-4.0.4.GA\server\default\.\tmp\deploy\tmp17003AppWeb-exp.war (Access is denied)) is not available.

Any idea whats wrong. Please let me know if you need other files used in file uploading.
I don't use jboss. Maybe there is a user listening ?
Anyway, let's go back. Does the code you posted first work ? Does it put the file in your web app's root folder ?
The code I posted works. I have three files but the above code is used for uploading. The bolow 2 codes are 1. is user interface for selecting file in Multipart form. 2. is output page displayed after file gets uploaded.

fileuploaddemo.htm

<!-- saved from url=(0022)http://internet.e-mail -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>Upload Test</title>
  <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body>

      <form name="myform" action="fileuploaddemo.jsp" method="post" enctype="multipart/form-data">
            Specify your name:<br/>
                  <input type="text" name="name" size="15"/><br/>
            Specify your Image:<br/>
                  <input type="file" name="myimage"><br/>
            Specify your File:<br/>
                  <input type="file" name="myfile"><br/><br/>

            <input type="submit" name="Submit" value="Submit your files"/>
      </form>

</body>
</html>

msg.jsp

<%
 
  String msg = (String)request.getAttribute("msg");
 
  if(msg != null)
        out.println("<font size=+1>" + msg + "</font><br/>");
%>

Click <a href="fileuploaddemo.htm">here</a> to go to the upload page.

Please let me know if you need anything more
>Click <a href="fileuploaddemo.htm">here</a> to go to the upload page.
What is the file path to  fileuploaddemo.htm  ?
Please show what these print.
<%=request.getContextPath()%>
<%=application.getRealPath("/")%>
when I try to run in WSAD I am getting below error.

*** Starting the server ***
************ Start Display Current Environment ************
WebSphere Platform 5.1 [BASE 5.1.0 b0344.02]  running with process name localhost\localhost\server1 and process id 1340
Host Operating System is Windows XP, version 5.1
Java version = J2RE 1.4.1 IBM Windows 32 build cn1411-20031011 (JIT enabled: jitc), Java Compiler = jitc, Java VM name = Classic VM
was.install.root = C:\Program Files\IBM\WebSphere Studio\Application Developer\v5.1.1\runtimes\base_v51
user.install.root = C:\Program Files\IBM\WebSphere Studio\Application Developer\v5.1.1\runtimes\base_v51
Java Home = C:\Program Files\IBM\WebSphere Studio\Application Developer\v5.1.1\runtimes\base_v51\java\jre
ws.ext.dirs = C:\Program Files\IBM\WebSphere Studio\Application Developer\v5.1.1\runtimes\base_v51/java/lib;C:\Program Files\IBM\WebSphere Studio\Application Developer\v5.1.1\runtimes\base_v51/classes;C:\Program Files\IBM\WebSphere Studio\Application Developer\v5.1.1\runtimes\base_v51/classes;C:\Program Files\IBM\WebSphere Studio\Application Developer\v5.1.1\runtimes\base_v51/lib;C:\Program Files\IBM\WebSphere Studio\Application Developer\v5.1.1\runtimes\base_v51/lib/ext;C:\Program Files\IBM\WebSphere Studio\Application Developer\v5.1.1\runtimes\base_v51/web/help;C:\Program Files\IBM\WebSphere Studio\Application Developer\v5.1.1\runtimes\base_v51/deploytool/itp/plugins/com.ibm.etools.ejbdeploy/runtime;C:/Program Files/IBM/WebSphere Studio/Application Developer/v5.1.1/wstools/eclipse/plugins/com.ibm.etools.webservice_5.1.1/runtime/worf.jar
Classpath = C:\Program Files\IBM\WebSphere Studio\Application Developer\v5.1.1\runtimes\base_v51/properties;C:\Program Files\IBM\WebSphere Studio\Application Developer\v5.1.1\runtimes\base_v51/properties;C:\Program Files\IBM\WebSphere Studio\Application Developer\v5.1.1\runtimes\base_v51/lib/bootstrap.jar;C:\Program Files\IBM\WebSphere Studio\Application Developer\v5.1.1\runtimes\base_v51/lib/j2ee.jar;C:\Program Files\IBM\WebSphere Studio\Application Developer\v5.1.1\runtimes\base_v51/lib/lmproxy.jar;C:\Program Files\IBM\WebSphere Studio\Application Developer\v5.1.1\runtimes\base_v51/lib/urlprotocols.jar;C:/Program Files/IBM/WebSphere Studio/Application Developer/v5.1.1/wstools/eclipse/plugins/com.ibm.etools.websphere.tools.common_5.1.1/runtime/wteServers.jar;C:/Program Files/IBM/WebSphere Studio/Application Developer/v5.1.1/wstools/eclipse/plugins/com.ibm.etools.websphere.tools.common_5.1.1/runtime/wasToolsCommon.jar
Java Library path = C:\Program Files\IBM\WebSphere Studio\Application Developer\v5.1.1\runtimes\base_v51/bin;C:\Program Files\IBM\WebSphere Studio\Application Developer\v5.1.1\runtimes\base_v51/java/bin;C:\Program Files\IBM\WebSphere Studio\Application Developer\v5.1.1\runtimes\base_v51/java/jre/bin;C:\Program Files\IBM\WebSphere Studio\Application Developer\v5.1.1\eclipse\jre\bin;.;C:\WINNT\system32;C:\WINNT;c:\Perl\bin;c:\oracle\products\9.2.0.1\bin;C:\Program Files\Oracle\jre\1.1.8\bin;C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;C:\WINNT\system32\nls;C:\WINNT\system32\nls\ENGLISH;C:\Program Files\Novell\ZENworks\;C:\j2sdk1.4.2_11\jre\bin;C:\ant\ant\bin;C:\cygwin\bin;T:.;W:.;Z:.
************* End Display Current Environment *************
[10/19/06 9:43:12:458 EDT] 3d197bdd ManagerAdmin  I TRAS0017I: The startup trace state is *=all=disabled.
[10/19/06 9:43:13:833 EDT] 3d197bdd AdminInitiali A ADMN0015I: AdminService initialized
[10/19/06 9:43:15:286 EDT] 3d197bdd Configuration A SECJ0215I: Successfully set JAAS login provider configuration class to com.ibm.ws.security.auth.login.Configuration.
[10/19/06 9:43:15:380 EDT] 3d197bdd SecurityDM    I SECJ0231I: The Security component's FFDC Diagnostic Module com.ibm.ws.security.core.SecurityDM registered successfully: true.
[10/19/06 9:43:15:723 EDT] 3d197bdd SecurityCompo I SECJ0309I: Java 2 Security is disabled.
[10/19/06 9:43:15:739 EDT] 3d197bdd SecurityCompo I SECJ0212I: WCCM JAAS configuration information successfully pushed to login provider class.
[10/19/06 9:43:15:770 EDT] 3d197bdd SecurityCompo I SECJ0240I: Security service initialization completed successfully
[10/19/06 9:43:15:770 EDT] 3d197bdd JMSRegistrati A MSGS0602I: WebSphere Embedded Messaging Client only has been installed
[10/19/06 9:43:23:676 EDT] 3d197bdd CacheServiceI I DYNA0048I: WebSphere Dynamic Cache initialized successfully.
[10/19/06 9:43:27:286 EDT] 3d197bdd JMXSoapAdapte A ADMC0013I: SOAP connector available at port 8880
[10/19/06 9:43:27:317 EDT] 3d197bdd SecurityCompo I SECJ0243I: Security service started successfully
[10/19/06 9:43:27:317 EDT] 3d197bdd SecurityCompo I SECJ0210I: Security enabled false
[10/19/06 9:43:28:880 EDT] 3d197bdd ApplicationMg A WSVR0200I: Starting application: IBMUTC
[10/19/06 9:43:29:973 EDT] 3d197bdd WebContainer  A SRVE0161I: IBM WebSphere Application Server - Web Container.  Copyright IBM Corp. 1998-2002
[10/19/06 9:43:30:005 EDT] 3d197bdd WebContainer  A SRVE0162I: Servlet Specification Level: 2.3
[10/19/06 9:43:30:020 EDT] 3d197bdd WebContainer  A SRVE0163I: Supported JSP Specification Level: 1.2
[10/19/06 9:43:30:411 EDT] 3d197bdd WebContainer  A SRVE0169I: Loading Web Module: IBM Universal Test Client.
[10/19/06 9:43:31:005 EDT] 3d197bdd WebGroup      I SRVE0180I: [IBM Universal Test Client] [/UTC] [Servlet.LOG]: JSP 1.2 Processor: init
[10/19/06 9:43:31:161 EDT] 3d197bdd WebGroup      I SRVE0180I: [IBM Universal Test Client] [/UTC] [Servlet.LOG]: SimpleFileServlet: init
[10/19/06 9:43:31:302 EDT] 3d197bdd ApplicationMg A WSVR0221I: Application started: IBMUTC
[10/19/06 9:43:31:302 EDT] 3d197bdd ApplicationMg A WSVR0200I: Starting application: fasApp
[10/19/06 9:43:31:333 EDT] 3d197bdd ApplicationMg E WSVR0101W: An error occurred starting, fasApp
[10/19/06 9:43:31:348 EDT] 3d197bdd ApplicationMg A WSVR0217I: Stopping application: fasApp
[10/19/06 9:43:31:458 EDT] 3d197bdd ApplicationMg A WSVR0220I: Application stopped: fasApp
[10/19/06 9:43:31:552 EDT] 3d197bdd ApplicationMg A WSVR0217I: Stopping application: IBMUTC
[10/19/06 9:43:31:645 EDT] 3d197bdd WebContainer  A SRVE0170I: Stopping Web Module: IBM Universal Test Client.
[10/19/06 9:43:31:661 EDT] 3d197bdd WebGroup      I SRVE0180I: [IBM Universal Test Client] [/UTC] [Servlet.LOG]: SimpleFileServlet: destroy
[10/19/06 9:43:31:661 EDT] 3d197bdd WebGroup      I SRVE0180I: [IBM Universal Test Client] [/UTC] [Servlet.LOG]: JSP 1.2 Processor:  destroy
[10/19/06 9:43:31:692 EDT] 3d197bdd ApplicationMg A WSVR0220I: Application stopped: IBMUTC
[10/19/06 9:43:32:036 EDT] 3d197bdd WsServer      E WSVR0003E: Server server1 failed to start
com.ibm.ws.exception.RuntimeError
      at com.ibm.ws.runtime.component.ApplicationMgrImpl.startApplication(ApplicationMgrImpl.java:285)
      at com.ibm.ws.runtime.component.ApplicationMgrImpl.start(ApplicationMgrImpl.java:249)
      at com.ibm.ws.runtime.component.ContainerImpl.startComponents(ContainerImpl.java:536)
      at com.ibm.ws.runtime.component.ContainerImpl.start(ContainerImpl.java:413)
      at com.ibm.ws.runtime.component.ApplicationServerImpl.start(ApplicationServerImpl.java:125)
      at com.ibm.ws.runtime.component.ContainerImpl.startComponents(ContainerImpl.java:536)
      at com.ibm.ws.runtime.component.ContainerImpl.start(ContainerImpl.java:413)
      at com.ibm.ws.runtime.component.ServerImpl.start(ServerImpl.java:183)
      at com.ibm.ws.runtime.WsServer.start(WsServer.java:128)
      at com.ibm.ws.runtime.WsServer.main(WsServer.java:225)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:79)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:41)
      at java.lang.reflect.Method.invoke(Method.java:386)
      at com.ibm.ws.bootstrap.WSLauncher.main(WSLauncher.java:94)
      at com.ibm.etools.websphere.tools.runner.api.ServerRunnerV5$1.run(ServerRunnerV5.java:97)
Caused by: com.ibm.etools.archive.exception.NoModuleFileException: A file does not exist for module element having uri: fasAppApplicationClient.jar
      at com.ibm.etools.commonarchive.impl.ModuleRefImpl.checkType(ModuleRefImpl.java:715)
      at com.ibm.etools.commonarchive.impl.ModuleRefImpl.initModuleFileFromEAR(ModuleRefImpl.java:270)
      at com.ibm.etools.commonarchive.impl.ModuleRefImpl.getModuleFile(ModuleRefImpl.java:247)
      at com.ibm.ws.classloader.ClassGraph.addModule(ClassGraph.java:101)
      at com.ibm.ws.classloader.ClassLoaderManager.initialize(ClassLoaderManager.java:126)
      at com.ibm.ws.classloader.ClassLoaderManager.<init>(ClassLoaderManager.java:103)
      at com.ibm.ws.runtime.component.DeployedApplicationImpl.start(DeployedApplicationImpl.java:529)
      at com.ibm.ws.runtime.component.ApplicationMgrImpl.startApplication(ApplicationMgrImpl.java:271)
      ... 15 more
---- Begin backtrace for nested exception
com.ibm.etools.archive.exception.NoModuleFileException: A file does not exist for module element having uri: fasAppApplicationClient.jar
      at com.ibm.etools.commonarchive.impl.ModuleRefImpl.checkType(ModuleRefImpl.java:715)
      at com.ibm.etools.commonarchive.impl.ModuleRefImpl.initModuleFileFromEAR(ModuleRefImpl.java:270)
      at com.ibm.etools.commonarchive.impl.ModuleRefImpl.getModuleFile(ModuleRefImpl.java:247)
      at com.ibm.ws.classloader.ClassGraph.addModule(ClassGraph.java:101)
      at com.ibm.ws.classloader.ClassLoaderManager.initialize(ClassLoaderManager.java:126)
      at com.ibm.ws.classloader.ClassLoaderManager.<init>(ClassLoaderManager.java:103)
      at com.ibm.ws.runtime.component.DeployedApplicationImpl.start(DeployedApplicationImpl.java:529)
      at com.ibm.ws.runtime.component.ApplicationMgrImpl.startApplication(ApplicationMgrImpl.java:271)
      at com.ibm.ws.runtime.component.ApplicationMgrImpl.start(ApplicationMgrImpl.java:249)
      at com.ibm.ws.runtime.component.ContainerImpl.startComponents(ContainerImpl.java:536)
      at com.ibm.ws.runtime.component.ContainerImpl.start(ContainerImpl.java:413)
      at com.ibm.ws.runtime.component.ApplicationServerImpl.start(ApplicationServerImpl.java:125)
      at com.ibm.ws.runtime.component.ContainerImpl.startComponents(ContainerImpl.java:536)
      at com.ibm.ws.runtime.component.ContainerImpl.start(ContainerImpl.java:413)
      at com.ibm.ws.runtime.component.ServerImpl.start(ServerImpl.java:183)
      at com.ibm.ws.runtime.WsServer.start(WsServer.java:128)
      at com.ibm.ws.runtime.WsServer.main(WsServer.java:225)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:79)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:41)
      at java.lang.reflect.Method.invoke(Method.java:386)
      at com.ibm.ws.bootstrap.WSLauncher.main(WSLauncher.java:94)
      at com.ibm.etools.websphere.tools.runner.api.ServerRunnerV5$1.run(ServerRunnerV5.java:97)

[10/19/06 9:43:32:098 EDT] 3d197bdd WsServer      E WSVR0009E: Error occurred during startup

Sorry, I don't use WSAD. I can't help you with that.