在struts2中的文件上传就已经做的非常简单了,只要按照规定的要求配置和命名,就可以做到文件上传。以下是客户端的部分代码:(注:红色字体为重要部分,需要注意)
<form__ action="${pageContext.request.contextPath}/ttt/uploadfile.action" method="post" enctype="multipart/form-data" >
<div__ name="div1" style="height: 100; widows: 70;"><img src="#" id="img" height="100%" width="100%"/></div><br/>
姓名:<input__ type="text" name="username"/><br/>
密码:<input__ type="password" name="userpwd"/><br/>
<input__ type="file" name="uplordfile" value="" οnchange="imgchange(this)"/>
<input__ type="submit" value="上传">
</form__>
说明:红色字体的uplordfile可以任意起,只要在对应的action中有对应的名称和符合命名规则就可以。
下面是服务器端的代码:(Action)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadFile extends ActionSupport{
//上传的文件,name必须与页面的name属性一致
private File uplordfile;
//上传问件的类型必须为xxxContentType,而xxx必须和File的name一致
private String uplordfileContentType;
//上传文件的名称,格式必须为xxxFileName; 而xxx必须和File的name一致
private String uplordfileFileName;
//上传文件的保存路径
private String savePath;
public File getUplordfile() {
return uplordfile;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
private String username;
private String userpwd;
public void setUplordfile(File uplordfile) {
this.uplordfile = uplordfile;
}
public String getUplordfileContentType() {
return uplordfileContentType;
}
public void setUplordfileContentType(String uplordfileContentType) {
this.uplordfileContentType = uplordfileContentType;
}
public String getUplordfileFileName() {
//在这里可以给文件重命名
String filetype=uplordfileFileName.substring(uplordfileFileName.indexOf("."));
String datatype="yyyyMMddHHmmss";
SimpleDateFormat sdf=new SimpleDateFormat(datatype);
String datastr=sdf.format(new Date());
this.setUplordfileFileName(datastr+filetype);
return uplordfileFileName;
}
public void setUplordfileFileName(String uplordfileFileName) {
this.uplordfileFileName = uplordfileFileName;
}
public String getSavePath() {
//以下是若没有文件目录,则创建一个并返回
//在struts2中,获取文件的根目录为ServletActionContext.getServletContext().getRealPath("/"),因为savePath为配置的目录,所以可以直接使用
File file=new File(ServletActionContext.getServletContext().getRealPath(savePath));
if(!file.exists()){
boolean createfile=file.mkdir();
}
return file.toString();
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
//上传文件的方法
@Override
public String execute() throws Exception {
System.out.println(getSavePath());
if(this.getUsername().equals("admin")&&this.getUserpwd().equals("123")){
FileOutputStream fos=new FileOutputStream(getSavePath()+"/"+getUplordfileFileName());
FileInputStream fis=new FileInputStream(getUplordfile());
byte[] buff=new byte[1024];
int count=0;
while((count=fis.read(buff))>0){
fos.write(buff, 0, count);
}
return SUCCESS;
}else{
return ERROR;
}
}
}
此段代码,我已经加了注释,并且加了如何按照自己的要求重命名和若保存的路径不存在,如何让程序自动创建,这样不论工程拷贝到哪里都可以使用。
下面是配置信息:
<constant name="struts.multipart.parser" value="cos"></constant>
<!--上传文件的配置文件-->
<action name="uploadfile" class="com.defu.com.UploadFile">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/bmp,image/jpg,image/png,image/gif,image/pjpeg,application/zip,audio/mpeg</param>
<param name="maximumSize">100000000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
<param name="savePath">/uploads</param>
<result name="success">/succ.jsp</result>
<result name="input">/index.jsp</result>
<result name="error">/error.jsp</result>
</action>
说明:<constant name="struts.multipart.parser" value="cos"></constant>
常量不能少。红色加粗的fileUpload为固定写法,struts2的文件上传拦截器。红色加粗的/uploads为文件的保存路径。以后若已经编译在服务器端运行了,可以在tomcat容器的该工程下面的配置文件中直接改变路径即可!