我正在尝试创建一个具有文件上传功能的网页。
文件上传.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>
文件上传动作.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 类的执行方法中不做任何事情,只返回 SUCCESS。但它抛出了同样的错误。
经过一些代码调试后,我发现这是因为 enctype="multipart/form-data"。我用不同的enctype修改了FileUpload.jsp页面并删除了<s:file>
jsp页面中的标签,它没有给出任何错误。
似乎很奇怪,为什么它在我的本地 tomcat 服务器上工作,但在不同的服务器上却没有。我是否应该更改 tomcat 服务器中的任何内容以使 encype="multipart/form-data" 正常工作...