Struts2实现多文件上传
来源:互联网 发布:联想网络同传 编辑:程序博客网 时间:2024/06/05 20:36
strus2 配置文件
package action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport {//封装文件标题请求参数的属性private String title;//封装上传文件域的属性private File[] upload;//封装上传文件类型的属性private String[] uploadContentType;//封装上传文件名的属性private String[] uploadFileName;//直接在struts.xml文件中配置的属性private String savePath;private String allowType;//返回上传文件的保存位置public String getSavePath() throws Exception{return ServletActionContext.getServletContext().getRealPath(savePath);}//接受struts.xml文件配置值的方法public void setSavePath(String savePath) {this.savePath = savePath;}public String upload() throws Exception {for(int i=0;i<upload.length;i++){//以服务器的文件保存地址和原文件名建立上传文件输出流FileInputStream fis = new FileInputStream(upload[i]);FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"+ getTitle()+uploadFileName[i]);System.out.println(getUploadFileName()+"=================");byte[] buffer = new byte[1024];int len = 0;while ((len = fis.read(buffer)) > 0) {fos.write(buffer, 0, len);}fos.close();fis.close();}return SUCCESS;}//文件标题的setter和getter方法public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}//上传文件对应文件内容的setter和getter方法public File[] getUpload() {return upload;}public void setUpload(File[] upload) {this.upload = upload;}//上传文件的文件类型的setter和getter方法public String[] getUploadContentType() {return uploadContentType;}public void setUploadContentType(String[] uploadContentType) {this.uploadContentType = uploadContentType;}//上传文件的文件名的setter和getter方法public String[] getUploadFileName() {return uploadFileName;}public void setUploadFileName(String[] uploadFileName) {this.uploadFileName = uploadFileName;}public String getAllowType() {return allowType;}public void setAllowType(String allowType) {this.allowType = allowType;}}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><!-- 国际化 --><constant name="struts.custom.i18n.resources" value="mess" /><!-- 设置该应用使用的解码集 --><constant name="struts.i18n.encoding" value="UTF-8" /><package name="upload1" extends="struts-default"><!-- 配置处理文件上传的Action --><action name="upload" class="action.UploadAction" method="upload"><!-- 动态设置Action的属性值 --><param name="savePath">/uploadFiles</param><interceptor-ref name="fileUpload"><!-- 配置允许上传的文件类型 --><param name="allowedTypes">image/png,image/gif,image/jpeg,image/jpg</param><!-- 配置允许上传的文件大小 --><param name="maximumSize">20000</param></interceptor-ref><!--配置内部的拦截器, --><interceptor-ref name="defaultStack" /><!-- 配置Struts 2默认的视图页面 --><result>/succ.jsp</result><!-- 拦截器调用 --><result name="input">/index.jsp</result></action></package></struts>
jsp 页面
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><html> <head> <title>简单的文件上传</title> </head> <body> <s:fielderror/> <s:form action="/upload" enctype="multipart/form-data"> <s:textfield name="title" label="文件标题"/> <s:file name="upload" label="选择文件"/> <s:file name="upload" label="选择文件"/> <s:file name="upload" label="选择文件"/> <s:submit value="上传"/> </s:form> </body></html>
succ.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><html> <head> <title>上传成功</title> </head> <body> 上传成功<br/> 文件标题:<s:property value=" +title"/><br/> 文件为: <img src="<s:property value=" 'uploadFiles/' +uploadFileName"/>"/><br/> </body></html>
国际化:
mess.properties
#\u6539\u53D8\u6587\u4EF6\u7C7B\u578B\u4E0D\u5141\u8BB8\u7684\u63D0\u793A\u4FE1\u606F
struts.messages.error.content.type.not.allowed=\u60A8\u4E0A\u4F20\u7684\u6587\u4EF6\u7C7B\u578B\u53EA\u80FD\u662F\u56FE\u7247\u6587\u4EF6\uFF01\u8BF7\u91CD\u65B0\u9009\u62E9\uFF01
#\u6539\u53D8\u4E0A\u4F20\u6587\u4EF6\u592A\u5927\u7684\u63D0\u793A\u4FE1\u606F
struts.messages.error.file.too.large=\u60A8\u8981\u4E0A\u4F20\u7684\u6587\u4EF6\u592A\u5927\uFF0C\u8BF7\u91CD\u65B0\u9009\u62E9\uFF01
0 0
- Struts2实现多文件上传
- Struts2实现多文件上传
- struts2实现多文件上传
- Struts2实现多文件上传
- Struts2实现多文件上传
- Struts2实现多文件上传
- Struts2实现多文件上传
- Struts2实现多文件上传
- Struts2实现多文件上传
- struts2实现多文件上传
- Struts2实现多文件上传
- Struts2 实现多文件上传
- Struts2实现文件上传
- struts2实现文件上传
- struts2实现文件上传
- Struts2实现文件上传
- struts2实现文件上传
- struts2实现文件上传
- iOS 图片毛玻璃效果
- 基于IBFV算法的二维矢量场可视化
- 哈市新建10个蔬菜设施园区 淡季蔬菜自给率达60%以上
- 怎么放代码
- C++中的累类!
- Struts2实现多文件上传
- CSDN的IP数据库太老了该更新了!2014.3
- 【C/C++】C++primer习题9.26中删除元素问题
- C++打印功能!
- c01
- 【翻译】Mathematical Analysis of Algorithms
- [tornado示例]最简单的表单输入和显示
- 把字符串按升序排列
- 精度计算(大数方面)