struts2中的s:file标签怎么限定上传文件为图片类型,最好是能在选择文件时就只能选择图片文件
展开全部
<struts>
<package name="default" extends="struts-default">
<action name="upload" class="g2w.struts2.FileUploadAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
text/plain,text/css,text/javascript
</param>
<param name="maximumSize">1000000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<param name="savePath">/uploads</param>
<result name="success">/success.jsp</result>
<result name="error">/failure.jsp</result>
<result name="input">/failure.jsp</result>
</action>
</package>
</struts>
package g2w.struts2;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private File file;
private String fileContentType;
private String fileFileName;
private String savePath;
// setters & getters
public String execute() {
try {
File destFile = new File(this.getSavePath(), fileFileName);
FileUtils.copyFile(this.file, destFile);
// ....
} catch (IOException e) {
this.setMessage(e.getMessage());
return ActionSupport.ERROR;
}
return SUCCESS;
}
}
<package name="default" extends="struts-default">
<action name="upload" class="g2w.struts2.FileUploadAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
text/plain,text/css,text/javascript
</param>
<param name="maximumSize">1000000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<param name="savePath">/uploads</param>
<result name="success">/success.jsp</result>
<result name="error">/failure.jsp</result>
<result name="input">/failure.jsp</result>
</action>
</package>
</struts>
package g2w.struts2;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private File file;
private String fileContentType;
private String fileFileName;
private String savePath;
// setters & getters
public String execute() {
try {
File destFile = new File(this.getSavePath(), fileFileName);
FileUtils.copyFile(this.file, destFile);
// ....
} catch (IOException e) {
this.setMessage(e.getMessage());
return ActionSupport.ERROR;
}
return SUCCESS;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询