Struts2文件上传
来源:互联网 发布:淘宝淘金币怎么用 编辑:程序博客网 时间:2024/06/11 22:32
用struts2来写文件上传是非常简单。
一、单文件上传
第一步写一个表单
<body> <form action="${pageContext.request.contextPath }/upload/UploadAction" method="post" enctype="multipart/form-data"> 文件上传<input type="file" name="image"><br> <input type="submit" value="上传"> </form> </body>
第二步在struts.xml添加
<package name="upload" namespace="/upload" extends="mypackage"> <action name="UploadAction" class="cn.hhtc.action.UploadAction" method="execute"> <result name="success">/success.jsp</result> </action> </package>
第三步编写类UploadAction
package cn.hhtc.action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.io.Serializable;import javax.servlet.ServletContext;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport implements Serializable {private File image; //表单输入的文件private String imageFileName;//获取文件名private String imageContentType;//获得上传文件的类型MIME类型public File getImage() {return image;}public void setImage(File image) {this.image = image;}public String getImageFileName() {return imageFileName;}public void setImageFileName(String imageFileName) {this.imageFileName = imageFileName;}public String getImageContentType() {return imageContentType;}public void setImageContentType(String imageContentType) {this.imageContentType = imageContentType;}public String execute(){try {System.out.println(imageContentType);ServletContext sc = ServletActionContext.getServletContext();String storePath = sc.getRealPath("/files");//构建输入输出流//OutputStream out = new FileOutputStream(storePath+"\\"+imageFileName);//InputStream in = new FileInputStream(image);//byte[] b = new byte[1024];//int len = -1;//while((len=in.read(b))!=-1)//{//out.write(b, 0, len);//}//in.close();//out.close();FileUtils.copyFile(image, new File(storePath, imageFileName));//代替上面的ActionContext.getContext().put("message", "文件上传成功");return SUCCESS;} catch (Exception e) {e.printStackTrace();return ERROR;}}}
第四步,success.jsp
<body>文件上传成功</body>
注意一下 UploadAction里面的字段要跟表单里面的字段一致,文件名字段为 表单文件名+FileName,文件类型为 表单文件名+ContentType
二、多文件上传
第一步:写一个表单
<body> <form action="${pageContext.request.contextPath }/upload2/UploadAction2" method="post" enctype="multipart/form-data"> 文件上传<input type="file" name="images"><br> 文件上传<input type="file" name="images"><br> <input type="submit" value="上传"> </form> </body>
第二步配置struts.xml
<package name="upload2" namespace="/upload2" extends="mypackage"> <action name="UploadAction2" class="cn.hhtc.action.UploadAction2" method="execute1"> <result name="success">/success.jsp</result> </action> </package>
第三步写UploadAction2类
package cn.hhtc.action;import java.io.File;import java.io.IOException;import java.io.Serializable;import javax.servlet.ServletContext;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class UploadAction2 extends ActionSupport implements Serializable {private File[] images;private String[] imagesFileName;private String[] imagesContentType;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;}public String[] getImagesContentType() {return imagesContentType;}public void setImagesContentType(String[] imagesContentType) {this.imagesContentType = imagesContentType;}public String execute1(){try {if(images!=null &&images.length>0){ServletContext sc = ServletActionContext.getServletContext();String storeFile = sc.getRealPath("/files");for(int i=0; i<images.length; i++){FileUtils.copyFile(images[i], new File(storeFile, imagesFileName[i]));}}ActionContext.getContext().put("message", "<font color='red'>文件上传成功</font>");return SUCCESS;} catch (IOException e) {e.printStackTrace();return ERROR;}}}
0 0
- struts2上传文件(三) struts2上传文件
- [Struts2]Struts2的文件上传
- Struts2的文件上传
- Struts2文件上传
- Struts2 上传文件实例
- struts2文件上传
- struts2文件上传
- Struts2的文件上传
- struts2-上传文件(1)
- struts2-上传文件(2)
- Struts2上传文件示例
- struts2文件上传
- Struts2实现文件上传
- struts2文件上传
- Struts2 文件上传
- ext + struts2 上传文件
- Struts2 文件上传
- Struts2的文件上传
- hdu3001——Travelling 三进制TSP, 状态压缩
- 第十二周项目 3 用递归方法求解(输出Fibnacci序列的第20个数)
- 为什么现在是创业的最好时代
- fragment+viewpager学习二(fragment间的跳转) 更新中
- 算法导论-------------快排的研究
- Struts2文件上传
- 利用Java反射机制和POI实现导出Excel功能
- 项目总结-前台js小知识
- 第12周项目--递归求最大公约数
- SpringMVC-03_SpringMVC基于注解开发
- sgu131:Hardwood floor
- Android 四大组件·五大布局
- Program work 5. 用链表实现矩阵及实现矩阵转置
- linux curl 命令详解,以及实例