WebContent 下的 Struts 2 上传文件目的地

问题描述 投票:0回答:2

我想将 Struts 2 上传文件目标放置在我的应用程序的

WebContent
下。

我已经用过这个方法了:

ServletContext context = ServletActionContext.getServletContext();
String uploadFilePath = context.getRealPath("/uploads");

但是

uploadFilePath
返回这个

D:\dev\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\MyApplication\uploads

我想得到这个返回值

D:\dev\workspace\MyApplication\WebContent\uploads

如何将Struts2上传文件目的地放置在

WebContent
下。我使用 Eclipse Juno 和 Windows7。

java eclipse file-upload struts2 nio
2个回答
0
投票

试试这个,

HttpServletRequest request=(HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
    String savePath =request.getRealPath("/uploads");
    //Here savePath=Application_ROOT_FOLDER\uploads

0
投票

Struts 2 将上传的文件放置在为您的网络服务器指定的 temporary 文件夹下。您可以将文件从此文件夹复制到您喜欢的目的地。

Files.copy(source, target, REPLACE_EXISTING); 

参见 Java 教程

© www.soinside.com 2019 - 2024. All rights reserved.