struts2上传实例
来源:互联网 发布:navicat for mysql破解 编辑:程序博客网 时间:2024/05/29 14:36
struts上传实例
1、新建一个web工程,添加struts2支持
2、在index.jsp页面写一个上传表单如下:
<body><!-- 一定要指定method为post,enctype为 multipart/form-data--><form action="upload.action" method="post"enctype="multipart/form-data">file:<input name="file" type="file" /><!-- name和后台定义的File变量名字一致 --><input type="submit" value="上传" /></form></body>3、在src下新建com.etc.action包,在包下新建UploadAction.java类:
package com.etc.action;import java.io.File;import java.io.IOException;import org.apache.commons.io.FileUtils;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport {private static final long serialVersionUID = -477184906674151401L;private File file;//定义一个File类型的变量,临时文件,需要另存到别的目录去private String fileFileName;//文件名 File变量+FileNameprivate String fileContentType;//文件上传类型 File变量+ContentType/** * Description:上传方法:将上传的文件保存在e:/upload目录下 * @return * @author zoey * @throws IOException * @date 2017年7月24日 */public String upload() throws IOException{String uploaddir = "D:/upload";new File(uploaddir).mkdirs();//如果目录不存在,就递归创建文件夹,如果已经存在,就不新建文件夹String filename = System.currentTimeMillis()+fileFileName;//避免文件名重复FileUtils.copyFile(file, new File(uploaddir,filename));return Action.SUCCESS;}public File getFile() {return file;}public void setFile(File file) {this.file = file;}public String getFileFileName() {return fileFileName;}public void setFileFileName(String fileFileName) {this.fileFileName = fileFileName;}public String getFileContentType() {return fileContentType;}public void setFileContentType(String fileContentType) {this.fileContentType = fileContentType;}}4、配置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="upload" namespace="/" extends="struts-default"><action name="upload" class="com.etc.action.UploadAction" method="upload"><result>/success.jsp</result></action></package></struts>5、在WebRoot下新建success.jsp页面如下:
<body>上传成功</body>6、运行:http://localhost:8080/struts-uploaddownload/index.jsp
上传之后,跳转到success.jsp页面,显示 上传成功 字样
注意:
struts默认只能上传大概2M左右的文件,如果上传较大的文件,就会报错,可以在struts.xml中添加常量配置,进行修改:
<constant name="struts.multipart.maxSize" value="1024000000"></constant>可以在struts2-core-2.2.1.jar下的org.apache.struts2下的/org/apache/struts2/default.properties中找到
阅读全文
0 0
- Struts2 上传文件实例
- struts2文件上传实例
- Struts2文件上传实例
- Struts2文件上传实例
- struts2 文件上传实例
- Struts2文件上传实例
- struts2上传实例
- Struts2上传图片实例
- struts2多图片上传实例
- struts2多图片上传实例
- struts2多上传实例代码
- struts2多图片上传实例
- Struts2.0实现单个文件上传实例
- Struts2完成文件的上传功能实例
- struts2的文件上传技术实例详解
- 基于Struts2上传文件实例解析
- struts2中上传文件的实例应用
- Struts2 + uploadify 多文件上传完整实例!
- java反射机制详解 及 Method.invoke解释 getMethod
- Python学习1
- Floyd,Dijkstra的详解
- get传参 有特殊字符 比如+ 会变为空格
- Linux 3.14的设备树-ARM架构-4412平台,最详细的实战开发代码(二)
- struts2上传实例
- PHP运行机制漫谈
- Mongodb集群搭建的三种方式
- HDU 1532-Drainage Ditches(网络最大流,EK,Ford-Fulkerson,dinic3种算法套模板)
- Spring 4x中的注解类使用
- 负载均衡汇总
- 支持多文件、多格式远程上传的FTP实战案例
- A
- POJ:Currency Exchange