简体   繁体   中英

Upload files inside server webcontent/upload folder in Struts

I want to upload .wav files inside Apache Tomcat server, inside Webcontent/upload folder.

How to get path from server, so that I will put that path inside my program for uploading .wav files using Struts framework?

i would suggest you declare use the configuration/property file and mention the path there instead of hardcoding it in java file, then read it from there. The advantage is that even if decide to change it , you dont have to compile it again. You can just restart the server.

Just for information ,you can also use FileUpload utility provided by struts 2 as interceptor. At least have a look on this. May be you find it usefule.

For uploading file struts provide a default interceptor names fileupload interceptor. You just need to add this in your struts.xml file . like this one.

<action name="clientlogin"  class="action.client.Clientaction" >

     <interceptor-ref name="fileUpload">
        <param name="maximumSize">2097152</param>
        <param name="allowedTypes">
            image/png,image/gif,image/jpeg,image/pjpeg
        </param>
      </interceptor-ref>
     <result name ="success">/Registration/clientsuccess.jsp  </result>
 </action>

and in action class you can save uploaded file by this code

          String filePath ="your location where you wan to save uploadedfile";
         System.out.println("Server path:" + filePath);
         File fileToCreate = new File(filePath, modelobj.getImagefileFileName());

         FileUtils.copyFile(modelobj.getImagefile(), fileToCreate);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM