一个简单的东西让我,弄了这么久- - !(zz!)
首先我没有,在xml文件中配置拦截器,然后Action的包名写错,还是细心的问题
注意在jsp中填写action=""的路径的时候,当前的路径一般是精确到项目名/下,所以我们不用再namespace的值前加上/了。
jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="item/upload.action" method="post" enctype="multipart/form-data">
Filename:<input type="text" name="filename"><br/>
file:<input type="file" name="myFile"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
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">
<!--
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<global-results>
<result name="error">/WEB-INF/jsp/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
<include file="example.xml"/>
-->
<struts>
<constant name="struts.devMode" value="false" />
<constant name="struts.configuration.xml.reload" value="true" />
<package name="upload" extends="struts-default" namespace="/item">
<action name="upload" class="com.struts2.struts2.UploadAction">
<result>/success.jsp</result>
</action>
</package>
</struts>
UploadAction:
package com.struts2.struts2;
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 com.opensymphony.xwork2.Action;
public class UploadAction {
private String filename;
private File myFile;
private String myFileFileName;
private String fileType;
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String getMyFileFileName() {
return myFileFileName;
}
public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
}
public String getFileType() {
return fileType;
}
public void setFileType(String fileType) {
this.fileType = fileType;
}
public String execute() throws Exception {
InputStream is = null;
OutputStream os = null;
try {
is = new BufferedInputStream(
new FileInputStream(myFile));
os = new BufferedOutputStream(
new FileOutputStream("c:\\"+myFileFileName));
int len = 0;
byte[] buffer = new byte[10024];
while( (len = is.read(buffer)) >0){
os.write(buffer,0,len);
}
} finally{
if(is!=null) is.close();
if(os!=null) os.close();
}
return Action.SUCCESS;
}
}
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>FileUpload</display-name>
<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>
<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>
</web-app>