问题描述
急,求上传例子,百度那些我都看过,有没有真实的例子。
解决方案
解决方案二:
解决方案三:
马上能给你写一个简单的例子,^_^
解决方案四:
你自己参照着改成s2的吧
解决方案五:
action://文件上传下载相关属性privateFilefile;privateStringfilename;privateStringfileFileName;privateStringfileContentType;privateStringsavePath;//上传文件publicStringupload()throwsException{//建立文件输出流Stringpath=ServletActionContext.getServletContext().getRealPath(getSavePath());FileOutputStreamfos=newFileOutputStream(path+"\"+getFileFileName());//建立文件上传流FileInputStreamfis=newFileInputStream(getFile());byte[]buffer=newbyte[1024];intlen=0;while((len=fis.read(buffer))>0){fos.write(buffer,0,len);}fis.close();fos.close();}
struts.xml:<!--系统支持的最大上传文件--><constantname="struts.multipart.maxSize"value="100000000"/><actionname="upload"class="com.oa.action.DocumentAction"><paramname="savePath">upload</param><!--配置上传拦截器--><interceptor-refname="fileUpload"><!--限制上传的文件类型--><paramname="allowedTypes">application/msword,application/zip,application/vnd.ms-excel,text/plain,application/pdf,application/vnd.ms-powerpoint</param><!--限制文件上传大小,最大为5M--><paramname="maximumSize">5242880</param></interceptor-ref><interceptor-refname="defaultStack"/><result>/Document/Upload.jsp</result><resultname="input">/Document/Upload.jsp</result></action>
解决方案六:
解决方案七:
我这里有个demo,要的话,一手给分,一手交货
解决方案八:
给你一个更好的例子,在我的博客里,Struts实现异步上传下载,很详细,你可以去看一下:http://blog.csdn.net/harderxin/article/details/9022439
解决方案九:
packagecom.zsq.sful.action;importjava.io.File;importorg.apache.commons.fileupload.FileUpload;importorg.apache.commons.io.FileUtils;importorg.apache.struts2.ServletActionContext;importcom.opensymphony.xwork2.ActionContext;importcom.opensymphony.xwork2.ActionSupport;importcom.sun.faces.application.ApplicationAssociate;publicclassUpLoadActionextendsActionSupport{privateFileimage;privateStringimageFileName;publicFilegetImage(){returnimage;}publicvoidsetImage(Fileimage){this.image=image;}publicStringgetImageFileName(){returnimageFileName;}publicvoidsetImageFileName(StringimageFileName){this.imageFileName=imageFileName;}publicStringexecute()throwsException{if(!(image==null)){Stringfilename=ServletActionContext.getServletContext().getRealPath("/image");FilesavePath=newFile(newFile(filename),imageFileName);if(!savePath.exists())savePath.getParentFile().mkdirs();FileUtils.copyFile(image,savePath);System.out.println(filename);ActionContext.getContext().put("message","上传成功");}return"success";}}具体的action。。。在页面上只写一个文本框即可,
jsp页面:<body><formenctype="multipart/form-data"action="UpLoad"method="post">文件:<inputtype="file"name="image"><inputtype="submit"value="上传"/></form></body>
struts.xml<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd"><struts><packagename="admin"namespace="/"extends="struts-default"><global-results><result>afterupload.jsp</result></global-results><actionname="UpLoad"class="com.zsq.sful.action.UpLoadAction"></action></package></struts>
自己写一个afterupload.jsp页面。。。
解决方案十:
引用6楼u012480848的回复:
我这里有个demo,要的话,一手给分,一手交货
好坑。。。
解决方案十一:
引用8楼zhouren1314的回复:
packagecom.zsq.sful.action;importjava.io.File;importorg.apache.commons.fileupload.FileUpload;importorg.apache.commons.io.FileUtils;importorg.apache.struts2.ServletActionContext;importcom.opensymphony.xwork2.ActionContext;importcom.opensymphony.xwork2.ActionSupport;importcom.sun.faces.application.ApplicationAssociate;publicclassUpLoadActionextendsActionSupport{privateFileimage;privateStringimageFileName;publicFilegetImage(){returnimage;}publicvoidsetImage(Fileimage){this.image=image;}publicStringgetImageFileName(){returnimageFileName;}publicvoidsetImageFileName(StringimageFileName){this.imageFileName=imageFileName;}publicStringexecute()throwsException{if(!(image==null)){Stringfilename=ServletActionContext.getServletContext().getRealPath("/image");FilesavePath=newFile(newFile(filename),imageFileName);if(!savePath.exists())savePath.getParentFile().mkdirs();FileUtils.copyFile(image,savePath);System.out.println(filename);ActionContext.getContext().put("message","上传成功");}return"success";}}具体的action。。。在页面上只写一个文本框即可,jsp页面:<body><formenctype="multipart/form-data"action="UpLoad"method="post">文件:<inputtype="file"name="image"><inputtype="submit"value="上传"/></form></body>
struts.xml<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd"><struts><packagename="admin"namespace="/"extends="struts-default"><global-results><result>afterupload.jsp</result></global-results><actionname="UpLoad"class="com.zsq.sful.action.UpLoadAction"></action></package></struts>
自己写一个afterupload.jsp页面。。。
够详细,谢了
解决方案十二:
引用5楼huxiweng的回复:学习了。以前一直在富文本编辑器里面上传附件,,,