struts2中文件上传
2016-06-14 21:24
459 查看
注意点
单个文件
jsp中
多个文件上传
jsp中
struts.xml中配置
设置文件上传大小
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>
相关文章推荐
- 【SSM学习之路】Maven安装配置和环境搭建(eclipse)
- MyEclipse的输入提示配置
- ArrayList简介
- Vector简介
- Struts2单文件上传和多文件上传两种类型
- java-设计模式
- Java对象初始化
- 报javax.servlet.ServletException: Servlet.init() for servlet springmvc threw exception异常
- Java千百问_04异常处理(006)_常见的Java异常有哪些(运行时)
- eclipse连接SqlServer2008(被它搞得惨兮兮)
- Java多种方式自定义序列化
- java反射
- Struts2的文件上传与下载
- Struts2下载文件时的Action的配置
- java排序算法 堆排序
- LinkedList简介
- IO-缓冲流的使用。
- SpringMVC学习--数据回显
- java的mysql api封装
- spring错误汇总