struts2實(shí)現(xiàn)多文件上傳的示例代碼
開(kāi)發(fā)環(huán)境JDK1.8 eclipse struts2-2.3.31
1.創(chuàng)建web項(xiàng)目
2.導(dǎo)入struts2核心jar包
3.更改web.xml配置文件(只要配置好struts2的Filter就好)
4.創(chuàng)建src/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>
<!-- 該屬性指定需要Struts2處理的請(qǐng)求后綴,該屬性的默認(rèn)值是action,即所有匹配*.action的請(qǐng)求都由Struts2處理。
如果用戶需要指定多個(gè)請(qǐng)求后綴,則多個(gè)后綴之間以英文逗號(hào)(,)隔開(kāi)。 -->
<constant name="struts.action.extension" value="do" />
<!-- 設(shè)置瀏覽器是否緩存靜態(tài)內(nèi)容,默認(rèn)值為true(生產(chǎn)環(huán)境下使用),開(kāi)發(fā)階段最好關(guān)閉 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 當(dāng)struts的配置文件修改后,系統(tǒng)是否自動(dòng)重新加載該文件,默認(rèn)值為false(生產(chǎn)環(huán)境下使用),開(kāi)發(fā)階段最好打開(kāi) -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 開(kāi)發(fā)模式下使用,這樣可以打印出更詳細(xì)的錯(cuò)誤信息 -->
<constant name="struts.devMode" value="true" />
<!-- 默認(rèn)的視圖主題 -->
<constant name="struts.ui.theme" value="simple" />
<!--<constant name="struts.objectFactory" value="spring" />-->
<!--解決亂碼 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 指定允許上傳的文件最大字節(jié)數(shù)。默認(rèn)值是2097152(2M) -->
<constant name="struts.multipart.maxSize" value="10701096"/>
<!-- 設(shè)置上傳文件的臨時(shí)文件夾,默認(rèn)使用javax.servlet.context.tempdir -->
<constant name="struts.multipart.saveDir " value="d:/tmp" />
<package name="upload" extends="struts-default">
<action name="fileUpload" class="com.ifan.action.FileUpload">
<!-- 動(dòng)態(tài)設(shè)置savePath的屬性值 -->
<param name="savePath">WEB-INF/images</param>
<result name="success">/success.jsp</result>
<result name="input">/error.jsp</result>
<interceptor-ref name="fileUpload">
<!-- 文件過(guò)濾 -->
<param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>
<!-- 文件大小, 以字節(jié)為單位 -->
<param name="maximumSize">1025956</param>
</interceptor-ref>
<!-- 默認(rèn)攔截器必須放在fileUpload之后,否則無(wú)效 -->
<interceptor-ref name="defaultStack" />
</action>
</package>
</struts>
5.創(chuàng)建src/com.ifan.action.FileUpload.Java
package com.ifan.action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUpload extends ActionSupport{
private File[] image; //上傳的文件
private String[] imageFileName; //文件名稱(chēng)
private String[] imageContentType; //文件類(lèi)型
public String execute() throws Exception {
ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(realpath);
if (image != null) {
File savedir=new File(realpath);
if(!savedir.getParentFile().exists())
savedir.getParentFile().mkdirs();
for(int i=0;i<image.length;i++){
File savefile = new File(savedir, imageFileName[i]);
FileUtils.copyFile(image[i], savefile);
}
ActionContext.getContext().put("message", "文件上傳成功");
}
return "success";
}
public File[] getImage() {
return image;
}
public void setImage(File[] image) {
this.image = image;
}
public String[] getImageContentType() {
return imageContentType;
}
public void setImageContentType(String[] imageContentType) {
this.imageContentType = imageContentType;
}
public String[] getImageFileName() {
return imageFileName;
}
public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
}
}
6.創(chuàng)建WebContent/index.jsp ,作為上傳文件的頁(yè)面
<%@ 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" >
<title>My JSP 'hello.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" >
-->
</head>
<body>
<!-- Struts2的文件上傳標(biāo)簽 -->
<s:form action="fileUpload" namespace="/" method="POST" enctype="multipart/form-data">
<!-- 該name需要和后臺(tái)的File類(lèi)型的名字對(duì)應(yīng)起來(lái),否則將得不到該文件 size 上傳文件的大小 -->
<s:file name="image" label="Select a File to upload" size="40" />
<s:file name="image" label="Select a File to upload" size="40" />
<s:submit value="submit" name="submit" />
</s:form>
</body>
</html>
7.創(chuàng)建WebContent/success.jsp 作為文件上傳成功跳轉(zhuǎn)的頁(yè)面,創(chuàng)建WebContent/error.jsp 作為文件上傳失敗的頁(yè)面 , 創(chuàng)建WebContent/images文件夾,作為上傳文件的存儲(chǔ)位置
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JS+Struts2多文件上傳實(shí)例詳解
- struts2實(shí)現(xiàn)多文件上傳
- java中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- struts2單個(gè)文件上傳的兩種實(shí)現(xiàn)方式
- 關(guān)于Struts2文件上傳與自定義攔截器
- Struts2+jquery.form.js實(shí)現(xiàn)圖片與文件上傳的方法
- JavaWeb中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- JavaEE中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- java中struts2實(shí)現(xiàn)簡(jiǎn)單的文件上傳與下載
- Struts2+uploadify多文件上傳實(shí)例
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)之HashMap源碼深入分析
Java HashMap是一種基于哈希表實(shí)現(xiàn)的鍵值對(duì)存儲(chǔ)結(jié)構(gòu),可以實(shí)現(xiàn)快速的數(shù)據(jù)查找和存儲(chǔ)。它是線程不安全的,但在單線程環(huán)境中運(yùn)行效率高,被廣泛應(yīng)用于Java開(kāi)發(fā)中2023-04-04
Spring Data JPA 簡(jiǎn)單查詢(xún)--方法定義規(guī)則(詳解)
下面小編就為大家?guī)?lái)一篇Spring Data JPA 簡(jiǎn)單查詢(xún)--方法定義規(guī)則(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
利用SpringBoot實(shí)現(xiàn)多數(shù)據(jù)源的兩種方式總結(jié)
關(guān)于動(dòng)態(tài)數(shù)據(jù)源的切換的方案有很多,核心只有兩種,一種是構(gòu)建多套環(huán)境,另一種是基于spring原生的AbstractRoutingDataSource切換,這篇文章主要給大家介紹了關(guān)于利用SpringBoot實(shí)現(xiàn)多數(shù)據(jù)源的兩種方式,需要的朋友可以參考下2021-10-10
解決springboot application.yml變灰色的問(wèn)題
這篇文章主要介紹了解決springboot application.yml變灰色的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Java Web項(xiàng)目中驗(yàn)證碼功能的制作攻略
使用servlet制作驗(yàn)證碼中最關(guān)鍵的部分是緩存的使用,驗(yàn)證session中的字符串,接下來(lái)我們就來(lái)看一下Java Web項(xiàng)目中驗(yàn)證碼功能的制作攻略2016-05-05
詳解Java線程池如何實(shí)現(xiàn)優(yōu)雅退出
這篇文章我們將從源碼角度深度解析線程池是如何優(yōu)雅的退出程序的,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)java線程池有一定幫助,需要的可以參考一下2022-07-07

