struts2详解(二)---->>单个文件上传
实现原理:
Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的文件。
具体实现
先要引入必要的jar包:
1、首先,创建文件上传页面index.jsp,内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <div align="center"> <!-- enctype="multipart/form-data" 这个属性是必须得有的 --> <form action="csdn/upload.action" method="post" enctype="multipart/form-data"> <table border="1px"> <tr> <td> 标题: </td> <td> <input type="text" name="title" /> </td> </tr> <tr> <td> 文件: </td> <td> <input type="file" name="pic" /> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="上传" /> </td> </tr> </table> </form> </div> </body> </html>
备注:
在index.jsp中,先将表单的提交方式设为POST,然后将enctype设为multipart/form-data
2、其次是UserAction.java代码
package cn.csdn.struts2.action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
/**
* @author z_xiaofei168
*/
private static final long serialVersionUID = 1L;
/** 标题 */
private String title;
/** 获取文件 文件名与视图层的名称一致 */
private File pic;
/** 获取文件的类型 必须是: 文件名+ContentType */
private String picContentType;
/** 获取文件的名称 必须是: 文件名+FileName */
private String picFileName;
/** 保存文件路径 /保存文件的文件名称 */
private String savePath;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getPic() {
return pic;
}
public void setPic(File pic) {
this.pic = pic;
}
public String getPicContentType() {
return picContentType;
}
public void setPicContentType(String picContentType) {
this.picContentType = picContentType;
}
public String getPicFileName() {
return picFileName;
}
public void setPicFileName(String picFileName) {
this.picFileName = picFileName;
}
public String getSavePath() {
/**获取上下的路径*/
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
/** copy读取 */
public String uploadFile(){
System.out.println("savePath:"+savePath);
System.out.println("pic:"+pic);
System.out.println("picContentType:"+picContentType);
System.out.println("picFileName:"+picFileName);
System.out.println("getSavePath():"+getSavePath());
/**保存的具体路径*/
String savepath = getSavePath();
/**根据保存的路径创建file对象*/
File file = new File(savepath);
if(!file.exists()){
/**创建此文件对象路径*/
file.mkdirs();
}
try {
/**使用的是:org.apache.commons.io.FileUtils FileUtils*/
FileUtils.copyFile(pic, new File(file,getPicFileName()));
} catch (IOException e) {
e.printStackTrace();
}
return SUCCESS;
}
}
3、配置文件: struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <package name="csdn" extends="struts-default" namespace="/csdn"> <global-results> <result name="input">/index.jsp</result> </global-results> <action name="upload" class="cn.csdn.struts2.action.UserAction" method="uploadFile"> <!-- 文件保存的文件名称 --> <param name="savePath">/uploads</param> <result name="success">/sc.jsp</result> </action> </package> </struts>
4、下面我们就来看看上传成功的页面: