ここを参照しました。
http://www.javaroad.jp/opensource/js_struts20.htm
-------
web.xml
-------
<?xml version="1.0" encoding="Windows-31J"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee
" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd
">
<display-name>Struts</display-name>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
<welcome-file-list>
<welcome-file>FileUp.jsp</welcome-file>
<!--
<welcome-file>index.jsp</welcome-file>
-->
</welcome-file-list>
</web-app>
-------
-------
Struts-config.xml
-------
<?xml version="1.0" encoding="windows-31J"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://struts.apache.org/dtds/struts-config_1_2.dtd
">
<struts-config>
<!-- (1)アクション・フォームBeanの定義 -->
<form-beans>
<form-bean
name="FileUpForm"
type="struts.FileUploadForm"/>
</form-beans>
<!-- (2)アクション・クラスの定義 -->
<action-mappings>
<action path="/FileUpAction"
type="struts.FileUploadAction"
name="FileUpForm"
scope="request">
<!-- (3)アクション・クラス実行後の遷移先の定義 -->
<forward name="success" path="/FileUp2.jsp"/>
</action>
</action-mappings>
<!-- (4)ファイルアップロードに関する定義 -->
<controller maxFileSize="1024K"
bufferSize="1024"
tempDir="./Struts/tmps" />
</struts-config>
-------
-------
struts.FileUploadAction .java
-------
package struts;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import java.io.*;
//(1)FormFileインタフェースのインポート
import org.apache.struts.upload.FormFile;
public final class FileUploadAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
HttpServletResponse res) throws Exception{
//(2)アクション・フォームBeanオブジェクトの取得
FileUploadForm efuf = (FileUploadForm)form;
//(3)アクセスメソッドを使用してFormFileオブジェクトの取得
FormFile fileUp = efuf.getFileUp();
//(4)getInputStreamメソッドを使用し、入力ストリームを取得
InputStream is = fileUp.getInputStream();
//(5)入力ストリームをバッファリング
BufferedInputStream inBuffer = new BufferedInputStream(is);
//(6)ファイルのアップロード先を指定して、出力ストリームを生成
FileOutputStream fos = new FileOutputStream
("./"
+ fileUp.getFileName());
//(7)出力ストリームをバッファリング
BufferedOutputStream outBuffer = new BufferedOutputStream(fos);
int contents = 0;
//(8)入力データがなくなるまで入出力処理を実行
while ((contents = inBuffer.read()) != -1) {
outBuffer.write(contents);
}
outBuffer.flush();
inBuffer.close();
outBuffer.close();
//(9)一時領域のアップロードデータを削除
fileUp.destroy();
return (mapping.findForward("success"));
}
}
-------
-------
struts.FileUploadForm.java
-------
package struts;
import org.apache.struts.action.*;
//(1)FormFileインタフェースのインポート
import org.apache.struts.upload.FormFile;
public final class FileUploadForm extends ActionForm {
/**
*
*/
private static final long serialVersionUID = 1L;
//(2)FormFileインタフェースのfileUp変数を宣言
private FormFile fileUp;
//(3)アップロードファイル情報に対するアクセスメソッドを宣言
public FormFile getFileUp() { return fileUp;}
public void setFileUp(FormFile fileUp) {this.fileUp = fileUp;}
}
-------
-------
webcontent/FileUp.jsp
-------
<%@ page contentType="text/html; charset=Shift_JIS" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html:html>
<head><title>FileUpload処理</title></head>
<body>
<%-- (1)enctype属性にmultipart/form-dataを指定 --%>
<html:form action="/FileUpAction"
method="POST"
enctype="multipart/form-data">
<%-- (2)ファイル選択を行う<html:file>タグを指定 --%>
<html:file property="fileUp" />
<html:submit property="submit" value="送信"/>
</html:form>
</body>
</html:html>
-------
-------
webcontent/FileUp2.jsp
-------
<%@ page contentType="text/html; charset=Shift_JIS" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html:html>
<head><title>FileUpload処理</title></head>
<body>
アップロードしたファイル
<br>
<%-- (1)アップロードファイルのファイル名を表示 --%>
<bean:write name="FileUpForm"
property="fileUp.fileName" />
</body>
</html:html>
-------
上記アンドstrutsのjarファイルをtomcatが認識してくれなかったから
以下のファイルlib下(\tomcat6.0\lib)に直接配備してみた。
commons-beanutils.jar
commons-digester.jar
commons-io-1.4.jar
この辺の判断はエラーメッセージをよく読むと
org.apache.struts.taglib.html.FormTag.lookup
みたいな感じでエラーがでてて
struts.jarかなんかでlookupクラスみたいのが見つからないみたいにいわれてるんだけど、
jarは配備してあるよ~って
ときの対処法としてtomcat6.0\libに直接配備してみるとなぜかうまくいく。
また、javaのclassファイルがtomcatの再起動なんかでどうしても作成してくれないから
自分でビルドさせたこともある。
ってとこかな。
upload.jsp JSPのアップロード用の画面サンプル。 このサイトから引用 |
<%@ page contentType="text/html;charset=Windows-31J" %> |
んで、アップロード用のjavaソースがこれ0
package to.msn.wings.javatips;import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
public class UploadServlet extends HttpServlet {
public void doPost(HttpServletRequest request
, HttpServletResponse response)
throws ServletException, IOException {
String path=getServletContext().getRealPath("WEB-INF/data");
DiskFileUpload objDfu=new DiskFileUpload();
// アップロードファイルの最大サイズ(-1は無限)
objDfu.setSizeMax(-1);
// バッファサイズ
objDfu.setSizeThreshold(1024);
// 一時ファイルの保存先フォルダ
objDfu.setRepositoryPath(path);
// ヘッダの文字エンコーディング
objDfu.setHeaderEncoding("Windows-31J");
try {
// アップロードされたファイル情報を
// FileItemオブジェクトのリストとして取得
List objLst=objDfu.parseRequest(request);
Iterator objItr=objLst.iterator();
// リストから順にファイルデータを取り出し、
// 「/WEB-INF/data/元のファイル名」の形式で
// アップロードファイルを保存
while(objItr.hasNext()){
FileItem objFi=(FileItem)objItr.next();
if(!objFi.isFormField()){
// アップロードファイルの元ファイル名を取得
String strNam=objFi.getName();
if(strNam!=null && !strNam.equals("")){
strNam=(new File(strNam)).getName();
objFi.write(new File(path + "/" + strNam));
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
// 処理後は元のupload.jspにリダイレクト
response.sendRedirect("upload.jsp");
}
}