救命啊!Struts使用FormFile上传图片遇到的难以解决的问题

homssk 2009-11-01 04:48:26
1.首先是错误信息如下(这里只取错误信息的头部部分的信息出来):
2009-11-1 15:42:47 org.apache.commons.beanutils.PropertyUtilsBean invokeMethod
严重: Method invocation failed.
java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1773)
2009-11-1 15:42:47 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet action threw exception
java.lang.IllegalArgumentException: Cannot invoke com.my.domain.BookForm.setPicturefile - argument type mismatch
at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1778)
at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1022)
at org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:811)
at org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:298)
at org.apache.struts.util.RequestUtils.populate(RequestUtils.java:493)
2.网上的一般的解决方法都如下几种:a.提交的表单里面欠加上一句enctype="multipart/form-data"
b.表单的提交方式为应该为post
c.表单中的名称与actionform相应的字段名应该相同
我自认为这几种我都做到了,而且它那个错误的信息应该是程序还没有进入到action里面,而是在actionform在收集数据的时候就已经出错了,而且它里面说的是setPicturefile的类型不匹配,我的表单里面用的是file,接收的actionform的相应字段的类型为FormFile,这已经足足烦了我好几天了,这也算是难得遇上的一个错误了,务必请高手帮帮啊,我想就这样屈服在这个错误下,很不甘心啊,而改用其它组件
下面我把涉及到的几个文件代码贴上去:
1.提交页面表单部分的(由于太长了,截取表单那部分)

<form action="/BeanBook/bookAction.do?" name="bookForm" enctype="multipart/form-data" method="post">
<input type="hidden" name="action" value="2">
<table width="702" border="1" cellspacing="0" cellpadding="0">
.......................................
.......
<tr>
<td nowrap>图 书 定 价:</td>
<td colspan="3"><span id="sprytextfield7">
<input type="text" name="price" id="price">
<span class="textfieldRequiredMsg">需要提供一个值。</span><span class="textfieldInvalidFormatMsg">格式无效。</span></span></td>
</tr>
<tr>
<td nowrap>折  扣:</td>
<td colspan="3"><span id="sprytextfield8">
<input type="text" name="discount" id="discount">
<span class="textfieldRequiredMsg">需要提供一个值。</span><span class="textfieldInvalidFormatMsg">格式无效。</span><span class="textfieldMaxValueMsg">输入值大于所允许的最大值。</span></span></td>
<td nowrap>图 书 封 面:</td>
<td>

<input type="file" name="picturefile" />

</td>
</tr>
<tr>
<td nowrap>普通会员价:</td>
<td colspan="3"><span id="sprytextfield9">
<input type="text" name="userprice" id="userprice">
<span class="textfieldRequiredMsg">需要提供一个值。</span><span class="textfieldInvalidFormatMsg">格式无效。</span></span></td>
<td nowrap>图 片 预 览:</td>
<td rowspan="2"><br></td>
</tr>
<tr>
<td nowrap>白银级会员价:</td>
<td colspan="3"><span id="sprytextfield10">
<input type="text" name="vipsliver" id="vipsliver">
<span class="textfieldRequiredMsg">需要提供一个值。</span><span class="textfieldInvalidFormatMsg">格式无效。</span></span></td>
<td nowrap> </td>
</tr>
......................
.................
<tr>
<td> </td>
 <td width="94"> </td>
<td width="77"><label for="添加"></label>
<input type="submit" name="add" id="添加" value="提交"></td>
<td width="5"> </td> 
<td><label for="button"></label>
<input type="reset" name="button" id="button" value="重设"></td>
<td> </td>
</tr>
</table>
</form>
2.actionform,即是BookForm部分的代码
package com.my.domain;

import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;

public class BookForm extends ActionForm {
private static final long serialVersionUID = 1L;
//图书 的属性值如下
private Integer bookid=1;
private String bookname="";
private String type="";
private Integer totalpages=1;
private String author="";
private String press="";
private Integer sales=1;
private Integer bigid=0;
private Integer smallid=0;
private Integer stock=1;
private String face="";
private FormFile picturefile;
private String publishdate="";
private String introduce="";
private String isbn="";
private String shelvedate="";
private double price=0.0;
private double discount=0.0;
private double userprice=0.0;
private double vipsliver=0.0;
private double vipgold=0.0;
private double costprice=0.0;
private Integer viewtimes=1;
private Integer evalpeople=1;
private double score=0;
private String detail="";
//独立的一个,主要用于图片的上传

//默认的构造方法
public BookForm()
{ }
//各个属性值对应的setter和getter方法
...........
public String getFace() {
return face;
}
public void setFace(String face) {
this.face = face;
}

public FormFile getPicturefile() {
return picturefile;
}
public void setPicturefile(FormFile picturefile) {
this.picturefile = picturefile;
}

public String getPublishdate() {
return publishdate;
}
public void setPublishdate(String publishdate) {
this.publishdate = publishdate;
}
public String getIntroduce() {
return introduce;
}
public void setIntroduce(String introduce) {
this.introduce = introduce;
}
public String getIsbn() {
return isbn;
}
...........
public void setDetail(String detail) {
this.detail = detail;
}

}
3.BookAction里面对应添加新图书的那部分代码
private Integer action=null; //设置Integer类型的变量action,该变量用于辨别用户的行为,从而执行不同的方法
private BookDAO dao=null; //设置BookDAO类型的对象

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("----------中转至BookAction------------");
// TODO Auto-generated method stub
dao=new BookDAO();
//将该类实例化
System.out.println("传递过来的action的值是:"+request.getParameter("action"));
if(request.getParameter("action")!=null)
action=Integer.parseInt(request.getParameter("action"));

