struts多文件上传

编程技术  /  houtizong 发布于 3年前   82

struts多文件上传

1、文件载入页面

 

  <body>    <form method="post" enctype="multipart/form-data" action="file_load">    文件一:<input type="file" name="images"><br/>    文件二:<input type="file" name="images"><br/>    文件三:<input type="file" name="images"><br/>    <input type="submit" value="提交"/>  </form></body>

 2、struts.xml里面action配置

 

 

<struts><package name="default" namespace="/" extends="struts-default" ><action name="file_*" class="com.edu.hpu.action.FileAction" method="{1}"><result>/file_{1}.jsp</result></action></package></struts>

 3、FileAction

 

 

package com.edu.hpu.action;import java.io.File;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class FileAction extends ActionSupport {/** * 使用数组进行多文件接收 * images:接收到的文件 * imagesFileName:接收到的文件名 */private File[] images;private String[] imagesFileName;public File[] getImages() {return images;}public void setImages(File[] images) {this.images = images;}public String[] getImagesFileName() {return imagesFileName;}public void setImagesFileName(String[] imagesFileName) {this.imagesFileName = imagesFileName;}@Overridepublic String execute() throws Exception {return super.execute();}public String input() throws Exception {return SUCCESS;}public String load() throws Exception  {//获得并创建存储路径String savePath = ServletActionContext.getServletContext().getRealPath("/images");File filePath = new File(savePath);if(!filePath.exists()) {filePath.mkdirs();}//遍历获得文件进行文件存储for(int i = 0; i < images.length; i++) {File file = new File(filePath,imagesFileName[i]);if(!file.exists()) {file.createNewFile();}System.out.println(file.getAbsolutePath());FileUtils.copyFile(images[i], file);}return SUCCESS;}}

 4、结果展示页面

<body>  文件  <s:iterator value="imagesFileName" var="fileName"> <s:property value="fileName"/>, </s:iterator>上传成功!</body>

 

 

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客