struts 2ファイルアップロードダウンロード
20469 ワード
ファイルのアップロード
Struts 2ファイルのアップロード
formフォームが必要で、Formフォームの提出方法はpostで、enctype=“multipart/form-data”を加えます
マルチファイルアップロードの名前と同じように、formを使う以外は、通常のテキストボックスでバックグラウンドに値を伝えるのと同じです.
Javaコード:
Javaコードにも違いはありません.fileのlistクラスがあり、fileを受信し、ioストリームでサーバハードディスクに書き込めば格納できます. 特別なところはない
struts 2のファイルでアップロードする際の注意点はいくつかあります.
1、 ページにformフォームを使用し、postでコミットし、enctype=「multipart/form-data」を追加します.
2、 複数のファイルでも1つのファイルでも、ページfileラベルnameとバックグラウンドjavaコードはバインドされています.たとえば、ページfileラベルnameが「**」である場合、バックグラウンドで定義された3つの変数(受信ファイル、受信ファイル名、受信ファイルタイプ)は、「*****」、「***ContentType」の順で、setメソッドとgetメソッドがあればよいでしょう.
Struts 2ダウンロード
ページ
strut 2構成
JAvaバックグラウンドコード
ファイルのダウンロードのポイントは アクションの public InputStream getInputStream() メソッド、struts.xml inputStream最終ダウンロード用はこの方法です
Struts 2ファイルのアップロード
1 <form method="post" action="fileupload.action" enctype="multipart/form-data">
2
3 <table>
4
5 <tr><td><input type="file" width="300px" name="myfile"/></td></tr>
6
7 <tr><td><input type="file" width="300px" name="myfile"></td></tr>
8
9 <tr><td><input type="file" width="300px" name="myfile"></td></tr>
10
11 <tr><td><input type="submit" value=" "></td></tr>
12
13 </table>
14
15 </form>
formフォームが必要で、Formフォームの提出方法はpostで、enctype=“multipart/form-data”を加えます
マルチファイルアップロードの名前と同じように、formを使う以外は、通常のテキストボックスでバックグラウンドに値を伝えるのと同じです.
<action name="fileupload" class="utilAction" method="fileupload"/>
Javaコード:
1 private List<File> myfile;
2
3 private List<String> myfileFileName;
4
5
6
7 public String fileupload(){
8
9 FileInputStream fin =null;
10
11 FileOutputStream fout =null;
12
13 byte[] buffer;
14
15 String filename ;
16
17 try {
18
19 for(int i=0;i<myfile.size();i++){
20
21
22
23 fin = new FileInputStream(myfile.get(i));
24
25 filename = PATH + myfileFileName.get(i);
26
27 fout = new FileOutputStream(new File(filename));
28
29
30
31 buffer = new byte[1024];
32
33 int count;
34
35 count = fin.read(buffer);
36
37 while(count>0){
38
39 fout.write(buffer);
40
41 count = fin.read(buffer);
42
43 }
44
45 System.out.println(" ");
46
47
48
49 }
50
51
52
53 fout.flush();
54
55 fin.close();
56
57 fout.close();
58
59
60
61 } catch (Exception e) {
62
63 e.printStackTrace();
64
65 }
66
67 return null;
68
69 }
Javaコードにも違いはありません.fileのlistクラスがあり、fileを受信し、ioストリームでサーバハードディスクに書き込めば格納できます. 特別なところはない
struts 2のファイルでアップロードする際の注意点はいくつかあります.
1、 ページにformフォームを使用し、postでコミットし、enctype=「multipart/form-data」を追加します.
2、 複数のファイルでも1つのファイルでも、ページfileラベルnameとバックグラウンドjavaコードはバインドされています.たとえば、ページfileラベルnameが「**」である場合、バックグラウンドで定義された3つの変数(受信ファイル、受信ファイル名、受信ファイルタイプ)は、「*****」、「***ContentType」の順で、setメソッドとgetメソッドがあればよいでしょう.
Struts 2ダウンロード
ページ
<a href="filedownload.action?filename=1"> </a>
strut 2構成
1 <action name="filedownload" class="utilAction" method="filedownload">
2
3 <param name="directory">f:/download/</param>
4
5 <result name="success" type="stream">
6
7 <!-- ,text/plain -->
8
9 <param name="contentType">text/plain</param>
10
11 <!-- inputName inputStream, action inputStream, -->
12
13 <param name="inputName">inputStream</param>
14
15 <!-- , Action filename-->
16
17 <param name="contentDisposition">
18
19 attachment;filename="${filename}"
20
21 </param>
22
23 <param name="bufferSize">2048</param>
24
25 </result>
26
27 </action>
JAvaバックグラウンドコード
1 public String filedownload(){
2
3
4
5 if(filename == null){
6
7 return null;
8
9 }
10
11 else{
12
13 return SUCCESS;
14
15 }
16
17 }
18
19
20
21
22
23 public InputStream getInputStream(){
24
25 String dir = directory + filename + ".jpg";
26
27
28
29 FileInputStream fin = null;
30
31 try {
32
33 fin = new FileInputStream(dir);
34
35 } catch (FileNotFoundException e) {
36
37 e.printStackTrace();
38
39 }
40
41
42
43 return fin; // dir
44
45 // return ServletActionContext.getServletContext().getResourceAsStream(dir); // dir Resource
46
47 }
ファイルのダウンロードのポイントは アクションの public InputStream getInputStream() メソッド、struts.xml inputStream最終ダウンロード用はこの方法です