我正在尝试创建一个具有文件上传功能的网页。
FileUpload.jsp
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>file upload Page</title>
</head>
<body bgColor="lightBlue">
<s:form action="fileUpload" method="post" enctype="multipart/form-data" >
<s:file name="userTmp" label="File" />
<br/>
<s:submit value="uploadFile"/>
</s:form>
</body>
</html>
struts.xml中
<package name="upload" namespace="/FileUpload" extends="struts-default">
<action name="fileUpload" class="FileUploadAction">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
FileUploadAction.java
public class FileUploadAction extends ActionSupport{
private File userTmp;
private String userTmpContentType;
private String userTmpFileName;
Connection conn = null;
public String execute() throws Exception
{
String result = ERROR;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn= DriverManager.getConnection(".....");
System.out.println("DATABASE CONNECTED");
String sql = "insert into Evidence(Filename, FileType, FileContent,DateSubmitted) values(?, ?, ?, now())";
PreparedStatement pstmt = conn.prepareStatement(sql);
FileInputStream fis = new FileInputStream(userTmp);
pstmt.setString(1, userTmpFileName);
pstmt.setString(2, userTmpContentType);
pstmt.setBinaryStream(3, fis, (int)userTmp.length());
pstmt.executeUpdate();
pstmt.close();
fis.close();
result = SUCCESS;
}
catch (Exception e) {
e.printStackTrace();
}
finally
{
conn.close();
}
return result;
}
public File getUserTmp() {
return userTmp;
}
public void setUserTmp(File userTmp) {
this.userTmp = userTmp;
}
public String getUserTmpContentType() {
return userTmpContentType;
}
public void setUserTmpContentType(String userTmpContentType) {
this.userTmpContentType = userTmpContentType;
}
public String getUserTmpFileName() {
return userTmpFileName;
}
public void setUserTmpFileName(String userTmpFileName) {
this.userTmpFileName = userTmpFileName;
}
}
当我在我的本地tomcat服务器上运行它时,这工作正常。
但是当我在不同的tomcat服务器上部署它时,就会出现这个错误:
No result defined for action FileUploadAction and result input
我尝试在FileUploadAction类的execute方法中做任何事情,只返回SUCCESS。但它引发了同样的错误。
经过一些代码调试后,我发现这是因为enctype =“multipart / form-data”。我使用不同的enctype修改了FileUpload.jsp页面并删除了jsp页面中的<s:file>
标记,但没有给出任何错误。
看起来很奇怪为什么它在我的本地tomcat服务器上工作,而不是在另一个服务器上工作。 我应该更改tomcat服务器中的任何内容以使encype =“multipart / form-data”工作...