0

Please help me,i was tried to find answer on google for whole week but no luck for me. Nothing work with me at all, and this is my problem, i will really appriciate your help.

i want to upload a picture on my host and i used code like this and it worked fine on localhost but not in my real Hosting. This is my code:

 boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (!isMultipart) {
        }
        else {  
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List items = null;

            try {
                items = upload.parseRequest(request);
            } catch (FileUploadException e) {
                e.printStackTrace();
            }
            Iterator iter = items.iterator();



            while (iter.hasNext()) {
                FileItem item = (FileItem) iter.next();
                if (item.isFormField()) {
                    params.put(item.getFieldName(), item.getString());
                } else {
                    try {
                        String itemName = item.getName();
                        filename = rdhash.randomHash() + itemName.substring(
                                itemName.lastIndexOf("\\") + 1);
                        String realPath = getServletContext().getRealPath("/")
                                + "banner\\" + filename;

                        out.println(realPath);
                        File savedFile = new File(realPath);
                        item.write(savedFile);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

as i said, it can not work on my real hosting. I was try to find why it doesn't work and i find out that it can not get the upload path, it likely can't get the upload path because the out.println() code show me like this:

null\banner\[my file name].jpg

but when i try on my localhost it is show whole path to my project like:

C:\[myproject folder]\banner\[my file name].jpg

i don't know why it can't get the realpath on my host. Please help me, my mind is blowing now. My hosting server is Tomcat 7 bros.

----------------------------update--------------------- this is part in my server.xml content.

 <Host name="localhost"  appBase="webapps"
        unpackWARs="true" autoDeploy="true">

    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="localhost_access" suffix=".log" rotatable="false"
           pattern="%h %l %u %t &quot;%r&quot; %s %b" />

  </Host>
   <Host name="[myhost].com" appBase="/Homedata/[hostname]/public_html" unpackWARs="true" deployXML="false">
      <Alias>www.[hostname].com</Alias> 
      <Context path="" reloadable="true" docBase="/Homedata/[hostname]/public_html" debug="1"/>
      <Context path="/manager" debug="0" privileged="true"
          docBase="/usr/local/easy/share/easy-tomcat7/webapps/manager">
      </Context>
   </Host>
  • Is the war file unpacked on your host? – Stefan Feb 04 '15 at 08:52
  • 1
    http://stackoverflow.com/questions/536228/why-does-getrealpath-return-null-when-deployed-with-a-war-file – James Hunt Feb 04 '15 at 08:56
  • @Stefan no stev, i try to deploy it on tomcat manager and it was fine, tomcat show OK status but when i look into my host, it is just a war file on host and nothing more. It is not unpacked and i don't know how to unpack it too, at least tomcat manager is uploaded it on my host but not unpack it :( – user3575259 Feb 04 '15 at 09:04

1 Answers1

0

Add the "unpackWARs" attribute to your Host configuration in Tomcats "server.xml" and set it to true. Might look like this:

<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">

Better: Don't place uploaded files into your apps directory. Once the application is redeployed, the content will be lost. Create an external directory anywhere on the server, and add the complete path to your apps configurtion.

Stefan
  • 12,108
  • 5
  • 47
  • 66
  • so i need to ask the host provider to add "unpackWARs" attribute in server.xml, After that, just need to redeploy the war file from my computer right?. I will try it and report to you, thanks @Stefan – user3575259 Feb 04 '15 at 13:53
  • Stefan: i was try to put attribute like you said and now my host is not working anymore :(. i don't know why but i see in server.xml have two tag, one with name localhost and one with name=[mysitename]. :(, what must i do next :( – user3575259 Feb 04 '15 at 15:27
  • I dont know how you host your application. If your provider does not support unpacking war files (dont think so), you might try my second suggestion. Please update your question with your current server.xml. – Stefan Feb 04 '15 at 16:17
  • where are you @Stefan – user3575259 Feb 06 '15 at 06:14
  • Can you add autoDeploy="true" and remove deployXML="false" from host element? – Stefan Feb 06 '15 at 10:15