问题描述
想做一个文件上传的功能,但是不管怎么整都没整成。我的方法是当表单提交后,在第二个页面通过request把文件数据取出来,并保存至我想要保存的位置,还有要取上传文件的名称。大小。谁有源码能否给一份,谢谢了。或教下怎么个弄法呀
解决方案
解决方案二:
表单提交到servlet,servlet里就把文件保存了,第二个页面是什么。莫非你是在第二个页面的<%%>里保存文件,这样也不是不可以
解决方案三:
可以考虑用其他作用于保存信息
解决方案四:
和第2个页面为什么扯上关系
解决方案五:
这种问题高手们束手无策
解决方案六:
只想在纯JSP<%%>中来保存,另外有没有人做过SOCKER呢
解决方案七:
http://blog.sina.com.cn/s/blog_4cc16fc50100bw3n.html
解决方案八:
直接一个页面就搞定啊,你存到任意位置,之后把路径保存起来就完事了
解决方案九:
现在网上不是有很多第三方的保存文件功能吗?
解决方案十:
packagecom.tqedu.crm.action;importjava.io.File;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.List;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.apache.commons.fileupload.FileItem;importorg.apache.commons.fileupload.FileUploadException;importorg.apache.commons.fileupload.disk.DiskFileItemFactory;importorg.apache.commons.fileupload.servlet.ServletFileUpload;importcom.google.gson.Gson;importcom.tqedu.crm.Constants;importcom.tqedu.crm.log.TqLog;importcom.tqedu.crm.util.Tools;importcom.tqedu.crm.util.UUIDGenerator;/**@authorFlex*文件上传,所有文件上传都使用些Servlet+jquery-uploadify组件完成上传工作*文件上传完成后,返回以下信息:{success:true/false,msg:"上传成功/上传失败,读取文件出错!",filepath:"upload/stu_head_img/12342343212321312.png"}文件需要下载时,请直接使用:域名+filepath,即可下载****/publicclassMultiUploadServletextendsHttpServlet{privatestaticfinallongserialVersionUID=1L;privateTqLoglogger=newTqLog(MultiUploadServlet.class);protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{this.doPost(request,response);}protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse){StringsubDir=request.getParameter("savepath");if(Tools.isEmpty(subDir)){subDir=Constants.FilePath.UPLOAD_DIR_DEFAULT;//如果不指定上传子目录}StringrealPath=Constants.FilePath.UPLOAD_DIR_ROOT+Constants.FilePath.SEPARATOR+subDir;StringrootPath=this.getServletConfig().getServletContext().getRealPath(Constants.FilePath.SEPARATOR);logger.info("上传根路径:"+rootPath);Filef1=newFile(rootPath+realPath);logger.info("上传子路径:"+rootPath+realPath);if(!f1.exists()){f1.mkdirs();}DiskFileItemFactoryfac=newDiskFileItemFactory();ServletFileUploadupload=newServletFileUpload(fac);upload.setHeaderEncoding("UTF-8");List<FileItem>fileList=null;try{fileList=upload.parseRequest(request);if(fileList==null||fileList.isEmpty()){writer(response,newMessage(false,"请选择要上传的文件!",null));return;}}catch(FileUploadExceptionex){logger.error("文件上传出错,网络中断或用户取消了上传操作!",ex);writer(response,newMessage(false,"文件上传出错,网络中断或用户取消了上传操作!",null));return;}for(FileItemitem:fileList){if(!item.isFormField()){Stringname=item.getName();if(Tools.isEmpty(name)){continue;}StringextName=null;if(name.lastIndexOf(".")>=0){extName=name.substring(name.lastIndexOf("."));}else{extName=".temp";}//upload/stu_head_img/ads87fds6f76sdaf7as6dfsadf.pngrealPath=realPath+Constants.FilePath.SEPARATOR+UUIDGenerator.getUUID()+Tools.getRandomNum(9999)+extName;Filetempfile=newFile(rootPath+realPath);try{item.write(tempfile);}catch(Exceptione){logger.info("上传失败!");writer(response,newMessage(false,"文件上传失败!",null));return;}logger.info("文件保存路径为:"+tempfile.getAbsolutePath());}else{logger.info(item.getFieldName()+""+item.getString());}}logger.info("上传成功!");writer(response,newMessage(true,"文件上传成功!",realPath));}privatevoidwriter(HttpServletResponseresponse,Messagemsg){try{response.setContentType("text/javascript;charset=utf-8");response.setCharacterEncoding("UTF-8");PrintWriterpw=response.getWriter();pw.print(newGson().toJson(msg));pw.flush();pw.close();}catch(Exceptione){logger.error("服务器响应IO错误!");}}classMessage{privatebooleansuccess;privateStringmsg;privateStringfilepath;publicMessage(){}publicMessage(booleansuccess,Stringmsg,Stringfilepath){super();this.success=success;this.msg=msg;this.filepath=filepath;}publicbooleangetSuccess(){returnthis.success;}publicvoidsetSuccess(booleansuccess){this.success=success;}publicStringgetMsg(){returnmsg;}publicvoidsetMsg(Stringmsg){this.msg=msg;}publicStringgetFilepath(){returnfilepath;}publicvoidsetFilepath(Stringfilepath){this.filepath=filepath;}}}
$(function(){//附件上传$("#uploadify").uploadify({'debug':false,//开启调试'auto':true,//选择后是否自动上传'method':"get",//使用post时,formData传递的参数后台无法获取'successTimeout':99999,//超时时间'queueID':'filelist',//需要上传的文件列表的显示位置(容器)'width':'80',//按钮宽度'height':'30',//按钮高度'buttonText':'选择附件',//按钮文字,与buttonImage选择其一即可'swf':'images/jquery-uploadify.swf',//swf文件路径'uploader':'/MultiUploadServlet',//上传地址'formData':{'savepath':'stu_excel'//upload目录不用写,直接写子目录},'fileTypeDesc':'附件格式',//描述'fileTypeExts':'*.xls;*.xlsx;',//文件类型'fileSizeLimit':'30MB',//文件大小'queueSizeLimit':'1',//最多同时上传文件数目'fileObjName':'uploadify',//服务器端脚本使用的文件对象的名称'successTimeout':'5',//超时时间'requeueErrors':false,'removeTimeout':'1','removeCompleted':true,//上传完后是否从移除queueID容器中移除'onSelectError':function(file,errorCode,errorMsg){//返回一个错误,选择文件的时候触发switch(errorCode){case-100:varlength=$('#uploadify').uploadify('settings','queueSizeLimit');alert("只多只能上传"+length+"个文件!");break;case-110:varsize=$('#uploadify').uploadify('settings','fileSizeLimit');alert("文件["+file.name+"]超出限制大小:"+size+"!");break;case-120:alert("文件["+file.name+"]大小异常!");break;}returnfalse;},'onFallback':function(){alert("您未安装FLASH控件,无法上传图片!请安装FLASH控件后再试。");},'onUploadSuccess':function(file,data,response){if(response){varobj=eval('('+data+')');if(obj.success){$("#upload_result").html("《"+file.name+"》 请点击开始导入</font>");$("#appendix").val(obj.filepath);$("#appendixFileName").val(file.name);}else{alert(obj.msg);}}else{alert("文件上传失败,请检查网络!");}}});});
页面需要这三个<linktype="text/css"rel="stylesheet"href="css/jquery-uploadify.css"/><scripttype="text/javascript"src="/js/jquery/jquery-1.9.1.min.js"></script><scripttype="text/javascript"src="js/jquery/jquery.uploadify.min.js"></script>百度uploadify就可以找到这三个