Struts2多文件上传
来源:互联网 发布:从淘宝上怎么买东西 编辑:程序博客网 时间:2024/05/29 19:33
多文件上传和单文件上传基本一样,只是变成了数组。类不要忘了建立get set方法。 上传文件jsp页面 大头像上传:<input type="file" name="mypic"/><br/>
小头像上传:<input type="file" name="mypic"/><br/>, 那个file那个name属性的名字要和实体类的
private String uploader;// 上传者
private File[] mypic;// 上传文件
private String[] mypicContentType;// 上传文件类型
private String[] mypicFileName;// 上传文件的文件名
// 定义服务器上传路径
Action类
package Action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import org.apache.struts2.ServletActionContext;public class UploadAction {// 定义请求参数private String uploader;// 上传者private File[] mypic;// 上传文件private String[] mypicContentType;// 上传文件类型private String[] mypicFileName;// 上传文件的文件名// 定义服务器上传路径private String savePath; public String getUploader() {return uploader;}public void setUploader(String uploader) {this.uploader = uploader;}public File[] getMypic() {return mypic;}public void setMypic(File[] mypic) {this.mypic = mypic;}public String[] getMypicContentType() {return mypicContentType;}public void setMypicContentType(String[] mypicContentType) {this.mypicContentType = mypicContentType;}public String[] getMypicFileName() {return mypicFileName;}public void setMypicFileName(String[] mypicFileName) {this.mypicFileName = mypicFileName;}public String getSavePath() {return savePath;}public void setSavePath(String savePath) {this.savePath = savePath;}public String load() throws Exception {// 获取服务器保存路径绝对地址String adsSavePath = ServletActionContext.getServletContext().getRealPath(savePath);try {//循环处理多按个文件上传for (int i = 0; i < mypic.length; i++) {// 使用文件读写实现文件上传FileInputStream fis = new FileInputStream(mypic[i]);FileOutputStream fos = new FileOutputStream(adsSavePath+ File.separator + mypicFileName[i]);byte[] b = new byte[fis.available()];fis.read();fos.write(b);// 关闭流fis.close();fos.flush();fos.close();}} catch (Exception e) {e.printStackTrace();return "error";}return "success";}}
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> <package name="default" namespace="/" extends="struts-default"> <action name="upload" class="Action.UploadAction" method="load"> <param name="savePath">/upload</param> <result name="success">success.jsp</result> <result name="error">error.jsp</result> </action> </package></struts>
上传页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>下载页面</title> </head> <body> <form action="upload.action" method="post" enctype="multipart/form-data"> 上传者:<input type="text" name="uploader"/><br/> 大头像上传:<input type="file" name="mypic"/><br/> 小头像上传:<input type="file" name="mypic"/><br/> <input type="submit" value="上传"/><br/> </form> </body></html>
上传成功页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>文件上传成功页面</title></head><body> 恭喜:${uploader }上传成功<br/> 文件上传列表: <c:forEach var="f" items="${mypicFileName}"> <li>${f}</li> </c:forEach></body></html>
上传失败页面
0 0
- Struts2多文件上传
- struts2多文件上传
- struts2多文件上传
- struts2 多文件上传
- struts2多文件上传
- struts2多文件上传
- struts2多文件上传
- struts2多文件上传
- Struts2多文件上传
- Struts2多文件上传
- Struts2 多文件上传
- struts2多文件上传
- struts2 多文件上传
- Struts2多文件上传
- struts2多文件上传
- struts2 多文件上传
- struts2多文件上传
- struts2多文件上传
- 第十四周项目1-验证算法
- java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for study.my
- timer多线程异步请求数据(Task)
- Android逆向之旅---Android中的sharedUserId属性详解
- 第十四周 项目一分块查找
- Struts2多文件上传
- angularjs 指令详解 - template, restrict, replace
- ubuntu升级提示/boot空间不足
- 关于前端结构调整的一次实践
- volatiel和synchronized的区别
- C# 简单发送邮件
- 第十三周项目3—Dijkstra算法
- 第十四周项目1(2)——验证分块查找算法
- 关于CSDN文章被自己不小心删除的解决办法