注意点

     private File image;//对应的就是表单中文件上传的那个输入域的名称,Struts2框架会封装成File类型的
private String imageFileName;// 上传输入域FileName 文件名
private String imageContentType;// 上传文件的MIME类型

单个文件

 package cn.itcast.action;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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 UploadAction1 extends ActionSupport implements Serializable { private File image;//对应的就是表单中文件上传的那个输入域的名称,Struts2框架会封装成File类型的
private String imageFileName;// 上传输入域FileName 文件名
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(){
System.out.println(imageContentType);
try {
//处理实际的上传代码
//找到存储文件的真实路径
// System.out.println(imageFileName);
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);
// }
// out.close();
// in.close(); FileUtils.copyFile(image, new File(storePath,imageFileName)); ActionContext.getContext().put("message", "上传成功!");
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
}
}

jsp中

   <body>
<form action="${pageContext.request.contextPath}/upload/upload1.action" method="post" enctype="multipart/form-data">
文件:<input type="file" name="image"/><br/>
<input type="submit" value="上传"/>
</form>
</body>

多个文件上传

 package cn.itcast.action;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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 UploadAction2 extends ActionSupport implements Serializable { private File[] images;//对应的就是表单中文件上传的那个输入域的名称,Struts2框架会封装成File类型的
private String[] imagesFileName;// 上传输入域FileName 文件名
private String[] imagesContentType;// 上传文件的MIME类型 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 execute(){
try { if(images!=null&&images.length>0){
ServletContext sc = ServletActionContext.getServletContext();
String storePath = sc.getRealPath("/files");
for(int i=0;i<images.length;i++)
FileUtils.copyFile(images[i], new File(storePath,imagesFileName[i]));
}
ActionContext.getContext().put("message", "上传成功!");
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
}
}

jsp中

   <body>
<form action="${pageContext.request.contextPath}/upload/upload2.action" method="post" enctype="multipart/form-data">
文件1:<input type="file" name="images"/><br/>
文件2:<input type="file" name="images"/><br/>
<input type="submit" value="上传"/>
</form>
</body>

struts.xml中配置

设置文件上传大小

     <constant name="struts.multipart.maxSize" value="52428800"></constant>
     <package name="upload" namespace="/upload" extends="mypackage">
<action name="upload1" class="cn.itcast.action.UploadAction1" method="execute">
<result name="success">/success.jsp</result>
</action>
<action name="upload2" class="cn.itcast.action.UploadAction2" method="execute">
<result name="success">/success.jsp</result>
</action>
</package>
 
 
 

最新文章

  1. 扩展RadioButtonListFor和CheckBoxListFor
  2. linux学习网站分享
  3. ubuntu包管理
  4. android学习笔记12——ListView、ListActivity
  5. [百度空间] [转]程序员趣味读物:谈谈Unicode编码
  6. 第三百四十六天 how can I 坚持
  7. 查看大图 zoomImage
  8. 【转】JAVA程序中Float和Double精度丢失问题
  9. 怎样获取HTML5视频的持续时间
  10. Java并发编程:Lock(锁)
  11. PostgreSQL安装和使用
  12. Python3 与 C# 并发编程之~ Net篇
  13. 开始使用Reflection
  14. gperftools 使用经验总结
  15. 反接保护电路 Reverse Voltage Protection
  16. 学会从后往前遍历,例 [LeetCode] Pascal&#39;s Triangle II,剑指Offer 题4
  17. No module named &#39;MySQLdb&#39; python3.6 + django 1.10 + mysql 无法连接
  18. 〖Linux〗录像桌面视频同时录音
  19. python--标准库 时间与日期 (time, datetime包)
  20. Codeforces 723 A. The New Year: Meeting Friends

热门文章

  1. jquery获取iframe里的元素
  2. 【BZOJ 3993】【SDOI 2015】序列统计
  3. 【转】python assert用法
  4. 2017 icpc 西安网络赛
  5. [拒绝毒瘤的小清新系列] give you a tree
  6. 【构造】CDOJ1607 大学生足球联赛
  7. 【分类讨论】【set】Codeforces Round #407 (Div. 2) B. Masha and geometric depression
  8. python基础之类与对象,继承与派生
  9. NHibernate 操作视图 第十三篇
  10. 一个简单的MVC模式练习