ファイルのアップロード機能を備えた Web ページを作成しようとしています。
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" が原因であることがわかりました。FileUpload.jsp ページを別の enctype で変更し<s:file>
、jsp ページのタグを削除しましたが、エラーは発生しませんでした。
ローカルの tomcat サーバーでは動作しているのに、別のサーバーでは動作していないのは奇妙に思えます。encype="multipart/form-data" を機能させるために、Tomcat サーバーの何かを変更する必要がありますか...