Action类——
package com.upload;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.upload3.*;
import com.upload4.Upbean;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.upload2.Conn;
public class Uploads extends ActionSupport{
private List list;
private File upload;
private String uploadFileName;
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
private String uploadContextType;
private String savePath;
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getUploadContextType() {
return uploadContextType;
}
public void setUploadContextType(String uploadContextType) {
this.uploadContextType = uploadContextType;
}
public String getSavePath() {
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
private String uname;
private String pic;
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
@Override
public String execute() throws Exception {
String filename=this.getSavePath()+"\\"+this.getUploadFileName();
String pic="save"+"/"+this.getUploadFileName();
System.out.print(pic);
FileOutputStream fos=new FileOutputStream(filename);
FileInputStream fis=new FileInputStream(this.getUpload());
byte[] b=new byte[1024];
int len=0;
while((len=fis.read(b))>0){
fos.write(b,0,len);
}
Upbean u=new Upbean();
u.add(uname, pic);
return SUCCESS;
}
public String all(){
Upbean u=new Upbean();
list=u.All();
return SUCCESS;
}
}
Struts.xml——
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.multipart.saveDir" value="/tmp"/>
<package name="struts2" extends="struts-default">
<action name="all" class="com.upload.Uploads">
<interceptor-ref name="fileUpload">
<param name="allowedType">image/pgn,image/gif,image/jpg</param>
<param name="maximumSize">90000000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<param name="savePath">/save</param>
<result name="success">/Succ.jsp</result>
<result name="input">/MyJsp.jsp</result>
</action>
<action name="upload" class="com.upload.Uploads" method="all">
<result name="success">/My.jsp</result>
</action>
</package>
</struts>
上传页面
<%@ 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 'MyJsp.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>
<form action="all.action" method="post" enctype="multipart/form-data">
<table>
<tr><td>姓名</td><td><input type="text" name="uname"/></td></tr>
<tr><td>上传</td><td><input type="file" name="upload"/></td></tr>
</table>
<input type="submit" value="Login"/>
</form>
</body>
</html>
显示上传的图片
<%@ page language="java" import="java.util.*,com.upload3.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
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 'My.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>
<s:iterator value="list">
<table>
<tr><td>姓名</td><td><s:property value="uname"/><br></td></tr>
<tr><td>图片</td><td><img src="<s:property value='pic'/>"width="100" height="100" /></td></tr>
</table>
</s:iterator>
</body>
</html>