分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

struts2——多文件上传

发布时间:2023-09-06 02:09责任编辑:熊小新关键词:文件上传
 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 ????pageEncoding="UTF-8"%> 3 <%@taglib uri="/struts-tags" prefix="s"%> 4 <!DOCTYPE html> 5 <html> 6 ????<head> 7 ????????<meta charset="UTF-8"> 8 ????????<title>struts2的一个例子</title> 9 ????</head>10 ????<body>11 ????????<s:form action="files.action" method="post" enctype="multipart/form-data">12 ????????????<s:textarea name="username" label="用户名"/>13 ????????????<s:file name="files" label="请选择上传文件"/>14 ????????????<s:file name="files" label="请选择上传文件"/>15 ????????????<s:submit value="提交"/>16 ????????</s:form>17 ????????18 ????</body>19 </html>
index.jsp代码
 1 <?xml version="1.0" encoding="UTF-8"?> 2 <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"> 3 ??<filter> 4 ????<filter-name>struts2</filter-name> 5 ????<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 6 ??</filter> 7 ??<filter-mapping> 8 ????<filter-name>struts2</filter-name> 9 ????<url-pattern>/*</url-pattern>10 ??</filter-mapping>11 ??<display-name></display-name>12 ??<welcome-file-list>13 ????<welcome-file>index.jsp</welcome-file>14 ??</welcome-file-list>15 </web-app>
web.xml代码
 1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 ????"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 ????"http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 ????<constant name="struts.devMode" value="true"/> 7 ????<package name="hello" extends="struts-default" namespace="/"> 8 ????????<action name="files" class="com.xiaostudy.web.UpFiles" method="upFiles"> 9 ????????????<result name="success">/ok.jsp</result>10 ????????????<result name="input">/err.jsp</result>11 ????????</action>12 ????</package>13 </struts>
struts.xml代码
 1 package com.xiaostudy.web; 2 ?3 import java.io.File; 4 ?5 import org.apache.struts2.ServletActionContext; 6 ?7 import com.opensymphony.xwork2.ActionSupport; 8 ?9 public class UpFiles extends ActionSupport {10 ????11 ????public String username;12 ????public File[] files;13 ????public String filesFileName[];14 ????public String filesContentType[];15 ????16 ????public String upFiles() {17 ????????18 ????????String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/files");19 ????????File file = new File(path);20 ????????if(!file.exists()) {21 ????????????file.mkdirs();22 ????????????System.out.println("创建了文件夹》》》》》》");23 ????????}24 ????????for(int i = 0; i < files.length && files != null; i++) {25 ????????????File file2 = new File(file, filesFileName[i]);26 ????????????System.out.println(files[i]);27 ????????????files[i].renameTo(file2);28 ????????????System.out.println(file);29 ????????????System.out.println(file2);30 ????????}31 ????????32 ????????33 ????????34 ????????return SUCCESS;35 ????}36 37 ????public String getUsername() {38 ????????return username;39 ????}40 41 ????public void setUsername(String username) {42 ????????this.username = username;43 ????}44 45 ????public File[] getFiles() {46 ????????return files;47 ????}48 49 ????public void setFiles(File[] files) {50 ????????this.files = files;51 ????}52 53 ????public String[] getFilesFileName() {54 ????????return filesFileName;55 ????}56 57 ????public void setFilesFileName(String[] filesFileName) {58 ????????this.filesFileName = filesFileName;59 ????}60 61 ????public String[] getFilesContentType() {62 ????????return filesContentType;63 ????}64 65 ????public void setFilesContentType(String[] filesContentType) {66 ????????this.filesContentType = filesContentType;67 ????}68 69 }
action动作类UpFiles
 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 ????pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 ????<head> 6 ????????<meta charset="UTF-8"> 7 ????????<title>struts2的一个例子</title> 8 ????</head> 9 ????<body>10 ????okokokok11 ????</body>12 </html>
ok.jsp代码
 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 ????pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body>10 上传失败!!!11 </body>12 </html>
err.jsp代码

struts2——多文件上传

原文地址:https://www.cnblogs.com/xiaostudy/p/9446522.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved