Java 在Struts中的服务器webcontent/Upload文件夹中上载文件
我想上传Apache Tomcat服务器中的.wav文件,在Java 在Struts中的服务器webcontent/Upload文件夹中上载文件,java,jakarta-ee,file-upload,struts2,Java,Jakarta Ee,File Upload,Struts2,我想上传Apache Tomcat服务器中的.wav文件,在Webcontent/upload文件夹中 如何从服务器获取路径,以便我将该路径放入我的程序中,以便使用Struts框架上载.wav文件?我建议您声明使用配置/属性文件,并在其中提及路径,而不是在java文件中硬编码,然后从中读取。优点是,即使决定更改它,也不必再次编译它。您可以重新启动服务器 仅供参考,您还可以使用struts 2提供的FileUpload实用程序作为拦截器。至少看看这个。您可能会发现它很有用。对于上载文件,strut
Webcontent/upload
文件夹中
如何从服务器获取路径,以便我将该路径放入我的程序中,以便使用Struts框架上载.wav文件?我建议您声明使用配置/属性文件,并在其中提及路径,而不是在java文件中硬编码,然后从中读取。优点是,即使决定更改它,也不必再次编译它。您可以重新启动服务器
仅供参考,您还可以使用struts 2提供的FileUpload实用程序作为拦截器。至少看看这个。您可能会发现它很有用。对于上载文件,struts提供了一个默认的拦截器名称
fileupload
拦截器。
您只需要将其添加到struts.xml
文件中。
像这个
<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>
“如何从服务器获取路径”是什么意思<代码>新文件(“/the/path/to/upload/files”)?我建议不要上传到webapp内容;如果你部署了一场战争,这(a)将不起作用,(b)在某些类型的重新部署中可能会失败。
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);