简体   繁体   English

在Struts的服务器webcontent / upload文件夹中上载文件

[英]Upload files inside server webcontent/upload folder in Struts

I want to upload .wav files inside Apache Tomcat server, inside Webcontent/upload folder. 我想在Webcontent/upload文件夹中的Apache Tomcat服务器中上传.wav文件。

How to get path from server, so that I will put that path inside my program for uploading .wav files using Struts framework? 如何从服务器获取路径,以便将该路径放入程序中,以便使用Struts框架上载.wav文件?

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. 我建议您声明使用配置/属性文件,并在其中提及路径,而不是在Java文件中对其进行硬编码,然后从那里读取它。 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. 仅作为参考,您还可以使用struts 2提供的FileUpload实用程序作为拦截器。 At least have a look on this. 至少看看这个。 May be you find it usefule. 也许您觉得它有用。

For uploading file struts provide a default interceptor names fileupload interceptor. 为了上传文件strut,请提供默认的拦截器名称fileupload拦截器。 You just need to add this in your struts.xml file . 您只需要在struts.xml文件中添加它即可。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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