用org.apache.struts.upload.FormFile;上传文件时,出现如下错误!!!!!!!!!

chengming2008 2005-07-01 05:22:18
java.lang.IllegalArgumentException: argument type mismatch

Form 如下:
public class ImageForm extends ActionForm{

private int fpicID;
private String fname;
private String fcode;
private int fportalID;
private int fsourceID;
private int fileTypeID;
private int fheight;
private int fwidth;
private int fkindID;
private int ftypeID;
private int fstyleID;
private String fcreatedate;
private int fcreatorname;
private String fcreatorID;
private FormFile fblob;
private byte[] picBlob;

/**
* @return 返回 fblob。
*/
public FormFile getFblob() {
return fblob;
}
/**
* @param fblob 要设置的 fblob。
*/
public void setFblob(FormFile fblob) {
this.fblob = fblob;
try {
InputStream in = fblob.getInputStream();
this.picBlob = new byte[in.available()];
in.read(picBlob);
in.close();
} catch (Exception e) {
//log.error("[系统错误]录入文件有问题:" + e.getMessage());
}
}
/**
* @return 返回 fcode。
*/
public String getFcode() {
return fcode;
}
/**
* @param fcode 要设置的 fcode。
*/
public void setFcode(String fcode) {
this.fcode = fcode;
}
/**
* @return 返回 fcreatedate。
*/
public String getFcreatedate() {
return fcreatedate;
}
/**
* @param fcreatedate 要设置的 fcreatedate。
*/
public void setFcreatedate(String fcreatedate) {
this.fcreatedate = fcreatedate;
}
/**
* @return 返回 fcreatorID。
*/
public String getFcreatorID() {
return fcreatorID;
}
/**
* @param fcreatorID 要设置的 fcreatorID。
*/
public void setFcreatorID(String fcreatorID) {
this.fcreatorID = fcreatorID;
}
/**
* @return 返回 fcreatorname。
*/
public int getFcreatorname() {
return fcreatorname;
}
/**
* @param fcreatorname 要设置的 fcreatorname。
*/
public void setFcreatorname(int fcreatorname) {
this.fcreatorname = fcreatorname;
}
/**
* @return 返回 fheight。
*/
public int getFheight() {
return fheight;
}
/**
* @param fheight 要设置的 fheight。
*/
public void setFheight(int fheight) {
this.fheight = fheight;
}
/**
* @return 返回 fileTypeID。
*/
public int getFileTypeID() {
return fileTypeID;
}
/**
* @param fileTypeID 要设置的 fileTypeID。
*/
public void setFileTypeID(int fileTypeID) {
this.fileTypeID = fileTypeID;
}
/**
* @return 返回 fkindID。
*/
public int getFkindID() {
return fkindID;
}
/**
* @param fkindID 要设置的 fkindID。
*/
public void setFkindID(int fkindID) {
this.fkindID = fkindID;
}
/**
* @return 返回 fname。
*/
public String getFname() {
return fname;
}
/**
* @param fname 要设置的 fname。
*/
public void setFname(String fname) {
this.fname = fname;
}
/**
* @return 返回 fpicID。
*/
public int getFpicID() {
return fpicID;
}
/**
* @param fpicID 要设置的 fpicID。
*/
public void setFpicID(int fpicID) {
this.fpicID = fpicID;
}
/**
* @return 返回 fportalID。
*/
public int getFportalID() {
return fportalID;
}
/**
* @param fportalID 要设置的 fportalID。
*/
public void setFportalID(int fportalID) {
this.fportalID = fportalID;
}
/**
* @return 返回 fsourceID。
*/
public int getFsourceID() {
return fsourceID;
}
/**
* @param fsourceID 要设置的 fsourceID。
*/
public void setFsourceID(int fsourceID) {
this.fsourceID = fsourceID;
}
/**
* @return 返回 fstyleID。
*/
public int getFstyleID() {
return fstyleID;
}
/**
* @param fstyleID 要设置的 fstyleID。
*/
public void setFstyleID(int fstyleID) {
this.fstyleID = fstyleID;
}
/**
* @return 返回 ftypeID。
*/
public int getFtypeID() {
return ftypeID;
}
/**
* @param ftypeID 要设置的 ftypeID。
*/
public void setFtypeID(int ftypeID) {
this.ftypeID = ftypeID;
}
/**
* @return 返回 fwidth。
*/
public int getFwidth() {
return fwidth;
}
/**
* @param fwidth 要设置的 fwidth。
*/
public void setFwidth(int fwidth) {
this.fwidth = fwidth;
}
/**
* @return 返回 picBlob。
*/
public byte[] getPicBlob() {
return picBlob;
}
/**
* @param picBlob 要设置的 picBlob。
*/
public void setPicBlob(byte[] picBlob) {
this.picBlob = picBlob;

}
public ActionErrors validate(ActionMapping arg0, HttpServletRequest arg1) {
// TODO Auto-generated method stub
return super.validate(arg0, arg1);
}

}



