fileUpLoad.jsp页面
<%@ 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 'fileUpLoad.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" mce_href="styles.css">
-->
</head>
<body>
<center>
<s:form action="fileUpload" method="POST"
enctype="multipart/form-data">
<s:fielderror />
<s:file name="myFile" label="Image File1" />
<s:file name="myFile" label="Image File2" />
<s:file name="myFile" label="Image File3" />
<s:textfield name="caption" label="Caption" />
<s:submit />
</s:form>
</center>
</body>
</html>
--------------------------------------------------------------------------------------------------
FileUploadAction.java action页面
package com.ywjava.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
private static final long serialVersionUID = 572146812454l;
private static final int BUFFER_SIZE = 16 * 1024;
private List<File> myFile = new ArrayList<File>();
private List<String> contentType = new ArrayList<String>();
private List<String> fileName = new ArrayList<String>(); //文件名
private List<String> imageFileName = new ArrayList<String>();
private String caption;
private static void copy(File src, File dst) {
try {
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];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
@Override
public String execute() {
if (myFile == null)
return INPUT;
for (int i = 0; i < myFile.size(); i++) {
imageFileName.add(new Date().getTime()+".jpg") ;
File imageFile = new File(ServletActionContext.getServletContext() //得到图片保存的位置(根据root来得到图片保存的路径在tomcat下的该工程里)
.getRealPath("UploadImages")
+ "/" + imageFileName.get(i));
copy(myFile.get(i), imageFile); //把图片写入到上面设置的路径里
}
return SUCCESS;
}
public List<File> getMyFile() {
return myFile;
}
public void setMyFile(List<File> myFile) {
this.myFile = myFile;
}
public List<String> getContentType() {
return contentType;
}
public void setContentType(List<String> contentType) {
this.contentType = contentType;
}
public List<String> getMyFileFileName() {
return fileName;
}
public void setMyFileFileName(List<String> fileName) {
this.fileName = fileName;
}
public List<String> getImageFileName() {
return imageFileName;
}
public void setImageFileName(List<String> imageFileName) {
this.imageFileName = imageFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
public static int getBufferSize() {
return BUFFER_SIZE;
}
}
------------------------------------------------------------------------
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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<!-- 指定国际化资源文件的baseName为messageResource -->
<constant name="struts.custom.i18n.resources" value="messageResource" />
<!-- 设置该应用使用的解码集 -->
<constant name="struts.i18n.encoding" value="utf-8" />
<!-- 上传的全部图片的最大限制-->
<constant name="struts.multipart.maxSize" value="1024102400" />
<!-- 临时存放文件的路径 -->
<constant name="struts.multipart.saveDir" value="d:/test" />
<package name="index" namespace="/" extends="struts-default"><!--
<action name="index" class="com.ywjava.action.IndexAction">
<result>
/WEB-INF/page/fileUpLoad.jsp
</result>
</action>
-->
<action name="fileUpload" class="com.ywjava.action.FileUploadAction">
<!-- 限制图片的格式和图片的大小 -->
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/pjpeg
</param>
</interceptor-ref>
<!-- 默认的拦截器,必须要写 -->
<interceptor-ref name="defaultStack" />
<result name="input">fileUpLoad.jsp</result>
<result name="success">showUpload.jsp</result>
</action>
</package>
<!--
<constant name="struts.multipart.saveDir" value="d:/test"></constant>
-->
<!-- Add packages here -->
</struts>
------------------------------------------------------------------------
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts Blank</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
--------------------------------------------------------------------------------------------------------------------
showUpload.jsp
<%@ page language="java" import="java.util.*" 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>Show Image</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" mce_href="styles.css">
-->
</head>
<body>
<s:iterator value="imageFileName" id="im" status="length">
<div
style="padding: 3px; border: solid 1px #cccccc; text-align: center">
<img width="100px" height="80px" src='UploadImages/<s:property value ="#im" /> ' />
<br />
<s:property value="caption" />
</div>
</s:iterator>
<s:property value ="caption" />
</body>
</html>