文件上载不适用于Struts2模型驱动接口
当我使用模型驱动界面时,我的文件上传操作不起作用 它不会弹出任何错误,也不会生成任何日志 特此附上我的代码 我想知道,对于文件,我们是否必须使用模型驱动接口在模型或操作中生成其getter/setter 至少应该将其上传到temp文件夹,默认情况下,struts也会上传到temp文件夹 动作 ProductAction.java文件上载不适用于Struts2模型驱动接口,struts2,ognl,model-driven,Struts2,Ognl,Model Driven,当我使用模型驱动界面时,我的文件上传操作不起作用 它不会弹出任何错误,也不会生成任何日志 特此附上我的代码 我想知道,对于文件,我们是否必须使用模型驱动接口在模型或操作中生成其getter/setter 至少应该将其上传到temp文件夹,默认情况下,struts也会上传到temp文件夹 动作 ProductAction.java package com.fileupload.action; import com.opensymphony.xwork2.ActionSupport; import
package com.fileupload.action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.fileupload.model.FileUploadModel;
/**
* Action class to requests
* @package com.fileupload.action
*/
public class FileUploadAction extends ActionSupport implements ModelDriven<Object>{
FileUploadModel fileModel = new FileUploadModel();
@Override
public Object getModel() {
return fileModel;
}
}
配置struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.devMode" value="true"/>
<package name="FileUploadStruts" extends="struts-default">
<action name="index">
<result>/index.jsp</result>
</action>
<action name="submitForm" class="com.fileupload.action.FileUploadAction">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
/index.jsp
/result.jsp
查看CreateProduct.jsp
<%--
Document : index
Created on : Nov 26, 2017, 12:50:38 PM
Author : owner
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>File upload example</title>
</head>
<body>
<h1>Fill the form below</h1>
<s:form action="submitForm" enctype="multipart/form-data" name="employeeform">
<s:textfield name="employeeName"/>
<s:file name="image"/>
<s:submit value="Submit"/>
</s:form>
</body>
</html>
文件上传示例
填写下表
当然,当您使用模型时,您的文件上载操作可以正常工作
驱动接口,但您犯了一些错误:
然后,您需要在代码中添加一段代码,以便像
这:
@名称空间(“/”)
@ParentPackage(“struts默认值”)
公共类FileUploadAction扩展ActionSupport实现模型驱动{
私有静态最终长serialVersionUID=1L;
FileUploadModel fileModel=新FileUploadModel();
@凌驾
public FileUploadModel getModel(){
返回文件模型;
}
@操作(value=“submitForm”,结果={
@结果(name=“success”,location=“/Result.jsp”)}
公共字符串submitForm(){
HttpServletRequest=ServletActionContext.getRequest();
字符串tomcatPath=ServletActionContext.getServletContext().getRealPath(“/”);
字符串projectName=request.getContextPath();
projectName=projectName.substring(1);
字符串filePath=tomcatPath.substring(0,tomcatPath.indexOf(projectName))+“uploadFile”;
File dest=新文件(filePath,fileModel.getImageFileName());
试一试{
copyFile(fileModel.getImage(),dest);
}捕获(IOE异常){
e、 printStackTrace();
}
返回“成功”;
}
}
如果您根据我的答案实现了该功能,请回复
我,微笑
谢谢,正在更新ContentType并设置最大大小
<%--
Document : index
Created on : Nov 26, 2017, 12:50:38 PM
Author : owner
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>File upload example</title>
</head>
<body>
<h1>Fill the form below</h1>
<s:form action="submitForm" enctype="multipart/form-data" name="employeeform">
<s:textfield name="employeeName"/>
<s:file name="image"/>
<s:submit value="Submit"/>
</s:form>
</body>
</html>
public class FileUploadModel {
private String employeeName;
private File image;
private String imageFileName;
// YourCode was wrong : private String imageFileCotentType;
private String imageFileContentType;
(get set)...
@Namespace("/")
@ParentPackage("struts-default")
public class FileUploadAction extends ActionSupport implements ModelDriven<FileUploadModel> {
private static final long serialVersionUID = 1L;
FileUploadModel fileModel = new FileUploadModel();
@Override
public FileUploadModel getModel() {
return fileModel;
}
@Action(value = "submitForm", results = {
@Result(name = "success", location = "/result.jsp") })
public String submitForm() {
HttpServletRequest request = ServletActionContext.getRequest();
String tomcatPath = ServletActionContext.getServletContext().getRealPath("/");
String projectName = request.getContextPath();
projectName = projectName.substring(1);
String filePath = tomcatPath.substring(0, tomcatPath.indexOf(projectName)) + "uploadFile";
File dest = new File(filePath, fileModel.getImageFileName());
try {
FileUtils.copyFile(fileModel.getImage(), dest);
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
}