【Java Web】【1】传统form表单上传file文件,struts后台接受

1.搭建简单的struts2框架
 --->src的下struts.xml
<struts>
		<!-- 配置动态方法调用是否开启常量默认是关闭的,需要开启-->
	<constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
		<package name="company" extends="struts-default" >
			<!-- 动态方法调用方式2:通配符方式使用{1} 取出第一个星号通配的内容-->
			<action name="test_*" class="com.test.action.formAction" method="{1}" >
			</action>
	</package>
</struts>

 --->根目录下struts.xml


--->jar包导入

--->web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
	id="WebApp_ID" version="2.5">
  <display-name>TestForm</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>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>
2.前台页面:Hellow.jsp
<form enctype="multipart/form-data" method="POST" action="${pageContext.request.contextPath}/formAction/test_testFile.action" id="uploadForm"> 
	<table>
		<tr>
			<td>
				<input type="file" style="width:240px;" name="file"/>
			</td>
		<td id="fileNameError" class="error"></td>
		</tr>
		<tr>
			<td>
				<input id="submitBtn1" type="submit" value="确定" style="padding:3px 25px;"/>
			</td>
		</tr>				     
	</table>
</form> 

3.action配置
package com.test.action;

import java.io.File;

import com.opensymphony.xwork2.ActionSupport;

public class formAction extends ActionSupport {

	private static final long serialVersionUID = -2757669937510150666L;
	
	private File file;

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	private String fileContentType;
	
	private String fileFileName;
	
	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	private String number;

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}
	
	public void testNumber(){
		System.out.println(number.toString());
	}
	
	public void testFile(){
		System.out.println(this.getFileFileName());
		System.out.println(file.getName());
	}
	

}

4.项目目录

PS:表单为file类型的时候,上传到后端,必须要有3个参数,参数在<form>标签下<input name = " ?">中的name值一致,对应在action中是private File ?;private  String  ?ContentType ; private String ?FileName ;


猜你喜欢

转载自blog.csdn.net/liangayang/article/details/80533281
今日推荐