//获取传递过来的要进行的操作方式
System.out.println("BookAction当中的action的值是"+action);
switch(action)
{
//action为0,

case 0:{
}

//action为1,图书添加页面中的刷新页面
case 1:{
return selectsmallname( mapping, form, request, response);
}
//action为2,添加新图书页面
case 2:{
return bookadd( mapping, form, request, response);
}
case 3: ;
case 4: ;
case 5: ;
case 6: ;
}
return super.execute(mapping, form, request, response);
}

//直接将它返回,其目的是根据大类别的选择刷新图书添加页面,从而实现级联下拉列表
public ActionForward selectsmallname(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
//此函数只是简单的接收在图书添加页面传递过来的bigid参数,然后不执行任何操作
request.setAttribute("bigid", request.getParameter("bigid"));
return mapping.findForward("bookadd");
}
//添加新图书函数
public ActionForward bookadd(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
System.out.println("-------中转至BookAction当中的bookadd--------");
BookBean bookbean; //定义一个bookbean对象
UploadFile uploadFile = new UploadFile(); //实例化一个文件上传类
BookForm bookform=(BookForm)form; //将参数form类型转换为BookForm类型
String dir = servlet.getServletContext().getRealPath("/BookFaces");
//获取真实路径
FormFile bookface=bookform.getPicturefile();
//利用截取子字符串的方式,获取图片的类型

//-------------------------------------------------------------------------------------------
System.out.println("---------测试部分--------");
System.out.println(bookform.getBookname()+bookform.getType()+bookform.getPress()+bookform.getDiscount());
System.out.println("文件名:"+bookface.getFileName()+" 大小:"+bookface.getFileSize());
//-------------------------------------------------------------------------------------------

return mapping.findForward("bookadd");
}

}
4.文件上传部分
package com.my.tool;

import java.io.*;
import java.util.Date;
import org.apache.struts.upload.FormFile;

public class UploadFile {
public String upload(String dir, FormFile formFile) throws Exception {
Date date = new Date();
// 取欲上传的文件的名字和长度
String fname = formFile.getFileName();
// 将上传时间加入文件名
int i = fname.indexOf(".");
String name = String.valueOf(date.getTime()); //将日期作为该文件名
String type = fname.substring(i + 1);
fname = name + "." + type; //合成新的文件名
InputStream streamIn = formFile.getInputStream(); // 创建读取用户上传文件的对象
File uploadFile = new File(dir); // 创建把上传数据写到目标文件的对象
if (!uploadFile.exists() || uploadFile == null) { // 判断指定路径是否存在,不存在则创建路径
uploadFile.mkdirs(); //不存在的话,则创建该目录
}
String path = uploadFile.getPath() + "/" + fname; //合成文件的完整路径
OutputStream streamOut = new FileOutputStream(path);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer, 0, bytesRead);
}
streamOut.close();
streamIn.close();
formFile.destroy();
return fname;
}
}
拜托各位了,因为我是新手,分数有的也就是这么多了,见谅,对问题还是很理解的话,可以问我本人:QQ:120786828,拜托了
...全文
348 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fanyuna 2010-01-15
  • 打赏
  • 举报
回复
argument type mismatch

类型不匹配啊,说明你表单中的控件的对应的FormBean中的属性类型与DB中的字段的类型不对应哦
crazylaa 2009-12-23
  • 打赏
  • 举报
回复
1.<html:form action="/chapter/chapter.do" method="POST"
enctype="multipart/form-data">
2.看看上面得@page里面只要 <%@ page contentType="text/html; charset=****"%>,如果下面得<body>前面还有meta,就把里面得去掉。
myqmanager 2009-12-23
  • 打赏
  • 举报
回复
应该是这句 enctype="multipart/form-data" 没生效,enctype="multipart/form-data" method="post" 这两句换下位置试试,很奇怪的
config_man 2009-11-01
  • 打赏
  • 举报
回复
救命?你有性命危机?
什么都不能 2009-11-01
  • 打赏
  • 举报
回复
试一下 <html:file property="picturefile" />

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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