struts2實現(xiàn)多文件上傳的示例代碼
開發(fā)環(huán)境JDK1.8 eclipse struts2-2.3.31
1.創(chuàng)建web項目
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處理的請求后綴,該屬性的默認值是action,即所有匹配*.action的請求都由Struts2處理。
如果用戶需要指定多個請求后綴,則多個后綴之間以英文逗號(,)隔開。 -->
<constant name="struts.action.extension" value="do" />
<!-- 設(shè)置瀏覽器是否緩存靜態(tài)內(nèi)容,默認值為true(生產(chǎn)環(huán)境下使用),開發(fā)階段最好關(guān)閉 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 當(dāng)struts的配置文件修改后,系統(tǒng)是否自動重新加載該文件,默認值為false(生產(chǎn)環(huán)境下使用),開發(fā)階段最好打開 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 開發(fā)模式下使用,這樣可以打印出更詳細的錯誤信息 -->
<constant name="struts.devMode" value="true" />
<!-- 默認的視圖主題 -->
<constant name="struts.ui.theme" value="simple" />
<!--<constant name="struts.objectFactory" value="spring" />-->
<!--解決亂碼 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 指定允許上傳的文件最大字節(jié)數(shù)。默認值是2097152(2M) -->
<constant name="struts.multipart.maxSize" value="10701096"/>
<!-- 設(shè)置上傳文件的臨時文件夾,默認使用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">
<!-- 動態(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">
<!-- 文件過濾 -->
<param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>
<!-- 文件大小, 以字節(jié)為單位 -->
<param name="maximumSize">1025956</param>
</interceptor-ref>
<!-- 默認攔截器必須放在fileUpload之后,否則無效 -->
<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; //文件名稱
private String[] imageContentType; //文件類型
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 ,作為上傳文件的頁面
<%@ 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的文件上傳標簽 -->
<s:form action="fileUpload" namespace="/" method="POST" enctype="multipart/form-data">
<!-- 該name需要和后臺的File類型的名字對應(yīng)起來,否則將得不到該文件 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)的頁面,創(chuàng)建WebContent/error.jsp 作為文件上傳失敗的頁面 , 創(chuàng)建WebContent/images文件夾,作為上傳文件的存儲位置
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)之HashMap源碼深入分析
Java HashMap是一種基于哈希表實現(xiàn)的鍵值對存儲結(jié)構(gòu),可以實現(xiàn)快速的數(shù)據(jù)查找和存儲。它是線程不安全的,但在單線程環(huán)境中運行效率高,被廣泛應(yīng)用于Java開發(fā)中2023-04-04
Spring Data JPA 簡單查詢--方法定義規(guī)則(詳解)
下面小編就為大家?guī)硪黄猄pring Data JPA 簡單查詢--方法定義規(guī)則(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
利用SpringBoot實現(xiàn)多數(shù)據(jù)源的兩種方式總結(jié)
關(guān)于動態(tài)數(shù)據(jù)源的切換的方案有很多,核心只有兩種,一種是構(gòu)建多套環(huán)境,另一種是基于spring原生的AbstractRoutingDataSource切換,這篇文章主要給大家介紹了關(guān)于利用SpringBoot實現(xiàn)多數(shù)據(jù)源的兩種方式,需要的朋友可以參考下2021-10-10
解決springboot application.yml變灰色的問題
這篇文章主要介紹了解決springboot application.yml變灰色的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07

