当前位置: 移动技术网 > IT编程>开发语言>Java > Struts2实现单文件或多文件上传功能

Struts2实现单文件或多文件上传功能

2019年07月22日  | 移动技术网IT编程  | 我要评论

一、简述

struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<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的优先级高于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.最终显示结果的页面,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/> 
    <!-- 这里也可以调用方法 <s:property value="#f.touppercase()"/> --> 
    </s:iterator> 
  
  
 </body> 
</html> 

遍历集合的方式,用struts2提供的标签 iterator 可以实现

<s:iterator value="file1filename" id="f"> <!-- id是一个对象,目前是一个字符串集合 可任意命名-->
    文件:<s:property value="#f"/> <br/> 
    <!-- 这里也可以调用方法 <s:property value="#f.touppercase()"/> -->
    touppercase()字符串的方法是把字母转为大写
</s:iterator>

下载链接:

1)servlet 文件上传  点击打开
2)struts2之下载  点击打开

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网