struts2文件上传file,contentType,fileName出现null
2014-08-29 15:40
387 查看
用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.
注意上面的X,代表文件名
也就是说如果是
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 Signature | Description |
---|---|
setX(File file) | The file that contains the content of the uploaded file. This is a temporary file andfile.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) |
也就是说如果是
<span class="code-tag"><s:form action=<span class="code-quote">"doUpload"</span> method=<span class="code-quote">"post"</span> enctype=<span class="code-quote">"multipart/form-data"</span>></span> <span class="code-tag"><s:file name=<span class="code-quote">"upload"</span> label=<span class="code-quote">"File"</span>/></span> <span class="code-tag"><s:submit/></span> <span class="code-tag"></s:form></span>
那么
<p><strong>Example Action class:</strong></p><div class="code panel" style="padding: 1px; border: 1px solid rgb(224, 198, 166); margin-bottom: 10px;"><div class="codeContent panelContent"><pre class="code-java" name="code" style="white-space: pre-wrap; word-wrap: break-word;"><span class="code-keyword">package</span> com.example; <span class="code-keyword">import</span> java.io.File; <span class="code-keyword">import</span> com.opensymphony.xwork2.ActionSupport; <span class="code-keyword">public</span> class UploadAction <span class="code-keyword">extends</span> ActionSupport { <span class="code-keyword">private</span> File upload; <span class="code-keyword">private</span> <span class="code-object">String</span> uploadContentType; <span class="code-keyword">private</span> <span class="code-object">String</span> uploadFileName; <span class="code-keyword">public</span> void setUpload(File file) { <span class="code-keyword">this</span>.file = file; } <span class="code-keyword">public</span> void setUploadContentType(<span class="code-object">String</span> contentType) { <span class="code-keyword">this</span>.contentType = contentType; } <span class="code-keyword">public</span> void setUploadFileName(<span class="code-object">String</span> filename) { <span class="code-keyword">this</span>.filename = filename; } <span class="code-keyword">public</span> <span class="code-object">String</span> execute() { <span class="code-comment">//... </span> <span class="code-keyword">return</span> SUCCESS; } }
而且要必须注意属性的大小写,否则会出现Null
<span class="code-keyword">private</span> File upload; <span class="code-keyword">private</span> <span class="code-object">String</span> uploadContentType; <span class="code-keyword">private</span> <span class="code-object">String</span> uploadFileName;
如果你写成文档上面的
<span class="code-keyword">package</span> com.example; <span class="code-keyword">import</span> java.io.File; <span class="code-keyword">import</span> com.opensymphony.xwork2.ActionSupport; <span class="code-keyword">public</span> class UploadAction <span class="code-keyword">extends</span> ActionSupport { <span class="code-keyword">private</span> File file; <span class="code-keyword">private</span> <span class="code-object">String</span> contentType; <span class="code-keyword">private</span> <span class="code-object">String</span> filename; <span class="code-keyword">public</span> void setUpload(File file) { <span class="code-keyword">this</span>.file = file; } <span class="code-keyword">public</span> void setUploadContentType(<span class="code-object">String</span> contentType) { <span class="code-keyword">this</span>.contentType = contentType; } <span class="code-keyword">public</span> void setUploadFileName(<span class="code-object">String</span> filename) { <span class="code-keyword">this</span>.filename = filename; } <span class="code-keyword">public</span> <span class="code-object">String</span> execute() { <span class="code-comment">//... </span> <span class="code-keyword">return</span> SUCCESS; } } 就会出现null(注意加上X代表表单type=file的name值)
相关文章推荐
- struts2文件上传file,contentType,fileName出现null
- Struts2 文件上传为什么会自动生成对应file的fileName和ContentType
- 关于struts2文件上传中的uploadContentType和uploadFileName
- 关于struts2文件上传中的uploadContentType和uploadFileName
- 关于struts2文件上传中的uploadContentType和uploadFileName
- 关于struts2文件上传中的uploadContentType和uploadFileName
- FCK和struts2整合,文件上传的时候出现error on file upload,error number:203
- SSH2中使用Struts2中的组件来上传文件出现文件上传后变得异常大的情况
- struts2上传文件,action接收到文件对象为null的解决方法
- ASP.NET中用HttpPostedFile.SaveAs(FileName)方法上传文件,文件大小限制
- Struts2实现文件上传,直接访问JSP页面出现异常
- struts2上传的Web文件ContentType类型大全
- Struts2 commons-fileupload 在上传2M以上文件出现异常解决方法
- struts2上传的Web文件ContentType类型大全
- Struts2中文件file的上传和下载
- Asp.net 文件上传的 FileUpload FileName 和 FileUpload PostedFile.FileName的细节问题
- struts2 使用 jakarta 上传文件时commons fileupload的异常捕捉
- struts2上传问题,取得文件为NULL问题
- struts2开发环境中,enctype="multipart/form-data"上传文件时,request.getInputStream()为null的原因(转http://topic.csdn.net/u/20101210/11/78454a19-7
- Struts2中导入外部xml文件出现Included file cannot be found错误!