struts2 文件上传

来源:互联网 发布:中国家具出口数据 编辑:程序博客网 时间:2024/06/03 17:01

新建包com.action,在包中新建类FileUploadAction,类代码如下:

package com.action;import java.io.File;import com.opensymphony.xwork2.ActionSupport;public class FileUploadAction extends ActionSupport{private static final long serialVersionUID = 1L;private File uploadFile;private String uploadFileFileName;private String uploadFileContentType;public File getUploadFile() {return uploadFile;}public void setUploadFile(File uploadFile) {this.uploadFile = uploadFile;}public String getUploadFileFileName() {return uploadFileFileName;}public void setUploadFileFileName(String uploadFileFileName) {this.uploadFileFileName = uploadFileFileName;}public String getUploadFileContentType() {return uploadFileContentType;}public void setUploadFileContentType(String uploadFileContentType) {this.uploadFileContentType = uploadFileContentType;}@Overridepublic String execute() throws Exception {if(uploadFile!=null){String dataDir="d:\\";//上传文件存放的目录File saveFile=new File(dataDir,uploadFileFileName);//上传文件在服务器具体的位置uploadFile.renameTo(saveFile);//将上传文件从临时文件复制到指定文件}else{return INPUT;}return SUCCESS;}}
在src目录下新建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><package name="Struts2_Ajax_Demo" extends="struts-default" namespace="/" ><action name="fileupload" class="com.action.FileUploadAction"><result>/success.jsp</result><interceptor-ref name="defaultStack"><param name="fileUpload.maximumSize">100000000</param><param name="fileUpload.allowedTypesSet">image/jpg,image/jpeg,image/png</param></interceptor-ref></action></package></struts>

在WebContent/WEB-INF下新建web.xml文件,代码如下:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">  <display-name>internalDemo</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.jsp</welcome-file></welcome-file-list></web-app>

在WebContent目录下新建index.jsp文件,代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>    <%@taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><center><s:form action="fileupload.action" enctype="multipart/form-data" mehtod="post"><s:file name="uploadFile" label="选择文件"></s:file><s:submit></s:submit></s:form></center></body></html>

新建success.jsp文件如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>      <%@taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><center><h2>文件名:<s:property value="uploadFileFileName"/><br/>文件类型:<s:property value="uploadFileContentType"/></h2></center></body></html>




0 0
原创粉丝点击