struts文件上传出错 求救啊!救命啊!

zz262067995 2010-05-27 11:23:23
action里面的代码:

//通过FormFile对象上传文件
private FormFile processDef;
private FormFile processImage;

public FormFile getProcessDef() {
return processDef;
}
public void setProcessDef(FormFile processDef) {
this.processDef = processDef;
}
public FormFile getProcessImage() {
return processImage;
}
public void setProcessImage(FormFile processImage) {
this.processImage = processImage;
}

页面的代码:
<tr>
<td class="tdEditLabel" ><strong>请选择流程定义文件</strong></td>
<td class="tdEditContent"><strong><input type="file" name="processDef"></strong>
</td>
<td class="tdEditLabel" ><strong>请选择流程定义图片</strong></td>
<td class="tdEditContent"><strong><input type="file" name="processImage"></strong></td>
</tr>


但是上传之后 出现了以下的错误!:
ognl.MethodFailedException: Method "setProcessDef" failed for object su.rong.jie.action.WorkflowAction@41fef [java.lang.NoSuchMethodException: setProcessDef([Ljava.io.File;)]
。。。
/-- Encapsulated exception ------------\
java.lang.NoSuchMethodException: setProcessDef([Ljava.io.File;)
。。。
\--------------------------------------/
expr: processDef val: [Ljava.io.File;@ccc48b context: ognl.OgnlContext@c7df21c1 root:[su.rong.jie.action.WorkflowAction@41fef, com.opensymphony.xwork2.DefaultTextProvider@17ba2e9] value: [Ljava.io.File;@ccc48b
ognl.MethodFailedException: Method "setProcessImage" failed for object su.rong.jie.action.WorkflowAction@41fef [java.lang.NoSuchMethodException: setProcessImage([Ljava.io.File;)]
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:823)


...全文
142 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
penghaozhong 2010-12-21
  • 打赏
  • 举报
回复
楼主 把问题解决没啊,我也遇到这个问题了 ,好烦哦,这是第二天了
izard999 2010-05-28
  • 打赏
  • 举报
回复
俺记岔了,html中式File的name
刚没看到第二个action, sorry

下面两个String的List名字改成contentType和fileName再看.!
izard999 2010-05-28
  • 打赏
  • 举报
回复
java.lang.NoSuchMethodException: setProcessImage
从异常信息看是没有set方法. 但是实际的本质还是我上面说的问题.!
izard999 2010-05-28
  • 打赏
  • 举报
回复
LZ估计不看官方文档和源码的.! 这里面都已经很清楚了
Struts2文件上传的拦截器(FileUploadInterceptor)中定义了Action中应有的三个属性(除了File的名字可以换,其他两个是固定的!)
1:String contentType 2:String fileName 3:File file
把你的formFile换成上面三个, 添加get/set方法.!
页面上的<s:file name="fileName">不是对应file.!
xinlan1022 2010-05-28
  • 打赏
  • 举报
回复
form标签加上这个method="post" enctype="multipart/form-data"
  • 打赏
  • 举报
回复
这个你找下怎么把文件流转换成byte[]就行了,
对于文件上传的action:你implements ServletContextAware。
重写下
public void setServletContext(ServletContext arg0){
// TODO Auto-generated method stub
try {
String urls=arg0.getRealPath(保存的路径)
//通过set方法来把真实的路径保存到upurl属性中
setUpurl(urls);
} catch (Exception e) {
log.error("系统忙!,请稍后再试!",e);
}
}
zz262067995 2010-05-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 fan578 的回复:]
使用到struts上传文件的时候,你可以先获取下文件的类型,文件名,打印出来看看!文件类型我不太记得了,文件名写一个private String 表单设置的file的name,比如你的就是file+fileName,就是你在你的action中添加上private File file; private String filefileName(文件名);你测测,还有你换个struts上传文件用的act……
[/Quote]
姐姐你好!
我知道你的意思。。。
我用Flie接收到 但是我需要转成byte[]数组 因为方法参数要求要是byte[]
怎么转啊
  • 打赏
  • 举报
回复
使用到struts上传文件的时候,你可以先获取下文件的类型,文件名,打印出来看看!文件类型我不太记得了,文件名写一个private String 表单设置的file的name,比如你的就是file+fileName,就是你在你的action中添加上private File file; private String filefileName(文件名);你测测,还有你换个struts上传文件用的action,别用actionSupport
希望对你有用
  • 打赏
  • 举报
回复
你可以在你的set方法那里设置一个断点,看下是否执行到那个方法,file用java.io.file就可以,如果不行,明天我发段我们项目中的struts2的上传给你看
zz262067995 2010-05-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fan578 的回复:]
错误提示是struts的OGNL在封装你的表单数据的时候,有个字段无法封装,找到到对应的对象字段进行封装,即没有那个字段的set方法,你把FormFile 改成FILE先试试
[/Quote]我不大懂你的意思啊。。。
为什么set方法会找不到呢
我有用过File属性 不过那个File是要引用struts2的File 还是要用java。io。File的File啊
zz262067995 2010-05-27
  • 打赏
  • 举报
回复
其实我希望你能帮我看下我的问题 不过还是谢谢你回答我的问题哈

我已经被这问题困恼几天了 都快抓狂了!
  • 打赏
  • 举报
回复
错误提示是struts的OGNL在封装你的表单数据的时候,有个字段无法封装,找到到对应的对象字段进行封装,即没有那个字段的set方法,你把FormFile 改成FILE先试试
lei30 2010-05-27
  • 打赏
  • 举报
回复

看看对你有没有帮助
页面源码
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP 'Upload.jsp' starting page</title>
<script type="text/javascript">

function addMore(){
var td=document.getElementById("more");

var br=document.createElement("br");
var input=document.createElement("input");
var button=document.createElement("button");

input.type="file";
input.name="file";

//button.type="button";
button.value="Remove";

button.onclick=function(){
td.removeChild(br);
td.removeChild(input);
td.removeChild(button);
}

td.appendChild(br);
td.appendChild(input);
td.appendChild(button);
}
</script>

</head>

<body>
<table align="center" width="50%">
<s:fielderror cssStyle="color:red"></s:fielderror>
</table>
<s:form action="Upload.action" method="post" enctype="multipart/form-data" theme="simple">
<table width="50%" border="1" align="center">
<tr>
<td>username:</td>
<td><s:textfield name="username"/></td>
</tr>
<tr>
<td>password:</td>
<td> <s:password name="password"/></td>
</tr>
<tr>
<td>file:</td>
<td id="more">
<s:file name="file"/><input type="button" value="more...." onclick="addMore()"/>
</td>
</tr>
<tr>
<td><s:submit label="submit"></s:submit></td>
<td><s:reset label="reset"></s:reset></td>
</tr>
</table>
</s:form>
</body>
</html>

lei30 2010-05-27
  • 打赏
  • 举报
回复
源码
package com.framework.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {
private static final long serialVersionUID = 1L;

private String username;

private String password;

private List<File> file;

private List<String> fileFileName;

private List<String> fileContentType;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public List<File> getFile() {
return file;
}

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

public List<String> getFileFileName() {
return fileFileName;
}

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

public List<String> getFileContentType() {
return fileContentType;
}

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

@SuppressWarnings("deprecation")
@Override
public String execute() throws Exception {
for (int i = 0; i < file.size(); i++) {

InputStream is = new FileInputStream(file.get(i));

String root = ServletActionContext.getRequest().getRealPath("\\item");

File destFile = new File(root, this.getFileFileName().get(i));

OutputStream os = new FileOutputStream(destFile);

byte[] by = new byte[400];

int length = 0;

while ((length = is.read(by)) > 0) {

os.write(by, 0, length);
}

is.close();
os.close();
}
return "success";
}

}


67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