溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

struts2實現多文件上傳

發布時間:2020-09-14 11:04:49 來源:腳本之家 閱讀:136 作者:雪飄雪融 欄目:編程語言

本文實例為大家分享了struts2實現多文件上傳的具體代碼,供大家參考,具體內容如下

首先搭建好struts2的開發環境,導入struts2需要的最少jar包

struts2實現多文件上傳

新建upload.jsp頁面,注意一定要把表單的enctype設置成multipart/form-data

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <title>My JSP 'upload.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"> 
 </head>
 
 <body>
 <s:fielderror name="fieldErrors"></s:fielderror>
 <s:form theme="simple" action="upload" enctype="multipart/form-data" method="post">
  file:<s:file name="file"></s:file>
  fileDesc:<s:textfield name="fileDesc[0]"></s:textfield>
  <br/><br/>
  file:<s:file name="file"></s:file>
  fileDesc:<s:textfield name="fileDesc[1]"></s:textfield>
  <br/><br/>
  file:<s:file name="file"></s:file>
  fileDesc:<s:textfield name="fileDesc[2]"></s:textfield>
  <s:submit></s:submit>
 </s:form>
 </body>
</html>

新建一個UploadAction類,這個類主要有三個屬性,并為這三個屬性生成對應的set get方法

  • [File Name] : 保存要上傳的文件
  • [File Name]ContentType : 保存要上傳的文件類型
  • [File Name]FileName :保存上傳的文件名
package cn.lfd.web.upload;
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;
/*
 * 多文件上傳要把對應的屬性類型都改為List集合,struts自動會把多個文件的數據封裝到里面
 */
public class UploadAction extends ActionSupport {
 private static final long serialVersionUID = 1L;
 private List<File> file;
 private List<String> fileContentType;
 private List<String> fileFileName;
 private List<String> fileDesc;
 
 public List<File> getFile() {
 return file;
 }
 
 public void setFile(List<File> file) {
 this.file = file;
 }
 
 public List<String> getFileContentType() {
 return fileContentType;
 }
 
 public void setFileContentType(List<String> fileContentType) {
 this.fileContentType = fileContentType;
 }
 
 public List<String> getFileFileName() {
 return fileFileName;
 }
 
 public void setFileFileName(List<String> fileFileName) {
 this.fileFileName = fileFileName;
 }
 
 public List<String> getFileDesc() {
 return fileDesc;
 }
 
 public void setFileDesc(List<String> fileDesc) {
 this.fileDesc = fileDesc;
 }
 
 @Override
 public String execute() throws Exception {
 //遍歷文件集合,通過IO流把每一個上傳的文件保存到upload文件夾下面
 for(int i=0;i<file.size();i++) {
 //得到要保存的文件路徑
 String dir = ServletActionContext.getServletContext().getRealPath("/upload/"+fileFileName.get(i));
 OutputStream out = new FileOutputStream(dir);
 InputStream in = new FileInputStream(file.get(i));
 byte[] flush = new byte[1024];
 int len = 0;
 while((len=in.read(flush))!=-1) {
 out.write(flush, 0, len);
 }
 in.close();
 out.close();
 }
 return "input";
 }
}

然后在struts.xml配置文件中配置一下

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 "http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 <constant name="struts.custom.i18n.resources" value="message"></constant>
 <package name="default" extends="struts-default">
 <interceptors>
 <interceptor-stack name="lfdstack">
 <interceptor-ref name="defaultStack">
  <param name="fileUpload.maximumSize">200000</param><!-- 限制上傳的單個文件的大小 -->
  <param name="fileUpload.allowedTypes">text/html,text/xml</param><!-- 限制上傳的文件的類型 -->
  <param name="fileUpload.allowedExtensions">txt,html,xml</param><!-- 限制上傳的文件的擴展名 -->
 </interceptor-ref>
 </interceptor-stack>
 </interceptors>
 <default-interceptor-ref name="lfdstack"></default-interceptor-ref>
 <action name="upload" class="cn.lfd.web.upload.UploadAction">
 <result>/success.jsp</result>
 <result name="input">/upload.jsp</result>
 </action>
 </package>
</struts>

在src目錄下新建一個message.properties文件定制錯誤消息

  • struts.messages.error.uploading - 文件不能被上傳
  • struts.messages.error.file.too.large - 文件超出大小
  • struts.messages.error.content.type.not.allowed - 文件類型不合法
  • struts.messages.error.file.extension.not.allowed - 文件擴展名不合法

struts2實現多文件上傳

顯示效果如下圖:

struts2實現多文件上傳

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女