Struts 的文件上传组件org.apache.struts.upload

Struts 的文件上传组件org.apache.struts.upload

用过多种java的文件上传组件,感觉都不是太方便,或者在windows与linux下转换的兼容性不太好,或者只能处理文件上传,而不能同时处理文件和其他表单信息的传递。
前两天尝试用struts的upload组件,根据网上找到的一些资料,修改我项目中的一个Action、Actionform和ftl模板,使得它们支持org.apache.struts.upload。最初感觉很方便,一下子就成功了,可是打开上传后的文件,却遇到文件错误的提示。doc文件无法打开,jpg里面的图片只能看见一半,txt文件倒是能打开,不过也提示字符错误。不管文件大小是10k还是5m都一样出错。
再次到网上查找原因,有几个文章简单阐述了一下原因,说是struts源代码的问题,都要求修改struts源代码重新编译jar包。天,太麻烦了,难道没有简单的办法?
我用的是struts1.1,以前下过它的源码,可是不知放到哪里去了。也不知apache网址是否还有1.1的源码。幸好记得jbuilderx里自带一套struts1.1的源码,在C:/JBuilderX/extras/jakarta-struts-1.1-src/src。打开分析C:/JBuilderX/extras/jakarta-struts-1.1-src/src/share/org/apache/struts/upload里面的文件,看见doc-files下有一个uploadUML.jpg,里面清楚的标明了各个文件的关系。但实际上也没有用。
后来再仔细查看,找到C:/JBuilderX/extras/jakarta-struts-1.1-src/src/upload/org/apache/struts/webapp/upload里面带的一个例子。仔细对比自己的Action中代码,终于发现一些不同,原来是用InputStream进行读取,OutputStream写出。转换到我的项目实际代码后如下:
        FormFile file = form.getUpFile();
        String realPath = this.getServlet().getServletContext().getRealPath("/uploads/");
        try{
          InputStream stream = file.getInputStream();
          OutputStream bos = new FileOutputStream(realPath+"
//"+file.getFileName());
          int bytesRead = 0;
          byte[] buffer = new byte[8192];
          while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
              bos.write(buffer, 0, bytesRead);
          }
          bos.close();

         } catch (IOException ioe) {
           //handle exceptions
         }

重新编译我的Action,上传测试,前面说的问题全部解决。OK。
开发环境:JBX、JDK1.5、Tomcat4.1.27、Struts1.1。

xiaozhen.com附:刚才文件写完以后,突然注意到一点,这里的buffer设置是8192,而我原来用别的办法遇到文件只能上传一半时,这个值好像是1024,不清楚是否跟它有关。懒得再试,感兴趣的朋友可以自行尝试。

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elabor
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值