国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Struts2實(shí)現(xiàn)單文件或多文件上傳功能

Struts2實(shí)現(xiàn)單文件或多文件上傳功能

2020-09-02 10:08hzc543806053 Java教程

這篇文章主要為大家詳細(xì)介紹了Struts2實(shí)現(xiàn)單文件或多文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一、簡(jiǎn)述

Struts2文件上傳其實(shí)也是通過(guò)攔截器來(lái)實(shí)現(xiàn)的,只是該攔截器定義為默認(rèn)攔截器了,所以不用自己去手工配置,<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>

二、指定用戶(hù)上傳文件的大小,有兩種方式

1)默認(rèn)是在default.properties 文件的 struts.multipart.maxSize=2097152  鍵值指定為2097152 也就是2M,通過(guò)計(jì)算 2097152/(1024*1024) = 2 M

那我們可以改變其默認(rèn)值,只要在src目錄下,新建一個(gè) struts.properties 文件,指定上傳大小 如下:

Struts2實(shí)現(xiàn)單文件或多文件上傳功能

一次上傳只可以上傳10M,不管一次上傳多少個(gè)文件,按總和計(jì)算

2)在struts.xml文件中指定,如圖:

Struts2實(shí)現(xiàn)單文件或多文件上傳功能

其實(shí)name就對(duì)應(yīng)struts.properties的鍵,value對(duì)應(yīng) 值

注意:如果即在struts.properties設(shè)定文件上傳大小,又在struts.xml 設(shè)定文件上傳大小,則struts.properties的優(yōu)先級(jí)高于struts.xml,一般在一處指定上傳大小即可,推薦 struts.properties

三、Struts2之單文件上傳

1.fileupload.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
  
 <title>My JSP 'fileupload.jsp' starting page</title>
  
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0"
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
 -->
 
 </head>
 
 <body>
  <!-- enctype 默認(rèn)是 application/x-www-form-urlencoded -->
  <form action="FileUpload2" enctype="multipart/form-data" method="post" >
   
    用戶(hù)名:<input type="text" name="usename"> <br/>
    上傳文件:<input type="file" name="file1"><br/>
     
    <input type="submit" value="提交"/>
  
  </form>
  
  
  
 </body>
</html>

2.具體處理上傳的 FileUpload.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.struts2.fileupload;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
 
import org.apache.struts2.ServletActionContext;
 
import com.opensymphony.xwork2.ActionSupport;
 
/**
 * 單個(gè)文件上傳
 * @author Administrator
 * 上傳文件其實(shí)是上傳了兩份,
 *
 * 首先將上傳的文件保存到 default.properties 文件中 struts.multipart.saveDir鍵指定的目錄中
 * 默認(rèn)是空的
 * 保存在 Tomcat 6.0\work\Catalina\localhost\struts2目錄下以.tmp后綴名的文件
 *
 * 如果要在 struts.multipart.saveDir 指定目錄, 則可以在 src文件夾下 建一個(gè) struts.properties,
 * 覆蓋 default.properties 的某些鍵值
 *
 * 還有一份是 存放在自己設(shè)定的目錄下
 */
public class FileUpload extends ActionSupport {
  
 private String usename ;
 private File file1 ; //具體上傳文件的 引用 , 指向臨時(shí)目錄中的臨時(shí)文件
 private String file1FileName ; // 上傳文件的名字 ,FileName 固定的寫(xiě)法
 private String file1ContentType ; //上傳文件的類(lèi)型, ContentType 固定的寫(xiě)法
  
 public String getUsename() {
  return usename;
 }
 public void setUsename(String usename) {
  this.usename = usename;
 }
 public File getFile1() {
  return file1;
 }
 public void setFile1(File file1) {
  this.file1 = file1;
 }
 public String getFile1FileName() {
  return file1FileName;
 }
 public void setFile1FileName(String file1FileName) {
  this.file1FileName = file1FileName;
 }
 public String getFile1ContentType() {
  return file1ContentType;
 }
 public void setFile1ContentType(String file1ContentType) {
  this.file1ContentType = file1ContentType;
 }
  
 @Override
 public String execute() throws Exception {
  //獲取文件存儲(chǔ)路徑
  String path = ServletActionContext.getRequest().getRealPath("/upload");
  //輸出流
  OutputStream os = new FileOutputStream(new File(path,file1FileName));
  //輸入流
  InputStream is = new FileInputStream(file1);
   
  byte[] buf = new byte[1024];
  int length = 0 ;
   
  while(-1 != (length = is.read(buf) ) )
  {
   os.write(buf, 0, length) ;
  }
  is.close();
  os.close();
   
  return SUCCESS;
 }
  
  
  
 
}

3.最終顯示結(jié)果的頁(yè)面,filedemo.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
 
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
  
 <title>My JSP 'filedemo.jsp' starting page</title>
  
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0"
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
 -->
 
 </head>
 
 <body>
 上傳成功: <br/>
 usename: <s:property value="usename" /><br/>
 file: <s:property value="file1FileName"/><br/>
 contentType: <s:property value="file1ContentType"/>
  
 </body>
</html>

四、Struts2之多文件上傳

