Struts2的文件上传
将表单的method属性设置为post,将enctype设置为multipart/form-data。
将enctype设置为multippart/form-data,浏览器将采用二进制流方式处理表单数据。
Struts2的文件上传默认使用jakarta的commons-fileupload文件上传框架,
依赖commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar
①.# struts.multipart.parser=cos
②.# struts.multipart.parser=pell
③.struts.multipart.parser=jakarta
实现文件上传的Action
需求:输入标题,浏览文件,上传
若表单中文件域的name属性为xxx,则Action需要三个属性来封装该文件域信息
①.类型为File的xxx属性封装了该文件域对应的文件内容
②.类型为String的xxxFileName属性封装了该文件域对应的文件名
③.类型为String的xxxContentType属性封装了该文件域对应的文件类型
编写处理上传请求的Action类
①.private String title;
②.private File upload;
③.private String uploadContentType;
④.private String uploadFileName;
⑤.private String savePath;
Action属性的值可以通过Struts2配置文件注入,即在配置文件中为属性指定值
①.ServletActionConext.getServletContext().getRealPath(savePath);
execute: FileUtils.copy(uploadImage,new File(folder,uploadImageFileName));
配置文件上传的Action
为Action指定/images
“/>
用java.util.UUID工具类生产唯一的文件名
上传实例:
struts.xml:
"-//apache Software Foundation//DTD Struts Configuation 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
/upload
/content/succ.jsp
/content/{1}.jsp
UploadAction.java:
package cn.edu.hpu.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
//封装文件标题请求的参数
private String title;
//封装上传文件域的属性
private File upload;
//封装上传文件类型的属性
private String uploadContenttype;
//封装上传文件名的属性
private String uploadFileName;
//直接在struts.xml中配置的属性
private String savePath;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContenttype() {
return uploadContenttype;
}
public void setUploadContenttype(String uploadContenttype) {
this.uploadContenttype = uploadContenttype;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
//返回上传文件的保存地址
public String getSavePath() throws Exception{
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String execute() throws Exception {
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos=new FileOutputStream(getSavePath()+"\"+getUploadFileName());
FileInputStream fis=new FileInputStream(getUpload());
byte [] buffer=new byte[1024];
int len=0;
while((len=fis.read(buffer))>0){
fos.write(buffer,0,len);
}
return SUCCESS;
}
}
upload.jsp:
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
upload
succ.jsp:
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
upload success
上传成功!
文件标题:
文件为:"/>
上述代码实现了一个基本的上传功能
注意:
在struts.xml中配置action,然后注入初始值param,,里面有允许上传的类型。
在action类中,用get、set方法得到允许上传的类型(一般是个字符串数组),之后与上传文件的类型比较,如果有一样的,放行,如果没有,说明上传类型非法,将错误信息放在FieldError里面,类似:addFieldError(“upload”,”上传的类型不正确!”);。在Jsp文件里以显示错误提示
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net