struts2示例代码--提交文件
来源:互联网 发布:iphone离线看书软件 编辑:程序博客网 时间:2024/06/05 02:59
jar包:commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar
//${pageContext.request.contextPath}根目录
<form action="${pageContext.request.contextPath}/test/helloworld" enctype="multipart/form-data" method="post"> //表单中属性要加上enctype="multipart/form-data"
<input type="file" name="image">
<input type="submit" value="ok">
</form>
package cn.action;
import java.io.File;import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public class HelloWordAction {
private File image; //文件名称,与之前的表单中的<input type=“file” name="image" >的name相对应
private String imageFileName; //文件名称后+FileName表示上传文件的名称
private String imageContentType;//文件名称后+ContentType表示上传文件的类型
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}
public String execute() throws Exception{
String path=ServletActionContext.getServletContext().getRealPath("/images");//获取“/images”文件夹的真实路径
System.out.println(path);
if(image!=null){ //判断上传文件是否存在
File savepath=new File(new File(path),imageFileName );//保存imageFileName到path目录底下
if(!savepath.getParentFile().exists()) //判断文件夹是否存在
savepath.getParentFile().mkdirs(); //创建文件夹
FileUtils.copyFile(image, savepath); //把image文件转存到savepath路径下,如果看不到FileUtils方法是因为没有导//包commons-io-1.3.2.jar
System.out.println("保存成功");
ActionContext.getContext().put("aa", "保存成功");//给request范围赋值
}
return "success";
}
}
如果上传文件超过2MB,需要到struts2.xml中加上:<constant name="struts.multipart.maxSize" value="10701096"/>//value里设置文件上传大小
0 0
- struts2示例代码--提交文件
- struts2示例代码-多个文件上传
- 快捷键提交示例代码
- Struts2+Jquery.uploadify实现的文件上传特效示例代码
- struts2----一个通用的文件下载代码示例
- Struts2上传文件示例
- struts2上传文件示例
- struts2下载文件示例
- Struts2上传文件示例
- struts2文件下载示例
- aJax 表单提交代码示例
- struts2示例代码-struts2的配置
- git提交文件代码
- Struts2上传文件示例 【转】
- Struts2上传文件示例 【转】
- struts2文件上传简单示例
- 关于回车键提交的示例代码
- ajax提交加载进度条示例代码
- SAP web dynpro for abap 环境配置
- C语言:关键字static的作用
- 开心的金明(dp 01背包)
- c# 树莓派扫描器
- 【大数类模板】hdoj 4927 Series 1
- struts2示例代码--提交文件
- calculate_dvbt_system_ofdm_output_float_scale_factor
- Linux Mint 17 V2版中文化与输入法
- 第十七天-JavaAPI(Collections、Arrays,静态导入)
- ym——Android从零开始(20)(TabHost+ExpandableListView)(新)
- java 自动拆箱与装箱
- jsp中的路径问题
- LabVIEW串口操作--labview高亮单步显示时串口缓冲区能够正常接收,直接执行时不能接收
- python使用MySQLdb,往库表内添加中文字段要做的修改