最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年03月27日 16:26:06   作者:hzc543806053  
這篇文章主要為大家詳細介紹了Struts2實現(xiàn)單文件或多文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

一、簡述

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

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

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

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

一次上傳只可以上傳10M,不管一次上傳多少個文件,按總和計算

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

其實name就對應struts.properties的鍵,value對應 值

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

三、Struts2之單文件上傳

1.fileupload.jsp

<%@ 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 默認是 application/x-www-form-urlencoded --> 
  <form action="FileUpload2" enctype="multipart/form-data" method="post" > 
   
    用戶名:<input type="text" name="usename"> <br/> 
    上傳文件:<input type="file" name="file1"><br/> 
     
    <input type="submit" value="提交"/> 
  
  </form> 
  
  
  
 </body> 
</html> 

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

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; 
 
/** 
 * 單個文件上傳 
 * @author Administrator 
 * 上傳文件其實是上傳了兩份, 
 * 
 * 首先將上傳的文件保存到 default.properties 文件中 struts.multipart.saveDir鍵指定的目錄中 
 * 默認是空的 
 * 保存在 Tomcat 6.0\work\Catalina\localhost\struts2目錄下以.tmp后綴名的文件 
 * 
 * 如果要在 struts.multipart.saveDir 指定目錄, 則可以在 src文件夾下 建一個 struts.properties, 
 * 覆蓋 default.properties 的某些鍵值 
 * 
 * 還有一份是 存放在自己設定的目錄下 
 */ 
public class FileUpload extends ActionSupport { 
  
 private String usename ; 
 private File file1 ; //具體上傳文件的 引用 , 指向臨時目錄中的臨時文件 
 private String file1FileName ; // 上傳文件的名字 ,FileName 固定的寫法 
 private String file1ContentType ; //上傳文件的類型, ContentType 固定的寫法 
  
 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 { 
  //獲取文件存儲路徑 
  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é)果的頁面,filedemo.jsp

<%@ 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

<%@ 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 默認是 application/x-www-form-urlencoded --> 
  <form action="FileUpload2" enctype="multipart/form-data" method="post" > 
   
    用戶名:<input type="text" name="usename"> <br/> 
    上傳文件:<input type="file" name="file1"><br/> 
    上傳文件: <input type="file" name="file1"><br/> <!-- 兩個名字相同 都是file1 --> 
    <input type="submit" value="提交"/> 
  
  </form> 
  
  
  
 </body> 
</html> 

兩個上傳文件的name屬性值要是一樣的,后臺方便處理

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

多文件上傳用集合的方式

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 { 
   
  //獲取文件存儲路徑 
  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

<%@ 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是一個對象,目前是一個字符串集合 可任意命名 --> 
             文件:<s:property value="#f"/> <br/> 
    <!-- 這里也可以調(diào)用方法 <s:property value="#f.toUpperCase()"/> --> 
    </s:iterator> 
  
  
 </body> 
</html> 

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

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

下載鏈接:

1)Servlet 文件上傳  點擊打開鏈接
2)Struts2之下載  點擊打開鏈接

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Java集合類知識點總結(jié)

    Java集合類知識點總結(jié)

    本文把Java集合類的相關知識點做了總結(jié),并把Java常用集合類之間的區(qū)別做了分析,一起參考學習下。
    2018-02-02
  • java數(shù)據(jù)結(jié)構(gòu)與算法之雙向循環(huán)隊列的數(shù)組實現(xiàn)方法

    java數(shù)據(jù)結(jié)構(gòu)與算法之雙向循環(huán)隊列的數(shù)組實現(xiàn)方法

    這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之雙向循環(huán)隊列的數(shù)組實現(xiàn)方法,結(jié)合實例形式分析了雙向循環(huán)隊列的原理與數(shù)組實現(xiàn)技巧,并附帶說明了該算法的用途,需要的朋友可以參考下
    2016-08-08
  • Java使用String.format方法格式化字符串的示例詳解

    Java使用String.format方法格式化字符串的示例詳解

    在編程過程中,我們經(jīng)常需要創(chuàng)建格式化的字符串來滿足特定的需求,比如生成用戶友好的消息、構(gòu)建報告或是輸出調(diào)試信息,Java 提供了一個強大的工具——String.format 方法,本文給大家介紹了Java使用String.format方法格式化字符串的示例,需要的朋友可以參考下
    2024-11-11
  • 幾句話說清session,cookie和token的區(qū)別及說明

    幾句話說清session,cookie和token的區(qū)別及說明

    這篇文章主要介紹了幾句話說清session,cookie和token的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Spring Security添加驗證碼的兩種方式小結(jié)

    Spring Security添加驗證碼的兩種方式小結(jié)

    使用spring security的時候,框架會幫我們做賬戶密碼的驗證,但是如我們需要添加一個驗證碼,就需要對配置文件進行修改,這篇文章主要給大家介紹了關于Spring Security添加驗證碼的兩種方式,需要的朋友可以參考下
    2021-10-10
  • java動態(tài)代理(jdk與cglib)詳細解析

    java動態(tài)代理(jdk與cglib)詳細解析

    靜態(tài)代理:由程序員創(chuàng)建或特定工具自動生成源代碼,再對其編譯。在程序運行前,代理類的.class文件就已經(jīng)存在了
    2013-09-09
  • mybatis如何在一個update標簽中寫多條update語句

    mybatis如何在一個update標簽中寫多條update語句

    這篇文章主要介紹了mybatis如何在一個update標簽中寫多條update語句問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Java中雙重檢查鎖(double checked locking)的正確實現(xiàn)

    Java中雙重檢查鎖(double checked locking)的正確實現(xiàn)

    雙重檢查鎖(Double-Check Locking),顧名思義,通過兩次檢查,并基于加鎖機制,實現(xiàn)某個功能,下面這篇文章主要給大家介紹了關于Java中雙重檢查鎖(double checked locking)的相關資料,需要的朋友可以參考下
    2021-09-09
  • Java 詳解如何獲取網(wǎng)絡接口信息

    Java 詳解如何獲取網(wǎng)絡接口信息

    讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實踐中才能獲得能力的提升,本篇文章手把手帶你用Java獲取網(wǎng)絡接口的信息,大家可以在過程中查缺補漏,提升水平
    2021-11-11
  • 探討Java中函數(shù)是值傳遞還是引用傳遞問題

    探討Java中函數(shù)是值傳遞還是引用傳遞問題

    這篇文章主要介紹了探討Java中函數(shù)是值傳遞還是引用傳遞問題,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-02-02

最新評論

内黄县| 芜湖市| 临湘市| 石阡县| 静乐县| 颍上县| 宿州市| 大冶市| 晋州市| 文昌市| 齐齐哈尔市| 玛多县| 濮阳县| 澄城县| 微博| 商城县| 托里县| 安徽省| 合江县| 马边| 高青县| 时尚| 舒兰市| 灵宝市| 卓资县| 昌平区| 航空| 手游| 高要市| 阳朔县| 嘉禾县| 寻乌县| 临泽县| 南宁市| 同德县| 当雄县| 武穴市| 辽宁省| 托里县| 漳浦县| 潢川县|