struts2文件上传问题,高手指教下

一步步的走 2010-09-26 11:40:51
运行后报错提示如下:
Could not find action or result
There is no Action mapped for action name upload/. - [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:178)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:861)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1584)
at java.lang.Thread.run(Unknown Source)
跪求高手指教。。。。

项目文件如下:1.UploadAction.java



package lee;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.*;
public class UploadAction extends ActionSupport {
private String title;
private File upload;
private String uploadContentType; //这两个属性是必须有的,有框架要用
private String uploadFileName; //这两个属性是必须有的,有框架要用

//接受依赖注入的属性

private String savePath;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

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 ServletActionContext.getRequest().getRealPath(savePath);
}
//接受依赖注入的方法
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String execute()throws Exception{
System.out.println("开始上传单个文件-------------");
System.out.println(this.getSavePath());
System.out.println("=========="+this.getUploadFileName());
System.out.println("=========="+this.getUploadContentType());
System.out.println("=========="+this.getUpload());
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos=new FileOutputStream(this.getSavePath()+"\\"+this.getUploadFileName());
FileInputStream fis=new FileInputStream(this.getUpload());
byte[] buffer=new byte[1024];
int len=0;
while((len=fis.read(buffer))>0){
fos.write(buffer,0,len);
}
return SUCCESS;
}
}



2.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>

<constant name="struts.custom.i18n.resources" value="globalMessages"/>
<constant name="struts.i18n.encoding" value="GBK"/>

<package name="lee" extends="struts-default">

<action name="upload" class="lee.UploadAction">
<param name="savePath">/upload</param>
<result>/succ.jsp</result>
</action>
</package>
</struts>



3.web.xml



<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- 定义Struts2的FilterDispathcer的Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>



4.upload.html





<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>简单的文件上传</title>
</head>
<body>
<form action="upload.action" method="post" enctype="multipart/form-data">
文件标题:<input type="text" name="title" /><br>
选择文件:<input type="file" name="upload" /><br>
<input value="上传" type="submit" />
</form>
</body>





5.succ.jsp



<%@ page language="java" contentType="text/html;charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>上传成功!</title>
</head>

<body>
上传成功!<br>
文件标题:<s:property value="+ title"/>
文件为:<img src="<s:property value="'upload/' + uploadFileName"/>"/><br>
</body>
</html>

</html>



...全文
108 点赞 收藏 13
写回复
13 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
jqf2009 2010-10-02
<param name="savePath">/upload</param>
楼主可以把这个给删了,可能就不会报错了
回复
a312983516 2010-10-02
你在HEAD标签内加上那个<bean ……> 就可以了
回复
lmzpp 2010-10-01
http://download.csdn.net/source/2625795
可以参考这个
回复
Colonel_Zhou 2010-09-29
private File upload;
private String uploadFileName;
private String uploadContentType;
private String fileUplodPath;
//鉴定允许上传文件的类型
private String allowedTypes;


/**
* 执行文件上传方法
* @return
* @throws IOException
*/
public String upFile() throws IOException
{
String toFaroward=null;
//分割允许上传文件的格式成数组
String[] alTy=getAllowedTypes().split(",");
//获取上传文件的格式
String isType=getUploadContentType();
//判断格式是否被允许上传
for (int i = 0; i < alTy.length; i++) {
if (alTy[i].equals(isType)) {
//判断文件的大小
if (getUpload().length()<=BUFFER_SIZE) {
//根据服务器的文件保存地址和原文件名创建目录文件全路径
setFileUplodPath("/filePath");
String dstPathString=ServletActionContext.getServletContext().getRealPath(getFileUplodPath())+"\\"+getUploadFileName();
//System.out.println("filepath"+dstPathString);
String filescr=ServletActionContext.getRequest().getContextPath()+
getFileUplodPath()+"/"+getUploadFileName();

File dstFile=new File(dstPathString);
try {
//此两个方法均可用,
//copyFile(getUpload(), dstFile);
//apache封装的方法.
FileUtils.copyFile(getUpload(), dstFile);
ServletActionContext.getRequest().setAttribute("srcpath", filescr);
toFaroward="SUCCESS";
} catch (Exception e) {
toFaroward="eorr";
}
}else {
ServletActionContext.getRequest().setAttribute("eorrs", "文件太大了!");
toFaroward="eorr";
}

}else {
ServletActionContext.getRequest().setAttribute("eorrs", "文件类型不正确");
ServletActionContext.getRequest().setAttribute("alTYPES", alTy);
toFaroward="eorr";
}
}
return toFaroward;
}




public File getUpload() {
return upload;
}

public void setUpload(File upload) {
this.upload = upload;
}

public String getUploadFileName() {
return uploadFileName;
}

public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}

public String getUploadContentType() {
return uploadContentType;
}

public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}

public String getFileUplodPath() {
return fileUplodPath;
}

public void setFileUplodPath(String fileUplodPath) {
this.fileUplodPath = fileUplodPath;
}

public String getAllowedTypes() {
return allowedTypes;
}

public void setAllowedTypes(String allowedTypes) {
this.allowedTypes = allowedTypes;
}


回复
一步步的走 2010-09-26
这个问题解决了,要把upload文件夹建立在WEB-INF下面,我晕啊。又出现新问题了,在welcome.jsp里面图片显示不出来,这个是怎么回事呢。。
回复
hubujun 2010-09-26
你的struts.xml放在什么目录下面 把他放到scr下试试
回复
hubujun 2010-09-26
过两天我也要实现这个功能 不过没发现楼主的错误 求解
回复
一步步的走 2010-09-26
<action name="upload" class="lee.UploadAction">
<param name="savePath">/upload</param>
<result>/succ.jsp</result>
</action>
在这里作为参数传到UploadAction里面的啊。。
回复
一步步的走 2010-09-26
success不是影射到welcome.jsp上了嘛,你看看啊,上面有struts.xml的内容啊
回复
luozhangwen 2010-09-26
upload/.

找下这个路径是哪里来的. 我找了半天也没找到...
回复
一步步的走 2010-09-26
没有高手来解释下吗?这个路径问题至今不是很清楚,用myeclipse建立的文件,路径问题都搞晕了
回复
tianshi1017 2010-09-26
从报的错来看好像是你的struts.xml配置的问题,
返回的success没有对应的配置
回复
相关推荐
发帖
Java EE
创建于2007-09-28

6.7w+

社区成员

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
申请成为版主
帖子事件
创建了帖子
2010-09-26 11:40
社区公告
暂无公告