问题描述
后台能获得textarea的内容,但是得不到imgFile(文件名),在StringfileName=wrapper.getFileNames("imgFile")[0];//imgFile,imgFile,imgFile这里提示空指针错误。能获得textarea内容,配置应该没错。刚弄这个东西,查了好几天不知道怎么办。求哥哥姐姐帮我!packagewyk.action;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.InputStream;importjava.io.PrintWriter;importjava.text.SimpleDateFormat;importjava.util.Arrays;importjava.util.Date;importjava.util.HashMap;importjava.util.Random;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.apache.commons.fileupload.servlet.ServletFileUpload;importorg.apache.struts2.ServletActionContext;importorg.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;importorg.json.simple.JSONObject;importcom.opensymphony.xwork2.ActionSupport;publicclassAddNewsActionextendsActionSupport{privateFileimgFile;privateStringimgFileFileName;privateStringimgWidth;privateStringimgHeight;privateStringalign;@OverridepublicStringexecute()throwsException{HttpServletRequestreq=(HttpServletRequest)ServletActionContext.getRequest();HttpServletResponseres=(HttpServletResponse)ServletActionContext.getResponse();//TODOAuto-generatedmethodstubPrintWriterout=res.getWriter();Stringtitle=req.getParameter("title").toString();Stringcontent=req.getParameter("content").toString();System.out.println("标题是"+title);System.out.println("内容是"+content);//文件保存目录路径//D:Tomcat6.0webappszswzattached/StringsavePath=req.getSession().getServletContext().getRealPath("/")+"attached/image";//文件保存目录URL/zswz/attached/StringsaveUrl=req.getContextPath()+"/attached/image";System.out.println("struts2文件保存目录路径"+savePath);System.out.println("文件保存目录URL"+saveUrl);//定义允许上传的文件扩展名//定义允许上传的文件扩展名HashMap<String,String>extMap=newHashMap<String,String>();extMap.put("image","gif,jpg,jpeg,png,bmp");extMap.put("flash","swf,flv");extMap.put("media","swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");extMap.put("file","doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");//允许最大上传文件大小struts.xmlstruts.multipart.maxSize=3GlongmaxSize=3000000000l;res.setContentType("text/html;charset=UTF-8");if(!ServletFileUpload.isMultipartContent(req)){System.out.println("请选择文件");out.println(getError("请选择文件"));return"error";}FileuploadDir=newFile(savePath);if(!uploadDir.isDirectory()){System.out.println("上传目录不存在");out.println(getError("上传目录不存在"));return"error";}StringdirName=req.getParameter("dir");//imageif(dirName==null){dirName="image";}if(!extMap.containsKey(dirName)){System.out.println("目录名不正确");out.println(getError("目录名不正确"));return"error";}savePath+=dirName+"/";//D:Tomcat6.0webappszswzattached/image/saveUrl+=dirName+"/";///zswz/attached/image/FilesaveDirFile=newFile(savePath);if(!saveDirFile.exists()){saveDirFile.mkdirs();}SimpleDateFormatsdf=newSimpleDateFormat("yyyyMMdd");Stringymd=sdf.format(newDate());savePath+=ymd+"/";//D:Tomcat6.0webappszswzattached/image/20111129/saveUrl+=ymd+"/";///zswz/attached/image/20111129/FiledirFile=newFile(savePath);if(!dirFile.exists()){dirFile.mkdirs();}if(!dirFile.isDirectory()){out.println(getError("上传目录不存在"));return"error";}//Struts2请求包装过滤器MultiPartRequestWrapperwrapper=(MultiPartRequestWrapper)req;//获得上传的文件名StringfileName=wrapper.getFileNames("imgFile")[0];//imgFile,imgFile,imgFileSystem.out.println("文件名是"+fileName);//获得文件过滤器Filefile=wrapper.getFiles("imgFile")[0];//检查扩展名StringfileExt=fileName.substring(fileName.lastIndexOf(".")+1).toLowerCase();if(!Arrays.<String>asList(extMap.get(dirName).split(",")).contains(fileExt)){out.println(getError("上传文件扩展名是不允许的扩展名。n只允许"+extMap.get(dirName)+"格式。"));return"error";}//检查文件大小if(file.length()>maxSize){out.println(getError("上传文件大小超过限制。"));return"error";}//重构上传图片的名称SimpleDateFormatdf=newSimpleDateFormat("yyyyMMddHHmmss");StringnewImgName=df.format(newDate())+"_"+newRandom().nextInt(1000)+"."+fileExt;byte[]buffer=newbyte[1024];//获取文件输出流FileOutputStreamfos=newFileOutputStream(savePath+"/"+newImgName);//获取内存中当前文件输入流InputStreamin=newFileInputStream(file);try{intnum=0;while((num=in.read(buffer))>0){fos.write(buffer,0,num);}}catch(Exceptione){e.printStackTrace(System.err);}finally{in.close();fos.close();}//发送给KEJSONObjectobj=newJSONObject();obj.put("error",0);obj.put("url",saveUrl+"/"+newImgName);out.println(obj.toJSONString());return"success";}privateStringgetError(Stringmessage){JSONObjectobj=newJSONObject();obj.put("error",1);obj.put("message",message);returnobj.toJSONString();}}
<script>ke.show({id:'kind',uploadJson:'newsUpload.action',//提交地址,action要自己配置fileManagerJson:'houtaimanage/editor/jsp/file_manager_json.jsp',allowFileManager:true,afterCreate:function(){varself=this;K.ctrl(document,13,function(){self.sync();document.forms['kingEditor'].submit();});K.ctrl(self.edit.doc,13,function(){self.sync();document.forms['kingEditor'].submit();});}});functionsubmit(){document.getElementById("kind").sync();//同步数据到服务器(图片,视频,音频什么的)//如果没有这一步对不起,你获取不到数据document.getElementById("kindEditor").submit();};</script><formid="kindEditor"method="post"action="newsUpload.action"enctype="multipart/form-data"><textareaid="kind"name="content"style="width:800px;height:400px;visibility:hidden;"></textarea><inputtype="button"id="uploadButton"value="Upload"onclick="submit()"/></form>
解决方案
解决方案二:
MultiPartRequestWrapper wrapper这个对象没有实例化?
解决方案三:
这个是我从别人那里复制过来的,都是这么写的
解决方案四:
你这里加个断点跟进去,看看实例化了没
解决方案五:
引用3楼defonds的回复:
你这里加个断点跟进去,看看实例化了没
实例化了,有值