您的位置:首页 > 编程语言 > Java开发

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.

Method SignatureDescription
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)
注意上面的X,代表文件名

也就是说如果是
<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值)



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: