Upload and download OAF files using Java I/O

Source: Internet
Author: User

File Upload
First, create a temporary View Object (View Object) for storing attachments, including the View Attribute of the Blob type, create a page that contains the messageFileUpload type Item, and create a page controller.
Write the following code in processFormRequest of the controller:
Try
{
OAViewObject vo = (OAViewObject) am. findViewObject ("FileVO1 ");
Row row = vo. getCurrentRow ();
  
String filePath = "/u01/demo10/demo10comn/temp /";
  
BlobDomain blob = (BlobDomain) row. getAttribute ("FileData ");
File f = new File (filePath + (String) row. getAttribute ("FileName "));
InputStream is = blob. getBinaryStream ();
FileOutputStream fos = new FileOutputStream (f );
  
Int size = blob. getBufferSize ();
Byte [] buffer = new byte [size];
Int len;
  
While (len = is. read (buffer ))! =-1)
{
Fos. write (buffer, 0, len );
}
Is. close ();
Fos. flush ();
Fos. close ();
}
Catch (IOException ex)
{
Ex. printStackTrace ();
System. out. println (ex. getMessage ());
}
  
File Download
The function is to automatically open the attachment save dialog box when you click the attachment link. The Code is as follows:
Try
{
OAViewObject vo = (OAViewObject) am. findViewObject ("FileVO1 ");
Row row = vo. getCurrentRow ();
  
BlobDomain blob = (BlobDomain) row. getAttribute ("FileData ");
HttpServletResponse response = (HttpServletResponse) pageContext. getRenderingContext (). getServletResponse ();
Response. addHeader ("Content-Disposition", "attachment; filename =" + (String) row. getAttribute ("FileName "));
Response. setContentType ("application/x-msdownload ");
  
InputStream is = blob. getBinaryStream ();
Int size = blob. getBufferSize ();
Byte [] buffer = new byte [size];
Int len;
  
While (len = is. read (buffer ))! =-1)
{
Response. getOutputStream (). write (buffer, 0, len );
}
Is. close ();
Response. getOutputStream (). flush ();
Response. getOutputStream (). close ();
}
Catch (IOException ex)
{
Ex. printStackTrace ();
System. out. println (ex. getMessage ());
}

 

Author: "Soy Milk"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.