我想将 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。
试试这个,
HttpServletRequest request=(HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
String savePath =request.getRealPath("/uploads");
//Here savePath=Application_ROOT_FOLDER\uploads
Struts 2 将上传的文件放置在为您的网络服务器指定的 temporary 文件夹下。您可以将文件从此文件夹复制到您喜欢的目的地。
Files.copy(source, target, REPLACE_EXISTING);
参见 Java 教程。