1.fileupload.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
  
 <title>My JSP 'fileupload.jsp' starting page</title>
  
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0"
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
 -->
 
 </head>
 
 <body>
  <!-- enctype 默認(rèn)是 application/x-www-form-urlencoded -->
  <form action="FileUpload2" enctype="multipart/form-data" method="post" >
   
    用戶(hù)名:<input type="text" name="usename"> <br/>
    上傳文件:<input type="file" name="file1"><br/>
    上傳文件: <input type="file" name="file1"><br/> <!-- 兩個(gè)名字相同 都是file1 -->
    <input type="submit" value="提交"/>
  
  </form>
  
  
  
 </body>
</html>

兩個(gè)上傳文件的name屬性值要是一樣的,后臺(tái)方便處理

2.具體處理上傳文件的FileUpload2.java

多文件上傳用集合的方式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.struts2.fileupload;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
 
import org.apache.struts2.ServletActionContext;
 
import com.opensymphony.xwork2.ActionSupport;
 
/**
 * 多文件上傳,用集合的方式
 * @author Administrator
 *
 */
 
public class FileUpload2 extends ActionSupport {
  
 private String usename ;
 private List<File> file1 ;
 private List<String> file1FileName ;
 private List<String> file1ContentType ;
  
 public String getUsename() {
  return usename;
 }
 public void setUsename(String usename) {
  this.usename = usename;
 }
 public List<File> getFile1() {
  return file1;
 }
 public void setFile1(List<File> file1) {
  this.file1 = file1;
 }
 public List<String> getFile1FileName() {
  return file1FileName;
 }
 public void setFile1FileName(List<String> file1FileName) {
  this.file1FileName = file1FileName;
 }
 public List<String> getFile1ContentType() {
  return file1ContentType;
 }
 public void setFile1ContentType(List<String> file1ContentType) {
  this.file1ContentType = file1ContentType;
 }
  
 @Override
 public String execute() throws Exception {
   
  //獲取文件存儲(chǔ)路徑
  String path = ServletActionContext.getRequest().getRealPath("/upload");
   
  for(int i = 0 ; i < file1.size() ; i++ )
  {
   OutputStream os = new FileOutputStream(new File(path,file1FileName.get(i)));
    
   InputStream is = new FileInputStream(file1.get(i));
    
   byte[] buf = new byte[1024];
   int length = 0 ;
    
   while(-1 != (length = is.read(buf) ) )
   {
    os.write(buf, 0, length) ;
   }
    
   is.close();
   os.close();
    
  }
   
  return SUCCESS;
 }
 
}

3.用于顯示的界面filedemo.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
 
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
  
 <title>My JSP 'filedemo2.jsp' starting page</title>
  
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0"
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
 -->
 
 </head>
 
 <body>
    上傳成功:<br/>
  usename:<s:property value="usename"/><br/>
    <!-- 遍歷值 -->
    <s:iterator value="file1FileName" id="f"> <!-- id是一個(gè)對(duì)象,目前是一個(gè)字符串集合 可任意命名 -->
             文件:<s:property value="#f"/> <br/>
    <!-- 這里也可以調(diào)用方法 <s:property value="#f.toUpperCase()"/> -->
    </s:iterator>
  
  
 </body>
</html>

遍歷集合的方式,用struts2提供的標(biāo)簽 iterator 可以實(shí)現(xiàn)

?
1
2
3
4
5
<s:iterator value="file1FileName" id="f"> <!-- id是一個(gè)對(duì)象,目前是一個(gè)字符串集合 可任意命名-->
    文件:<s:property value="#f"/> <br/>
    <!-- 這里也可以調(diào)用方法 <s:property value="#f.toUpperCase()"/> -->
    toUpperCase()字符串的方法是把字母轉(zhuǎn)為大寫(xiě)
</s:iterator>

下載鏈接:

1)Servlet 文件上傳  點(diǎn)擊打開(kāi)鏈接
2)Struts2之下載  點(diǎn)擊打開(kāi)鏈接

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 羞羞网站免费 | 免费看黄色影片 | 中文字幕在线三区 | 蜜桃成人在线观看 | 国产高清美女一级a毛片久久 | 这里只有精品久久 | 久久亚洲一区二区 | 欧美黑人一级爽快片淫片高清 | 女同另类 | 九九re| 在线播放一区二区三区 | 中文字幕亚洲一区二区三区 | 特黄色一级片 | 精品视频一区二区三区 | 久久伊人国产 | 狠狠干五月天 | 欧美浮力 | 国产精品久久久久久中文字 | 成人在线观| 国产精品成人国产乱一区 | 亚洲三级黄色 | 婷婷激情综合 | 久久久精品天堂 | 精品久久久久久久久久久久久久 | 欧美二区三区 | 午夜寂寞少妇aaa片毛片 | 成人av网站在线观看 | 欧美日韩在线电影 | 精品免费 | 亚洲精品1区2区 | 亚洲精品国产二区 | 亚洲欧美在线一区 | 青青久草 | 激情五月婷婷综合 | 懂色中文一区二区在线播放 | 欧美精品v国产精品v日韩精品 | 久久久久久av | 久久国产精品久久喷水 | 中文字幕亚洲一区二区三区 | 日韩精品一级毛片 | 久久99视频这里只有精品 |