| 发表于:2007-08-30 14:18:493楼 得分:0 |
package com.huadu.action; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.apache.struts.action.action; import org.apache.struts.action.actionform; import org.apache.struts.action.actionforward; import org.apache.struts.action.actionmapping; import org.apache.struts.upload.formfile; import com.huadu.actionform.uploadactionform; import org.apache.struts.upload.formfile; import java.io.inputstream; import java.io.outputstream; import java.io.ioexception; import java.io.filenotfoundexception; import java.io.fileoutputstream; public class uploadaction extends action { public actionforward EXECute(actionmapping actionmapping,actionform actionform,httpservletrequest request,httpservletresponse response) throws exception { //webfun getname = new webfun(); uploadactionform uploadfileform = (uploadactionform) actionform; //uploadform是formbean formfile file = uploadfileform.getfile(); //那个重要的类 system.out.println(uploadfileform.getfile()); 为什么这里是空啊 string fname = file.getfilename(); //获得文件名 inputstream streamin = null; //输入输出流 outputstream streamout = null; string sysroot = servlet.getservletcontext().getrealpath( "userinfo "); //获取图片文件夹路径 string filepath = sysroot + "\\ " + "89 "; //最终路径 try { streamin = file.getinputstream(); //以下是上传的代码,不用变,固定的 streamout = new fileoutputstream(filepath); int bytesread = 0; byte[] buffer = new byte[20480]; while ( (bytesread = streamin.read(buffer, 0, 20480)) != -1) { streamout.write(buffer, 0, bytesread); } streamout.close(); streamin.close(); request.setattribute( "info ", "上传成功啦 "); return actionmapping.findforward( "fail "); } catch (filenotfoundexception e) { request.setattribute( "error ", "上传失败啦 "); e.printstacktrace(); return actionmapping.findforward( "fail "); } catch (ioexception e) { // todo 自动生成 catch 块 request.setattribute( "error ", "上传失败啦 "); e.printstacktrace(); return actionmapping.findforward( "fail "); } } } 这个我action中的 什么地方错了吗? ------------------ actionfrom private string filename; private formfile file; public string getfilename() { return filename; } public void setfilename(string filename) { this.filename = filename; } public formfile getfile() { return file; } public void setfile(formfile file) { this.file = file; } ----------- 上传: <input type= "file " name= "filename " size= "30 "> <input type= "submit " value= "提交 "> 页面 的 什么地方错了嘛 <input type= "file " name= "filename " size= "30 "> 我把name= "filename "改成file会暴错 | | |
|