【问题标题】:How to get uploaded file in Struts action如何在 Struts 操作中获取上传的文件
【发布时间】:2015-07-02 12:20:56
【问题描述】:

这是上传文件的jsp页面:

<html:form action="/process.do?method=upload" method="post" enctype="multipart/form-data">       

     File :<input type="file" name="filename" /><br />

    <input type="submit" value="Upload File" style="margin: 40px 0 0 160px"/>

</html:form>

这是处理上传文件的动作转发方法:

public ActionForward upload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)  throws Exception 
     {
      System.out.println("Inside ProcessAction class upload method ");


    String filePath="";
     boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
     System.out.println("isMultipartContent -->>> "+isMultipartContent);
     FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
            try {
                @SuppressWarnings("unchecked")
                List<FileItem> fields = upload.parseRequest(request);
                 System.out.println("file name -> "+request.getParameter("filename"));
                System.out.println("fields --> "+fields.size());

                 Iterator<FileItem> it = fields.iterator();
                while (it.hasNext()) {
                System.out.println("inside while -->>> ");
                    FileItem fileItem = it.next();
                    filePath = fileItem.getName();
                    System.out.println("inside while filePath -->>> "+filePath);
                }

            } catch (FileUploadException e) {
                e.printStackTrace();
            }
    System.out.println("filePath -->>> "+filePath);

}

输出:

ProcessAction类内部上传方法
isMultipartContent -->> 真
文件名 -> null
场地-。 0
文件路径 -->>>

我无法获取上传的文件路径。我正在从本地计算机将文件上传到服务器。需要帮助

【问题讨论】:

  • 这是在struts 2中提交表单的正确方法吗?

标签: jsp servlets file-upload struts-1


【解决方案1】:

在struts 1中有更简单的上传文件的方法。你可以使用FormFile

使用方法可以看一下 http://wiki.apache.org/struts/StrutsFileUpload

【讨论】:

    猜你喜欢
    • 2013-05-15
    • 2013-02-18
    • 1970-01-01
    • 2020-02-23
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多