struts2文件上传file,contentType,fileName出现null

用struts2文件上传,查看文档

The fileUpload interceptor will use setter injection to insert the uploaded file and related data into your Action class. For a form field named upload you would provide the three setter methods shown in the following example:

The purpose of each one of these methods is described in the table below. Notice that if you have multiple file form elements with different names you would be required to have another corresponding set of these methods for each file uploaded.

Method SignatureDescription
setX(File file)The file that contains the content of the uploaded file. This is a temporary file and file.getName() will not return the original name of the file
setXContentType(String contentType)The mime type of the uploaded file
setXFileName(String fileName)The actual file name of the uploaded file (not the HTML name)

注意上面的X,代表文件名

也就是说如果是

<s:form action="doUpload" method="post" enctype="multipart/form-data">
    <s:file name="upload" label="File"/>
    <s:submit/>
</s:form>
那么

Example Action class:

package com.example;

   import java.io.File;
   import com.opensymphony.xwork2.ActionSupport;

   public class UploadAction extends ActionSupport {
      private File upload;
      private String uploadContentType;
      private String uploadFileName;

      public void setUpload(File file) {
         this.file = file;
      }

      public void setUploadContentType(String contentType) {
         this.contentType = contentType;
      }

      public void setUploadFileName(String filename) {
         this.filename = filename;
      }

      public String execute() {
         //...
         return SUCCESS;
      }
 }
而且要必须注意属性的大小写,否则会出现Null
private File upload; private String uploadContentType; private String uploadFileName;
如果你写成文档上面的
package com.example;

   import java.io.File;
   import com.opensymphony.xwork2.ActionSupport;

   public class UploadAction extends ActionSupport {
      private File file;
      private String contentType;
      private String filename;

      public void setUpload(File file) {
         this.file = file;
      }

      public void setUploadContentType(String contentType) {
         this.contentType = contentType;
      }

      public void setUploadFileName(String filename) {
         this.filename = filename;
      }

      public String execute() {
         //...
         return SUCCESS;
      }
 }
就会出现null(注意加上X代表表单type=file的name值)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Struts2导出Word文件的步骤: 1.添加poi-ooxml和poi-ooxml-schemas依赖包到项目中。 2.创建一个Word模板文件,可以使用Microsoft Word或其他工具创建。在模板文件中,使用占位符来标记需要替换的文本,例如:${name}。 3.创建一个Action类,该类将负责生成Word文件。在该类中,使用POI库来读取Word模板文件,并将占位符替换为实际的文本。最后,将生成的Word文件作为响应发送给客户端。 下面是一个示例Action类的代码: ```java import java.io.FileInputStream; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import com.opensymphony.xwork2.ActionSupport; public class ExportWordAction extends ActionSupport { private static final long serialVersionUID = 1L; private InputStream inputStream; private String fileName; public String execute() throws Exception { // 读取Word模板文件 XWPFDocument doc = new XWPFDocument(new FileInputStream("template.docx")); // 替换占位符 Map<String, String> map = new HashMap<String, String>(); map.put("name", "张三"); map.put("age", "25"); for (XWPFParagraph p : doc.getParagraphs()) { for (XWPFRun r : p.getRuns()) { String text = r.getText(0); if (text != null) { for (Map.Entry<String, String> entry : map.entrySet()) { if (text.contains(entry.getKey())) { text = text.replace(entry.getKey(), entry.getValue()); r.setText(text, 0); } } } } } // 输出Word文件 fileName = "output.docx"; inputStream = doc.generateInputStream(); return SUCCESS; } public InputStream getInputStream() { return inputStream; } public String getFileName() { return fileName; } } ``` 4.在struts.xml文件中配置Action类和结果类型: ```xml <action name="exportWord" class="com.example.ExportWordAction"> <result name="success" type="stream"> <param name="contentType">application/vnd.openxmlformats-officedocument.wordprocessingml.document</param> <param name="inputName">inputStream</param> <param name="contentDisposition">attachment;filename="${fileName}"</param> <param name="bufferSize">1024</param> </result> </action> ``` 5.在JSP页面中添加一个链接或按钮,以触发Action类的执行: ```html <a href="exportWord.action">导出Word文件</a> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值