标签:struts-上传
一、创建项目
项目名称:demoupload
二、添加jar包
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.5.jar
struts2-core-2.3.4.1.jar
xwork-core-2.3.4.1.jar
三、在web.xml文件中配置过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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.jsp</welcome-file>
</welcome-file-list>
</web-app>
四、在WebRoot目录下创建upload目录用于存放上传文件
/upload
五、添加核心配置文件
1.在项目中创建conf目录
/conf
2.在conf目录下添加配置文件
配置文件名称:struts.xml
配置文件内容:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<!-- 配置全局文件大小 -->
<constant name="struts.multipart.maxSize" value="21851335"></constant>
</struts>
六、在WEB-INF下创建pages目录
/pages
在pages目录下创建页面
/pages
/error.jsp
/success.jsp
七、创建Action
1.在src目录下创建包
cn.jbit.demoupload.web.action
2.在包下创建Action
Action名称:UploadAction.java
Action内容:
public class UploadAction extends ActionSupport {
private File upload;//文件对象
private String uploadContentType;//文件类型
private String uploadFileName;//文件名称
public String saveFile(){
ServletContext servletContext = ServletActionContext.getServletContext();
String path = servletContext.getRealPath("/upload");
System.out.println(path+"路径");
File newFile = new File(path,uploadFileName);
try {
FileUtils.copyFile(upload, newFile);
} catch (IOException e) {
e.printStackTrace();
}
return SUCCESS;
}
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;
}
}
3.在struts核心配置文件中添加配置
<package name="default" namespace="/" extends="struts-default">
<action name="*_uploadAction" class="cn.jbit.demoupload.web.action.UploadAction" method="{1}">
<interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">20480</param>
<param name="fileUpload.allowedTypes">image/pjpeg,image/gif</param>
<param name="fileUpload.allowedExtensions">jpg,gif</param>
</interceptor-ref>
<result name="success">/WEB-INF/pages/success.jsp</result>
<result name="input">/WEB-INF/pages/error.jsp</result>
</action>
</package>
八、创建页面
页面名称: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">
</head>
<body>
<form action="${pageContext.request.contextPath }/saveFile_uploadAction.action" method="post" enctype="multipart/form-data">
上传文件:
<input type="file" name="upload"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
本文出自 “素颜” 博客,请务必保留此出处http://suyanzhu.blog.51cto.com/8050189/1564066
标签:struts-上传
原文地址:http://suyanzhu.blog.51cto.com/8050189/1564066