JSF檔案上傳與下載

來源:互聯網
上載者:User

JSF檔案上傳與下載
--使用myfaces

一、依賴的庫
myfaces相關以及tomahawk等

二、設定檔
修改web.xml,加入如下代碼
-------------------------------------------------------
 <filter>
  <filter-name>extensionsFilter</filter-name>
  <filter-class>
   org.apache.myfaces.webapp.filter.ExtensionsFilter
  </filter-class>
  <init-param>
   <param-name>uploadMaxFileSize</param-name>
   <param-value>100m</param-value>
  </init-param>
  <init-param>
   <param-name>uploadThresholdSize</param-name>
   <param-value>100k</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>extensionsFilter</filter-name>
  <url-pattern>*.html</url-pattern>
 </filter-mapping>
-------------------------------------------------------
 
修改faces-config.xml,加入如下代碼
-------------------------------------------------------
 <component>
  <component-type>
   org.apache.myfaces.HtmlInputFileUpload
  </component-type>
  <component-class>
   org.apache.myfaces.custom.fileupload.HtmlInputFileUpload
  </component-class>
 </component>
 <converter>
  <converter-for-class>
   org.apache.myfaces.custom.fileupload.UploadedFile
  </converter-for-class>
  <converter-class>
   org.apache.myfaces.custom.fileupload.UploadedFileConverter
  </converter-class>
 </converter>
 <render-kit>
  <render-kit-id>HTML_BASIC</render-kit-id>

  <!-- extended standard renderers -->
  <renderer>
   <component-family>javax.faces.Input</component-family>
   <renderer-type>org.apache.myfaces.FileUpload</renderer-type>
   <renderer-class>
    org.apache.myfaces.custom.fileupload.HtmlFileUploadRenderer
   </renderer-class>
  </renderer>
 </render-kit>
-------------------------------------------------------

三、設計上傳頁面

<h:form id="sendmail" enctype="multipart/form-data" >
<t:inputFileUpload id="fileupload"
                               value="#{oaMailMainForm.myFile}"        
                               storage="file"
                               styleClass="fileUploadInput"
                               maxlength="200000"/>
<f:verbatim><br></f:verbatim>

<h:commandButton image="/images/mail/sendfile.gif"
               action="#{oaMailMainForm.upload}" />
</h:form>

四、oaMailMainForm的實現
import org.apache.myfaces.custom.fileupload.UploadedFile;

private UploadedFile myFile;

 public String upload() throws IOException {
  if(myFile==null)
   return null;
  log.debug("upload........" + myFile.getName());
  String fileName=myFile.getName(); //獲得檔案的全名,含路徑
  if(!fileName.equals("")){
   File temp = new File(fileName);
   fileName = temp.getName();//獲得檔案名稱
   }
   log.debug(fileName);
  }
  byte[] buffer = new byte[new Long(myFile.getSize()).intValue()];
  buffer=myFile.getBytes();
 
  //attach set it's properties
  attach.setAtFile(Hibernate.createBlob(buffer));
  attach.setAtSize(new Long(buffer.length));
  oaMailAttachGroup.add(attach);
 
  //then you should save oaMailAttachGroup
 
  return null;
 }
 
五、下載
public String download() {
  OaMailAttach oaMailAttach = getOaMailAttach();

  try {
   FacesContext ctx = FacesContext.getCurrentInstance();
   String contentType = "application/octet-stream;charset=utf-8";
   HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
   response.setContentType(contentType);

   StringBuffer contentDisposition = new StringBuffer();

   contentDisposition.append("attachment;");

   contentDisposition.append("filename=/"");
   contentDisposition.append(oaMailAttach.getAtFilename());
   contentDisposition.append("/"");
   log.debug(System.getProperty("file.encoding"));
   response.setHeader("Content-Disposition", new String(contentDisposition.toString().getBytes(System.getProperty("file.encoding")),"iso8859_1"));
   log.debug(contentDisposition.toString());
   ServletOutputStream out = response.getOutputStream();

   log.debug(new Long(oaMailAttach.getAtFile().length()));

   byte[] bytes = new byte[0xffff];
   InputStream is = oaMailAttach.getAtFile().getBinaryStream();
   int b = 0;
   while ((b = is.read(bytes, 0, 0xffff)) > 0) {
    out.write(bytes, 0, b);
   }
   is.close();
   out.flush();
   ctx.responseComplete();
  } catch (Exception e) {
   // TODO 自動產生 catch 塊
   e.printStackTrace();
  }
  return null;
 }
 
 六、下載頁面設計

<h:form id="d">
<h:commandButton value="download" action="#{fileUploadForm.download}"></h:commandButton>           
</h:form>

 本文轉自http://hi.baidu.com/mymzd/blog/item/6195c011c3507010b8127b74.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.