溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2020-09-28 14:16:05 來源:腳本之家 閱讀:176 作者:Giving_bestself 欄目:編程語言

前臺(tái)form 表單:設(shè)置method=post,enctype=multipart/form-data。

struts2在原有的上傳解析器繼承上做了進(jìn)一步封裝,更進(jìn)一步簡(jiǎn)化了文件上傳。

Action需要使用3個(gè)屬性來封裝該文件域的信息:

(1)類型為File的*屬性封裝了該文件域?qū)?yīng)的文件內(nèi)容;
(2)類型為String的***FileName屬性封裝了該文件域?qū)?yīng)的文件的文件類型;
(3)類型為String的***ContentType屬性封裝了該文件域?qū)?yīng)的文件的類型。

具體實(shí)現(xiàn):

新建web項(xiàng)目

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

添加struts2相關(guān)包
myeclipse可直接下載,右擊項(xiàng)目,如下。

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

前臺(tái)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
 <body>
  <form action="upload.action" method="post" enctype="multipart/form-data">
    <input type="file" name="upload" multiple="multiple"/>
    <input type="submit" value="提交"/>

  </form>
 </body>
</html>

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
 <display-name>UploadFile</display-name>
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>*.action</url-pattern>
 </filter-mapping>
</web-app>

配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
  <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
  <constant name="struts.devMode" value="true"/>
  <constant name="struts.multipart.saveDir" value="/tmp"/>
  <constant name="struts.custom.i18n.resources" value="app"></constant>

  <package name="default" namespace="/" extends="struts-default">
    <action name="upload" class="com.yf.action.UploadAction">
    <result>/index.jsp</result>
    <interceptor-ref name="defaultStack"></interceptor-ref>
    </action>
  </package>
</struts>

后臺(tái)代碼

public class UploadAction extends ActionSupport{
  private List<File> upload;
  private List<String> uploadContentType;
  private List<String> uploadFileName;

  public List<File> getUpload() {
    return upload;
  }

  public void setUpload(List<File> upload) {
    this.upload = upload;
  }

  public List<String> getUploadContentType() {
    return uploadContentType;
  }

  public void setUploadContentType(List<String> uploadContentType) {
    this.uploadContentType = uploadContentType;
  }

  public List<String> getUploadFileName() {
    return uploadFileName;
  }

  public void setUploadFileName(List<String> uploadFileName) {
    this.uploadFileName = uploadFileName;
  }

  @Override
  public String execute() throws Exception {
    //文件保存路徑
    String path = ServletActionContext.getServletContext().getRealPath("/images");
    File file = new File(path);
    //不存在則創(chuàng)建
    if(!file.exists()){
      file.mkdir();
    }
    //循環(huán)將文件上傳到指定路徑
    for(int i = 0; i< upload.size(); i++){
      FileUtils.copyFile(upload.get(i), new File(file,uploadFileName.get(i)));
    }
    return SUCCESS;
  }

結(jié)果如下

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

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

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI