Struts2文件上传
来源:互联网 发布:反抄袭软件调色盘 编辑:程序博客网 时间:2024/06/11 02:52
单文件上传:
package com.structs2;
import java.io.File;
import java.io.IOException;
import javax.servlet.jsp.PageContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public class HelloWordAction {
private File image;
private String imageFileName;
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 execute() throws IOException {
//单文件上传
String realpath = ServletActionContext.getServletContext().getRealPath("/update");
System.out.println(realpath);
if(image!=null)
{
File savefile = new File(new File(realpath),imageFileName);
if(!savefile.getParentFile().exists())
savefile.getParentFile().mkdirs();
FileUtils.copyFile(image, savefile);
//下边两句必须放在if语句里面
ActionContext ctx = ActionContext.getContext();
ctx.put("message", "上传成功");
}
return "success";
}
}
多文件上传:
package com.structs2;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public class HelloAction {
private File[] image;//可以是数组类型,也可以是List类型
private String[] imageFileName;//可以是数组类型,也可以是List类型
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 execute() throws IOException{
//多文件上传
String realpath = ServletActionContext.getServletContext().getRealPath("/update");
System.out.println(realpath);
if(image!=null)
{
File pfile = new File(realpath);
if(!pfile.exists())//判断如果文件夹不存在,那么就创建文件夹,带有目录的文件夹
pfile.mkdirs();
for(int i=0;i<image.length;i++)
{
File savefile = new File(pfile,imageFileName[i]);
FileUtils.copyFile(image[i], savefile);
}
//下边两句必须放在if语句里面
ActionContext ctx = ActionContext.getContext();
ctx.put("message", "上传成功");
}
return "success";
}
}
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>
<!-- 上传文件限制大小,是同时上传文件的总大小,上传三个文件,就是三个文件的总大小 -->
<constant name="struts.multipart.maxSize" value="10701096"/>
<package name="default" namespace="/test" extends="struts-default">
<action name="file" class="com.structs2.HelloWordAction"
method="execute">
<result name="success" >/hello.jsp</result>
</action>
<action name="files" class="com.structs2.HelloAction"
method="execute">
<result name="success" >/hello.jsp</result>
</action>
</package>
</struts>
package com.structs2;
import java.io.File;
import java.io.IOException;
import javax.servlet.jsp.PageContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public class HelloWordAction {
private String imageFileName;
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 execute() throws IOException {
//单文件上传
String realpath = ServletActionContext.getServletContext().getRealPath("/update");
System.out.println(realpath);
if(image!=null)
{
File savefile = new File(new File(realpath),imageFileName);
if(!savefile.getParentFile().exists())
savefile.getParentFile().mkdirs();
FileUtils.copyFile(image, savefile);
//下边两句必须放在if语句里面
ActionContext ctx = ActionContext.getContext();
ctx.put("message", "上传成功");
}
return "success";
}
}
多文件上传:
package com.structs2;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public class HelloAction {
private File[] image;//可以是数组类型,也可以是List类型
private String[] imageFileName;//可以是数组类型,也可以是List类型
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 execute() throws IOException{
//多文件上传
String realpath = ServletActionContext.getServletContext().getRealPath("/update");
System.out.println(realpath);
if(image!=null)
{
File pfile = new File(realpath);
if(!pfile.exists())//判断如果文件夹不存在,那么就创建文件夹,带有目录的文件夹
pfile.mkdirs();
for(int i=0;i<image.length;i++)
{
File savefile = new File(pfile,imageFileName[i]);
FileUtils.copyFile(image[i], savefile);
}
//下边两句必须放在if语句里面
ActionContext ctx = ActionContext.getContext();
ctx.put("message", "上传成功");
}
return "success";
}
}
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>
<!-- 上传文件限制大小,是同时上传文件的总大小,上传三个文件,就是三个文件的总大小 -->
<constant name="struts.multipart.maxSize" value="10701096"/>
<package name="default" namespace="/test" extends="struts-default">
<action name="file" class="com.structs2.HelloWordAction"
method="execute">
<result name="success" >/hello.jsp</result>
</action>
<action name="files" class="com.structs2.HelloAction"
method="execute">
<result name="success" >/hello.jsp</result>
</action>
</package>
</struts>