您的位置:首页 > 编程语言 > Java开发

struts2中文件上传

2016-06-14 21:24 459 查看
注意点

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: