1.上传的jsp页面upFile.jsp
<form action="fileUpload.action" method="POST" enctype="multipart/form-data">
文件标题:<input type="text" name="title" size="50"/><br/>
选择文件:<input type="file" name="upload" size="50"/><br/>
<input type="submit" value=" 上传 "/>
</form>
2.创建处理上传请求的Action类
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
private static final int BUFFER_SIZE = 16 * 1024;
// 文件标题
private String title;
// 上传文件域对象
private File upload;
// 上传文件名
private String uploadFileName;
// 上传文件类型
private String uploadContentType;
// 保存文件的目录路径(通过依赖注入)
private String savePath;
//服务器保存的文件名
private String destFile;
//以下省略getter和setter......
//自己封装的一个把源文件对象复制成目标文件对象
private static void copy(File src, File dst) {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private String getExtention(String fileName){
int pos = fileName.lastIndexOf(".");
String str = fileName.substring(pos);
return str;
}
@Override
public String execute() throws Exception {
//设置上传后服务器保存的名字
destFile = new Date().getTime()+getExtention(this.getUploadFileName());
//根据服务器的文件保存地址和原文件名创建目录文件全路径
String dstPath = ServletActionContext.getServletContext()
.getRealPath(this.getSavePath())
+ "//" + destFile;
System.out.println("上传的文件的类型:"+ this.getUploadContentType());
File dstFile = new File(dstPath);
copy(this.upload, dstFile);
return SUCCESS;
}
}
<action name ="fileUpload" class ="com.dyp.struts2.action.FileUploadAction">
<interceptor-ref name="fileUpload">
<!-- 配置允许上传的文件类型,多个用","分隔 -->
<param name="allowedTypes">
image/png,image/gif,image/jpeg,image/jpg,image/x-png, image/pjpeg
</param>
<!-- 配置允许上传的文件大小,单位字节 -->
<param name="maximumSize">102400</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<!-- 动态设置Action中的savePath属性的值 -->
<param name="savePath">/upload</param>
<result name="input">/upFile.jsp</result>
<result name ="success" >/showUpload.jsp</result>
</action>
private File[] upload;
// 上传文件名
private String[] uploadFileName;
// 上传文件类型
private String[] uploadContentType;
// 保存文件的目录路径(通过依赖注入)
private String savePath;
//服务器保存的文件名
private String[] destFile;
this.setDestFile(new String[upload.length]);//初始化否则空指针
for(int i=0;i<upload.length;i++){
//设置上传后服务器保存的名字
this.getDestFile()[i] = new Date().getTime()+getExtention(this.getUploadFileName()[i]);
//根据服务器的文件保存地址和原文件名创建目录文件全路径
String dstPath = ServletActionContext.getServletContext()
.getRealPath(this.getSavePath())
+ "//" + this.getDestFile()[i];
System.out.println("上传的文件的类型:"+ this.getUploadContentType()[i]);
File dstFile = new File(dstPath);
copy(this.upload[i], dstFile);
}
return SUCCESS;
}