t:inputFileUpload的使用 我想用这个控件实现上传功能,但是不知道该怎么写我是新手哪位朋友有代码给我贴一下吧,最好是有注释的。页面,后台bean,配置文件,这些都应该注意些什么呢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://blog.sina.com.cn/s/blog_602feaa80100guy6.html 一:采用myfaces标签进行上传<t:inputFileUpload styleClass="fileupload" value="#{QuploadBean.upFile}" size="60"> </t:inputFileUpload>其中upFile是myfaces.custom.FileUpload.uploadedFile的一个对象,通过这样的绑定,在jsf的back bean中就可以得到上传文件的inputStream:InputStream in = upFile.getInputStream();---获得输入流 然后在服务器中生成一个以时间命名的文件,用outputStream将获得的输入byte,write到该文件中。 Date date = new Date();// 获取当前系统的时间 SimpleDateFormat s = new SimpleDateFormat("yyyyMMddHHmmss");// 格式化日期 String dateStr = s.format(date); // 转为字符串 int ms = Calendar.getInstance().get(Calendar.MILLISECOND);// 得到当前的毫秒数 String fileName = dateStr + String.valueOf(ms) + ".xml";// 以日期命名xml文件 String serverPath = getServerPath() + "\\upload\\";// 文件上传的目标目录 serverPath = checkPath(serverPath);// 判断该目录是否存在,不存在则创建 String filePath = serverPath + fileName;OutputStream os= new FileOutputStream(filePath); os.write(upFile.getBytes()); os.close(); ps:commandButton的action属性主要是用于页面转向,而actionListener主要是处理一些业务。在提交页面时会先执行actionlistener,然后再执行action对应的方法。二:如何得到该工程所在的文件夹路径此问题碰到过很多次,每次都要查!!!public String getServerPath() { javax.Faces.context.FacesContext fc = FacesContext.getCurrentInstance(); HttpServletRequest request = (HttpServletRequest) fc .getExternalContext().getRequest(); String filePath = request.getSession().getServletContext().getRealPath( "/");// 通过上下文取得应用程序在服务器上的真实路径 File fi = new File(filePath); String fil = fi.getParent(); return fil;-----E:\platform\apache-tomcat-5.5.25-new\webapps } 刚刚搞定一个文件上传功能,就是用Servlet,用common-fileupload.jar 谢谢,已经解决了,我是用的这句话,少些很多代码的,哈哈 FileUtils.copyInputStreamToFile(file[0].getInputStream(), new File(dirPath+"/"+fileNameStr)); run on server 求一个简单递归的树型菜单!谢 不同项目之间的action访问问题 JSF中DataTable处理多选的问题 请问如何做才能防止页面在按住“ctrl”+滚动鼠标滚轮文字产生变化? JSP标签的嵌套问题 小白求教 关于小型机上+weblogic上出现的编码问题,导致getParameter根本取不到值(在线等待) 关于JSP中使用JAVABEAN的存放路径问题? 请问怎样用JAVAMAIL实现自动给用户发送邮件的功能?弄好了送100分 在JSP页面显示本地硬盘上的多个图片,图片不在项目webRoot目录 http和socket通讯问题
</t:inputFileUpload>其中upFile是myfaces.custom.FileUpload.uploadedFile的一个对象,通过这样的绑定,在jsf的back bean中就可以得到上传文件的inputStream:InputStream in = upFile.getInputStream();---获得输入流 然后在服务器中生成一个以时间命名的文件,用outputStream将获得的输入byte,write到该文件中。 Date date = new Date();// 获取当前系统的时间
SimpleDateFormat s = new SimpleDateFormat("yyyyMMddHHmmss");// 格式化日期
String dateStr = s.format(date); // 转为字符串
int ms = Calendar.getInstance().get(Calendar.MILLISECOND);// 得到当前的毫秒数
String fileName = dateStr + String.valueOf(ms) + ".xml";// 以日期命名xml文件
String serverPath = getServerPath() + "\\upload\\";// 文件上传的目标目录
serverPath = checkPath(serverPath);// 判断该目录是否存在,不存在则创建
String filePath = serverPath + fileName;OutputStream os= new FileOutputStream(filePath);
os.write(upFile.getBytes());
os.close(); ps:commandButton的action属性主要是用于页面转向,而actionListener主要是处理一些业务。在提交页面时会先执行actionlistener,然后再执行action对应的方法。二:如何得到该工程所在的文件夹路径此问题碰到过很多次,每次都要查!!!public String getServerPath() { javax.Faces.context.FacesContext fc = FacesContext.getCurrentInstance(); HttpServletRequest request = (HttpServletRequest) fc
.getExternalContext().getRequest();
String filePath = request.getSession().getServletContext().getRealPath(
"/");// 通过上下文取得应用程序在服务器上的真实路径
File fi = new File(filePath);
String fil = fi.getParent(); return fil;-----E:\platform\apache-tomcat-5.5.25-new\webapps
}
谢谢,已经解决了,我是用的这句话,少些很多代码的,哈哈 FileUtils.copyInputStreamToFile(file[0].getInputStream(), new File(dirPath+"/"+fileNameStr));