使用struts2 编写上传代码
(一)编写UploadAction类
package com.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class UploadAction extends ActionSupport {
private String title; //标题
private File upload; //上传的文件
private String uploadContentType; //文件的类型
private String uploadFileName; //上传文件的名称
private String savePath; //要保存的路径
@Override
public String execute() throws Exception {
//以服务器文件保存地址和源文件建立上传输出流(到哪里去)
FileOutputStream fos=new FileOutputStream(this.getSavePath()+"\\"+this.getUploadFileName());
//以上传文件建立文件上传流(从哪里来)
FileInputStream fis=new FileInputStream(this.getUpload());
//将上传文件写到服务器
byte [] buffer =new byte[1024];
int len=0;
while((len=fis.read(buffer))>0)
{
fos.write(buffer,0,len);
}
fos.close();// 注意:流应当关闭。
fis.close();
return SUCCESS;
}
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() {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
//注入struts配置的属性
@SuppressWarnings("deprecation")
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
(二)在struts.xml中写入:
<action name="upload" class="com.action.UploadAction" method="execute">
<!-- 配置fileUpload 拦截器-->
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/jpg
</param>
<!-- 配置上传文件的大小 -->
<param name="maximumSize">500000</param>
</interceptor-ref>
<!-- 配置系统默认拦截器 -->
<interceptor-ref name="defaultStack"/>
<!-- 动态设置Action 的属性 上传文件的默认保存路径 -->
<param name="savePath">/upload</param>
<result name="success">/succ.jsp</result>
</action>
(三) 编写上传页面 upload.jsp
<body>
<form action="upload.action" method="post" enctype="multipart/form-data">
文件标题:<input type="text" name="title"/><br/>
选择文件:<input type="file" name="upload"/><br/>
<input type="submit" value="上传"/>
</form>
</body>
(四)编写显示页面 succ.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'success.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">
</head>
<body>
文件标题:<s:property value=" + title"/>
文件为:<img src="<s:property value="'upload/'+uploadFileName"/>"/><br/>
HELLO NIHAO</body>
</html>