Java學(xué)習(xí)

          java,spring,structs,hibernate,jsf,ireport,jfreechart,jasperreport,tomcat,jboss -----本博客已經(jīng)搬家了,新的地址是 http://www.javaly.cn 如果有對文章有任何疑問或者有任何不懂的地方,歡迎到www.javaly.cn (Java樂園)指出,我會盡力幫助解決。一起進(jìn)步

           

          用JSF和MyFaces上傳文件(2)——使用jsf中的inputFileUpload組件實(shí)現(xiàn)文件上傳下載

          <t:inputFileUpload id="fileupload" accept="WebContent/images/*"
          value="#{uploadCourse.learningpic}" storage="file"
          styleClass="fileUploadInput" required="true" />

          <h:panelGrid>
          <h:commandButton id="uploadButton" value="開始上傳"
          action="#{uploadCourse.upCourse}" type="submit" />
          </h:panelGrid>

          private UploadedFile learningpic;

          set() & get()

          public String upCourse() throws IllegalAccessException,
          InvocationTargetException, NoSuchMethodException ,IOException {
          Long a = new Long("1000000000000000");
          Double b = (a * Math.random());
          try {
          InputStream in = new BufferedInputStream(learningpic.getInputStream());
          try {
          byte[] buffer = new byte[64 * 1024];
          FileOutputStream fileOutputStream = new FileOutputStream("C:\\"+ b.toString() + ".jpg");
          while (in.read(buffer) > 0) {
          fileOutputStream.write(buffer);
          }
          } finally {
          in.close();
          }
          System.out.println("End");
          } catch (Exception e) {
          System.out.println("IOException");
          FacesMessage message = new FacesMessage(
          FacesMessage.SEVERITY_FATAL, e.getClass().getName(), e
          .getMessage());
          FacesContext.getCurrentInstance().addMessage(null, message);
          return "false";
          }


          UploadCourse uploadCourse = new UploadCourse();
          uploadCourse.setStatus("2");
          PropertyUtils.copyProperties(uploadCourse, this);

          if (uploadCourseService.upCourse(uploadCourse) == true) {
          return "success";
          } else {
          return "false";
          }

          }

          注意一定要在web.xml里加上filter,否則form將不能提交。
            <filter>
              <description>Set the size limit for uploaded files.
                      Format: 10 - 10 bytes
                      10k - 10 KB
                      10m - 10 MB
                      1g - 1 GB </description>
              <filter-name>extensionsFilter </filter-name>
              <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter </filter-class>
              <init-param>
                <param-name>uploadMaxFileSize </param-name>
                <param-value>10m </param-value>
              </init-param>
              <init-param>
                <description>Set the threshold size - files
                          below this limit are stored in memory, files above
                          this limit are stored on disk.

                          Format: 10 - 10 bytes
                          10k - 10 KB
                          10m - 10 MB
                          1g - 1 GB </description>
                <param-name>uploadThresholdSize </param-name>
                <param-value>100k </param-value>
              </init-param>
            </filter>
            <filter-mapping>
            <filter-name>extensionsFilter </filter-name>
            <servlet-name>Faces Servlet </servlet-name>
            </filter-mapping>


          posted on 2008-10-28 16:02 找個美女做老婆 閱讀(1772) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計

          公告

          本blog已經(jīng)搬到新家了, 新家:www.javaly.cn
           http://www.javaly.cn

          常用鏈接

          留言簿(6)

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 年辖:市辖区| 北票市| 五台县| 乌审旗| 财经| 乌鲁木齐县| 和政县| 海安县| 工布江达县| 宁德市| 汝南县| 资中县| 永川市| 远安县| 桑植县| 夹江县| 荣成市| 鄂尔多斯市| 辰溪县| 栾川县| 遂溪县| 扬州市| 专栏| 贵州省| 大连市| 普定县| 盱眙县| 卢龙县| 东安县| 社会| 濮阳市| 澄江县| 开封市| 墨脱县| 化隆| 黑山县| 南丰县| 惠东县| 彭州市| 米易县| 治县。|