up.jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>up 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>
<s:form action="up" method="post" enctype="multipart/form-data" theme="simple" namespace="/">
<s:file name="upload" cssStyle="width:300px"></s:file>
<s:submit value="确定"></s:submit>
</s:form>
</body>
</html>MyUpAction.java代码:
package com.chen.validate;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.java.util.*;public class MyUpAction extends ActionSupport {
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;
} public String getSavePath() {
return savePath;
} public void setSavePath(String savePath) {
this.savePath = savePath;
} private File upload;
private String uploadContentType;
private String uploadFileName;
private String savePath;
public String getSavePath()throws Exception{
return ServletActionContext.getRequest().getRealPath(savePath);
}
public String excute()throws Exception{
String fileName=getSavePath()+"\\"+getUploadFileName();
FileOutputStream fos=new FileOutputStream(fileName);
FileInputStream fis=new FileInputStream(getUploadFileName());
byte[] b=new byte[1024];
int len=0;
while((len=fis.read(b))>0){
fos.write(b, 0, len);
}
return SUCCESS;
}
}struts.xml代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true"/>
<package name="default" namespace="/" extends="struts-default">
<action name="up" class="com.chen.validate.MyUpAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png, image/pjpeg
</param>
<param name="maximumSize">90000000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
<param name="savaPath">/save</param>
<result name="input">up.jsp</result>
<result name="seccess">/success.jsp</result>
</action>
</package>
</struts>
另外:我的Action所在的包名是:com.chen.validate;最后在提交上传图片的“确定”按钮后,出现的错误是:
Struts Problem Report
Struts has detected an unhandled exception: Messages: No result defined for action com.chen.validate.MyUpAction and result success
提问:请问各位高手,这是什么原因?
谢了!
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>up 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>
<s:form action="up" method="post" enctype="multipart/form-data" theme="simple" namespace="/">
<s:file name="upload" cssStyle="width:300px"></s:file>
<s:submit value="确定"></s:submit>
</s:form>
</body>
</html>MyUpAction.java代码:
package com.chen.validate;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.java.util.*;public class MyUpAction extends ActionSupport {
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;
} public String getSavePath() {
return savePath;
} public void setSavePath(String savePath) {
this.savePath = savePath;
} private File upload;
private String uploadContentType;
private String uploadFileName;
private String savePath;
public String getSavePath()throws Exception{
return ServletActionContext.getRequest().getRealPath(savePath);
}
public String excute()throws Exception{
String fileName=getSavePath()+"\\"+getUploadFileName();
FileOutputStream fos=new FileOutputStream(fileName);
FileInputStream fis=new FileInputStream(getUploadFileName());
byte[] b=new byte[1024];
int len=0;
while((len=fis.read(b))>0){
fos.write(b, 0, len);
}
return SUCCESS;
}
}struts.xml代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true"/>
<package name="default" namespace="/" extends="struts-default">
<action name="up" class="com.chen.validate.MyUpAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png, image/pjpeg
</param>
<param name="maximumSize">90000000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
<param name="savaPath">/save</param>
<result name="input">up.jsp</result>
<result name="seccess">/success.jsp</result>
</action>
</package>
</struts>
另外:我的Action所在的包名是:com.chen.validate;最后在提交上传图片的“确定”按钮后,出现的错误是:
Struts Problem Report
Struts has detected an unhandled exception: Messages: No result defined for action com.chen.validate.MyUpAction and result success
提问:请问各位高手,这是什么原因?
谢了!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货