Struts 2 之文件上传
如果要获得上传文件的原始名称,需要定义一个String类型的属性,属性名必须为***FileName,其中***为File属性的名称;同理,如果要获取该文件的MIME类型,需要定义一个***ContentType的String属性
单个文件上传
public class UploadAction extends ActionSupport{ private File image; //上传的文件 private String imageFileName; //文件名称 private String imageContentType; //文件类型 public String execute() throws Exception { String realpath =ServletActionContext.getServletContext().getRealPath("/images"); FileOutputStream fos = null; FileInputStream fis = null; try { // 建立文件输出流 System.out.println(getSavePath()); fos = new FileOutputStream(realpath+ "\\" + getImageFileName()); // 建立文件上传流 fis = newFileInputStream(getImage()); byte[] buffer = new byte[1024]; int len = 0; while ((len = fis.read(buffer))> 0) { fos.write(buffer, 0, len); } } catch (Exception e) { System.out.println("文件上传失败"); e.printStackTrace(); } finally { close(fos, fis); } return SUCCESS; } public File getImage() { return image; } public void setImage(File image) { this.image = image; } public String getImageFileName() { return imageFileName; } public void setImageFileName(StringimageFileName) { this.imageFileName = imageFileName; } public String getImageContentType() { return imageContentType; } public void setImageContentType(StringimageContentType) { this.imageContentType = imageContentType; } }多个文件上传
import java.io.File; import java.io.IOException; import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class TagUploadListAction extends ActionSupport { private static final long serialVersionUID= 1L; private String name; // 上传多个文件的集合文本 private List<File> upload; // /多个上传文件的类型集合 private List<String>uploadContextType; // 多个上传文件的文件名集合 private List<String> uploadFileName; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<File> getUpload() { return upload; } public void setUpload(List<File>upload) { this.upload = upload; } public List<String>getUploadContextType() { return uploadContextType; } public voidsetUploadContextType(List<String> uploadContextType) { this.uploadContextType =uploadContextType; } public List<String>getUploadFileName() { return uploadFileName; } public voidsetUploadFileName(List<String> uploadFileName) { this.uploadFileName = uploadFileName; } public String execute() { // 把上传的文件放到指定的路径下 String path =ServletActionContext.getServletContext().getRealPath( "/WEB-INF/uploadList"); // 写到指定的路径中 File file = new File(path); // 如果指定的路径没有就创建 if(!file.exists()) { file.mkdirs(); } // 把得到的文件的集合通过循环的方式读取并放在指定的路径下 for (int i = 0; i < upload.size();i++) { try { //list集合通过get(i)的方式来获取索引 FileUtils.copyFile(upload.get(i), new File(file,uploadFileName.get(i))); } catch (IOException e) { // TODO Auto-generated catchblock e.printStackTrace(); } } return SUCCESS; } }
Struts 2 之文件上传的更多相关文章
- Struts学习之文件上传
* 单文件上传: * 在动作类action中声明相关属性: * 在动作类action中,要声明与页面中表单name属性同名的属性,同名的属性的类型是File类型: ...
- Struts 框架 之 文件上传下载案例
Struts 框架 文件上传 1. 先准备 Struts 环境 (我使用的是struts 2.3.4版本) 导jar包:
- Struts多个文件上传
Struts2多个文件上传 10级学员 韩晓爽课堂笔记 多个文件上传分为List集合和数组,下面我们着重介绍一下list集合的上传.都大同小异. 一 介绍 1. 在struts2文件上传的时候要先导入 ...
- Struts 1 之文件上传
Struts 1 对Apache的commons-fileupload进行了再封装,把上传文件封装成FormFile对象 定义UploadForm: private FormFilefile; //上 ...
- Java Struts文件上传和下载详解
Struts2文件上传 Struts 2框架提供了内置支持处理文件上传使用基于HTML表单的文件上传.上传一个文件时,它通常会被存储在一个临时目录中,他们应该由Action类进行处理或移动到一个永久的 ...
- 转:在Struts 2中实现文件上传
(本文转自:http://www.blogjava.net/max/archive/2007/03/21/105124.html) 前一阵子有些朋友在电子邮件中问关于Struts 2实现文件上传的问题 ...
- java框架篇---struts之文件上传和下载
Struts2文件上传 Struts 2框架提供了内置支持处理文件上传使用基于HTML表单的文件上传.上传一个文件时,它通常会被存储在一个临时目录中,他们应该由Action类进行处理或移动到一个永久的 ...
- Struts 2(八):文件上传
第一节 基于Struts 2完成文件上传 Struts 2框架中没有提供文件上传,而是通过Common-FileUpload框架或COS框架来实现的,Struts 2在原有上传框架的基础上进行了进一步 ...
- 使用commons-fileupload包进行大文件上传注意事项
项目中使用 commons-fileupload-1.2.1.jar 进行大文件上传. 测试了一把,效果很不错. 总结如下: 必须设置好上传文件的最大阀值 final long MAX_SIZE = ...
随机推荐
- 以技术面试官的经验分享毕业生和初级程序员通过面试的技巧(Java后端方向)
本来想分享毕业生和初级程序员如何进大公司的经验,但后来一想,人各有志,有程序员或许想进成长型或创业型公司或其它类型的公司,所以就干脆来分享些提升技能和通过面试的技巧,技巧我讲,公司你选,两厢便利. 毕 ...
- [CQOI2013]新Nim游戏
Description 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作,每次可以选一个火柴堆拿走若干根火柴.可以只拿一根,也可以拿走整堆火柴 ...
- 李耀于NOIP2010集训出的题 Dvalue
此题模型比较明显,求无向图的一棵生成树,使得最大边减去最小边的值最小,这是最小生成树的一个变式 设计出此题的算法需要利用Kruskal贪心的性质,首先枚举一条最小边,接着求原图的一棵最小生成树,根据k ...
- ●BZOJ 4596 [Shoi2016]黑暗前的幻想乡
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4596 题解: 容斥,矩阵树定理,矩阵行列式 先说说容斥:(一共有 N-1个公司) 令 f[i ...
- 计蒜客NOIP2017提高组模拟赛(三)day2-小区划分
传送门 dp,注意边界 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cst ...
- k-d tree模板练习
1. [BZOJ]1941: [Sdoi2010]Hide and Seek 题目大意:给出n个二维平面上的点,一个点的权值是它到其他点的最长距离减最短距离,距离为曼哈顿距离,求最小权值.(n< ...
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- hzoj 2301(莫比乌斯反演)
题意 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公 数. 思路: 与先前的那个相比,这次a,c并不一定 ...
- hdu 5583 Kingdom of Black and White
Kingdom of Black and White Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- java的泛型
泛型概述 先看下面的代码: ArrayList al1 = new ArrayList(); ArrayList al2 = new ArrayList(); al1.add("hello& ...