java常用类 blobFileBean

package com.whlongyi.sys.blob.bean;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.myfaces.custom.fileupload.UploadedFile;

import com.whlongyi.common.util.FacesUtils;
import com.whlongyi.sys.blob.bo.BlobFile;
import com.whlongyi.sys.blob.service.BlobFileService;

public class BlobFileBean {
 
 public final static String uploadPath="uploadFile";
 
 private BlobFileService blobFileService;

 public BlobFileService getBlobFileService() {
  return blobFileService;
 }

 public void setBlobFileService(BlobFileService blobFileService) {
  this.blobFileService = blobFileService;
 }
 
 public String downLoadFile()
 {
  String blobId=FacesUtils.getRequestParameter("blobFileID");
  BlobFile blobVO=blobFileService.getBlobFile(Integer.parseInt(blobId));
  HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
  ServletContext servletContext=FacesUtils.getServletContext();
        InputStream s = new java.io.ByteArrayInputStream(blobVO.getFileContent());
        BufferedInputStream bis =  new BufferedInputStream(s);
        String realName=blobVO.getName();
        String extendName=null;
        try {
            String mimeType = null;
            int pos = realName.lastIndexOf(".");
            if (pos >= 0 && (pos + 1) < realName.length()) {
                extendName = realName.substring(pos + 1);
            }
            if (extendName != null)
                mimeType = servletContext.getMimeType(extendName);
            if (mimeType == null)
                mimeType = "APPLICATION/OCTET-STREAM";

            response.setContentType(mimeType);
            response.setHeader("Content-Disposition", "attachment; filename=/"" + URLEncoder.encode(realName, "UTF-8") + "/"");
            byte[] data = new byte[1024];
            OutputStream out = response.getOutputStream();
            int length=0;
            do {
                length = bis.read(data);
                if (length != -1) {
                    out.write(data, 0, length);
                }
            } while (length > 0);
            out.flush();
            out.close();
            data = null;
            //
            response.setStatus(HttpServletResponse.SC_OK);
            response.flushBuffer();
        } catch (IOException ex) {
            response.setContentType("text/html");
            response.setHeader("Content-Disposition", "");
        } finally {
            try {
                if (bis != null)
                    bis.close();
            } catch (IOException ex) {
            }
        }
        FacesContext.getCurrentInstance().responseComplete();
  return null;
 }
 
 public static String uploadFileToDisk(UploadedFile myFile)
 {
  try {
   long time = System.currentTimeMillis();
   InputStream in = myFile.getInputStream();
   FacesContext ctx = FacesContext.getCurrentInstance();
   HttpSession hs = (HttpSession) ctx.getExternalContext().getSession(true);
   String directory = hs.getServletContext().getRealPath("//")+uploadPath;
   File fileDir = new File(directory);
   if (!fileDir.exists()) {
    fileDir.mkdir();
   }
   String fileName=String.valueOf(Thread.currentThread().getContextClassLoader().hashCode())+"_"+time+myFile.getName().substring(myFile.getName().lastIndexOf("."));
   String file = hs.getServletContext().getRealPath("//") + uploadPath+"//"+ fileName;
   BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
   byte[] buffer = new byte[1024 * 2];
   int length;
   while ((length = in.read(buffer, 0, buffer.length)) > 0) {
    out.write(buffer, 0, length);
   }
   out.flush();
   out.close();
   return "//" + uploadPath+"//"+ fileName;
  } catch (Exception e) {
   e.printStackTrace();
  }
  return null;
 }
 

 public String downLoadFileFromDisk()
 {
//  String blobId=FacesUtils.getRequestParameter("blobFileID");
//  BlobFile blobVO=blobFileService.getBlobFile(Integer.parseInt(blobId));
  String filepath=FacesUtils.getRequestParameter("filepath");;
  String realName=FacesUtils.getRequestParameter("realName");;
  HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
  ServletContext servletContext=FacesUtils.getServletContext();
        InputStream s=null;
  try {
   s = new BufferedInputStream(new FileInputStream(filepath));
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
        BufferedInputStream bis =  new BufferedInputStream(s);
        String extendName=null;
        try {
            String mimeType = null;
            int pos = realName.lastIndexOf(".");
            if (pos >= 0 && (pos + 1) < realName.length()) {
                extendName = realName.substring(pos + 1);
            }
            if (extendName != null)
                mimeType = servletContext.getMimeType(extendName);
            if (mimeType == null)
                mimeType = "APPLICATION/OCTET-STREAM";

            response.setContentType(mimeType);
            response.setHeader("Content-Disposition", "attachment; filename=/"" + URLEncoder.encode(realName, "UTF-8") + "/"");
            byte[] data = new byte[1024];
            OutputStream out = response.getOutputStream();
            int length=0;
            do {
                length = bis.read(data);
                if (length != -1) {
                    out.write(data, 0, length);
                }
            } while (length > 0);
            out.flush();
            out.close();
            data = null;
            //
            response.setStatus(HttpServletResponse.SC_OK);
            response.flushBuffer();
        } catch (IOException ex) {
            response.setContentType("text/html");
            response.setHeader("Content-Disposition", "");
        } finally {
            try {
                if (bis != null)
                    bis.close();
            } catch (IOException ex) {
            }
        }
        FacesContext.getCurrentInstance().responseComplete();
  return null;
 }
 
 
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值