如果把private FormFile fblob;去掉就无问题!!
...全文
374 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
kui 2005-07-09
  • 打赏
  • 举报
回复
试试Struts自己带的upload例子,我试了没有错误。
wangguobin 2005-07-01
  • 打赏
  • 举报
回复
其实代码看不出来什么毛病 哈哈 我也是这个问题 帮你顶一下
web中使用POI导入导出EXCEL文件的例子 struts1.x的例子,struts2.x可以参考自己修改 1.action的写法 import java.io.*; import java.sql.*; import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.*; import org.apache.struts.action.*; import org.apache.struts.upload.FormFile; import org.apache.commons.beanutils.BeanUtils; public class Action { /**//* * 把数据库中的字段导入到Excel ,并生成Excel文档 **/ public ActionForward getDownload(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { Form fm = (Form) actionForm; // Excel 文件存放在服务器的相对路径下 String outputFile = request.getRealPath("/tmp/Excel.xls"); try { // 创建新的Excel 工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); // 在Excel 工作簿中建一工作表 HSSFSheet sheet = workbook.createSheet("Sheet1"); // 设置单元格格式(文本) HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("@")); // 在索引0的位置创建行(第一行) HSSFRow row = sheet.createRow((short) 0); HSSFCell cell1 = row.createCell((short) 0);// 第一列 HSSFCell cell2 = row.createCell((short) 1); HSSFCell cell3 = row.createCell((short) 2); // 定义单元格为字符串类型 cell1.setCellType(HSSFCell.CELL_TYPE_STRING); cell2.setCellType(HSSFCell.CELL_TYPE_STRING); cell3.setCellType(HSSFCell.CELL_TYPE_STRING); cell1.setEncoding(HSSFCell.ENCODING_UTF_16); cell2.setEncoding(HSSFCell.ENCODING_UTF_16); cell3.setEncoding(HSSFCell.ENCODING_UTF_16); // 在单元格中输入数据 cell1.setCellValue("姓名"); cell2.setCellValue("性别"); cell3.setCellValue("年龄"); Connection connection = session.connection(); String sql = "Select t.name, t.sex, t.age from table t where t.sex = ?"; try { PreparedStatement ps = connection.prepareStatement(sql); ps.setString(1, fm.getSex());// 入查询条件 ResultSet rs = ps.executeQuery();// 查询结果存入rs connection.commit();// 执行SQL while (rs.next()) { //设置j行从第二行开始 int j = 1; row = sheet.createRow((short) j); //设置i列从第二列开始 for (int i = 1; i <= 3; i++) { HSSFCell cell = row.createCell((short) (i-1)); // 设置单元格格式 cell.setCellStyle(cellStyle); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue(rs.getString(i)); } j++; } request.setAttribute("message", "文件生成成功!"); } catch (SQLException e) { request.setAttribute("message", "创建文件失败!"); e.printStackTrace(); } // 删除路径下同名的Excel 文件 File path = new File(outputFile); path.delete(); // 新建一输出文件流 FileOutputStream fOut = new FileOutputStream(outputFile); // 把相应的Excel 工作簿存盘 workbook.write(fOut); // 操作结束,关闭文件 fOut.flush(); fOut.close(); //该处如果Excel过大会影响效率,谁有好的想法可以提出来参考(不过从页面下载完后就会清空) request.getSession().setAttribute("Download", outputFile); } catch (Exception ioexception) { request.setAttribute("message", "创建文件失败!"); return actionMapping.findForward("outJSP"); } return actionMapping.findForward("outJSP"); }
/* * 使用java.util.Map接口实现文件组的上 */ private void muchUploadFile(IndexActionForm objForm) { MapFormFile> fileList = objForm.getFileList(); for(String str : fileList.keySet()) if((fileList.get(str)).getFileSize() > 0 && (fileList.get(str)).getFileSize() < BUFFER_SIZE) { String fileName = DIRECTORY +"/"+ fileList.get(str).getFileName(); try { this.fileStream( fileList.get(str).getInputStream(), fileName, fileList.get(str).getFileSize()); } catch (IOException ex) { System.out.println(ex.getMessage()); } } } /* * 使用org.apache.strtus.upload.FormFile实现文件的单一上 */ private void singleUploadFile(IndexActionForm objForm) { if(objForm.getFile().getFileSize() == 0|| BUFFER_SIZE < objForm.getFile().getFileSize()) throw new RuntimeException("文件过大或不存在!!!"); String fileName = DIRECTORY +"/"+ objForm.getFile().getFileName(); try { this.fileStream( objForm.getFile().getInputStream(), fileName, objForm.getFile().getFileSize()); } catch (IOException ex) { System.out.println(ex.getMessage()); } } /* * copy 到本地目录 */ private void fileStream(InputStream strem, String fileName, int size) { byte[] buffer = new byte[size]; try { InputStream in = null; OutputStream out = null; try{ in = new BufferedInputStream(strem, size); out = new BufferedOutputStream(new FileOutputStream(fileName), size); while(in.read(buffer) > 0) out.write(buffer); }finally { if(null != in) in.close(); if(null != out){ out.flush(); out.close(); } } System.out.println("Uploading Success!!!"); } catch (IOException ex) { System.out.println(ex.getMessage()); } }
package com.actionForm; import org.apache.struts.action.*; import org.apache.struts.upload.*; public class FileActionForm extends ActionForm { private String fileName;//上文件的名称 private String fileSize;//上文件的大小 private String filePath;//上文件到服务器的路径 private String fileDate;//上文件的日期 private FormFile file;//上文件 public String getFileName() { return fileName; } public FormFile getFile() { return file; } public String getFileSize() { return fileSize; } public String getFilePath() { return filePath; } public String getFileDate() { return fileDate; } public void setFileName(String fileName) { this.fileName = fileName; } public void setFile(FormFile file) { this.file = file; } public void setFileSize(String fileSize) { this.fileSize = fileSize; } public void setFilePath(String filePath) { this.filePath = filePath; } public void setFileDate(String fileDate) { this.fileDate = fileDate; } } package com.action; import org.apache.struts.action.*; import javax.servlet.http.*; import com.actionForm.FileActionForm; import org.apache.struts.actions.DispatchAction; import java.util.Date; import java.text.*; import org.apache.struts.upload.FormFile; import java.io.*; import java.net.URLEncoder; public class FileAction extends DispatchAction { private JDBConnection connection =new JDBConnection(); //以下方法实现文件的上 public ActionForward upLoadFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward forward=null; Date date = new Date(); FileActionForm fileActionForm = (FileActionForm) form; //FormFile用于指定存取文件的类型 FormFile file = fileActionForm.getFile(); //获取当前的文件 // 获得系统的绝对路径 String dir = servlet.getServletContext().getRealPath("/image"); //我上文件没有放在服务器上。而是存在D:D:\\loadfile\\temp\\ String dir="D:\\loadfile\\temp\\"; int i = 0; String type = file.getFileName(); while(i!=-1){ //找到上文件的类型的位置,这个地方的是'.' i = type.indexOf("."); /**//* System.out.println(i);*/ /**//*截取上文件的后缀名,此得到了文件的类型*/ type = type.substring(i+1); } // 限制上类型为jpg,txt,rar; if (!type.equals("jpg") && !type.equals("txt")&& !type.equals("bmp")) {//当上的类型不为上述类型,跳转到错误页面。 forward=mapping.findForward("error"); } else { // 将上间加入文件名(这个地方的是毫秒数) String times = String.valueOf(date.getTime()); //组合成 time.type String fname = times + "." + type; //InInputStream是用以从特定的资源读取字节的方法。 InputStream streamIn = file.getInputStream(); //创建读取用户上文件的对象 //得到是字节数,即byte,我们可以直接用file.getFileSize(),也可以在创建读取对象用streamIn.available(); // int ok=streamIn.available(); int ok=file.getFileSize(); String strFee = null; //这个地方是处理上的为M单位计算,下一个是以kb,在下一个是byte; if(ok>=1024*1024) { float ok1=(((float)ok)/1024f/1024f); DecimalFormat myformat1 = new DecimalFormat("0.00"); strFee = myformat1.format(ok1)+"M"; System.out.println(strFee+"M"); } else if(ok>1024 && ok<=1024*1024) { double ok2=((double)ok)/1024; DecimalFormat myformat2=new DecimalFormat("0.00"); strFee = myformat2.format(ok2)+"kb"; System.out.println(strFee+"kb"); } else if(ok<1024) { System.out.println("aaaaaaaaa"); strFee=String.valueOf(ok)+"byte"; System.out.println(strFee); } System.out.println( streamIn.available()+"文件大小byte"); //这个是io包下的上文件类 File uploadFile = new File(dir); //指定上文件的位置 if (!uploadFile.exists() || uploadFile == null) { //判断指定路径dir是否存在,不存在则创建路径 uploadFile.mkdirs(); } //上的路径+文件名 String path = uploadFile.getPath() + "\\" + fname; //OutputStream用于向某个目标写入字节的抽象类,这个地方写入目标是path,通过输出流FileOutputStream去写 OutputStream streamOut = new FileOutputStream(path); int bytesRead = 0; byte[] buffer = new byte[8192]; //将数据读入byte数组的一部分,其中读入字节数的最大值是8192,读入的字节将存储到,buffer[0]到buffer[0+8190-1]的部分中 //streamIn.read方法返回的是实际读取字节数目.如果读到末尾则返回-1.如果bytesRead返回为0则表示没有读取任何字节。 while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) { //写入buffer数组的一部分,从buf[0]开始写入并写入bytesRead个字节,这个write方法将发生阻塞直至字节写入完成。 streamOut.write(buffer, 0, bytesRead); } // 关闭输出输入流,销毁File流。 streamOut.close(); streamIn.close(); file.destroy(); String paths=path; System.out.println(paths); String fileName = Chinese.toChinese(fileActionForm.getFileName()); //获取文件的名称 //String fileSize = String.valueOf(file.getFileSize()); String fileDate = DateFormat.getDateInstance().format(date); String sql = "insert into tb_file values('" + fileName + "','" + strFee + "','" + fileDate + "','" + paths + "')"; connection.executeUpdate(sql); connection.closeConnection(); forward=mapping.findForward("upLoadFileResult"); } return forward; } //实现文件的下载 public ActionForward downFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String path = request.getParameter("path"); System.out.println(path+"111"); BufferedInputStream bis = null; BufferedOutputStream bos = null; OutputStream fos = null; InputStream fis = null; //如果是从服务器上取就用这个获得系统的绝对路径方法。 String filepath = servlet.getServletContext().getRealPath("/" + path); String filepath=path; System.out.println("文件路径"+filepath); File uploadFile = new File(filepath); fis = new FileInputStream(uploadFile); bis = new BufferedInputStream(fis); fos = response.getOutputStream(); bos = new BufferedOutputStream(fos); //这个就就是弹出下载对话框的关键代码 response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(path, "utf-8")); int bytesRead = 0; //这个地方的同上的一样。我就不多说了,都是用输入流进行先读,然后用输出流去写,唯一不同的是我用的是缓冲输入输出流 byte[] buffer = new byte[8192]; while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) { bos.write(buffer, 0, bytesRead); } bos.flush(); fis.close(); bis.close(); fos.close(); bos.close(); return null; } }

81,095

社区成员

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

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