随笔-295  评论-26  文章-1  trackbacks-0

/*
 * ViewUploadFile.java
 *
 * Created on 2006年2月4日, 上午1:29
 */

package path.system.manager;

import java.io.*;
import java.net.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;
import org.hibernate.Session;
import system.entity.CRM_UploadFile;
import.FileStorageService;

public class ViewUploadFile extends HttpServlet {
    private FileInputStream isFile = null;
   
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String szCoObject=request.getParameter("CoObject");
        String szID=request.getParameter("ID");
        String szMode = request.getParameter("Mode");
       
        //清空缓冲
        response.reset();
       
        OutputStream osOut = response.getOutputStream();
       
        if(szID == null || szID.equals("") || szID.equals("0"))
        {  
            byte[] cFileData = this.getNoDate (szCoObject);
           
            this.isFile.read(cFileData);
            osOut.write(cFileData);
        }
        else
        {
            Session ssSession = HibernateUtil.getSession();
            CRM_UploadFile csUpload;

            csUpload = (CRM_UploadFile)ssSession.load(CRM_UploadFile.class,Integer.valueOf(szID));

            String szFileName =csUpload.getFileName();
            if (szFileName==null) szFileName = "";
            String szRightName = new String( szFileName.getBytes(), "ISO8859_1" );      //中文文件名处理
            String szFileSize = csUpload.getFileSize().toString();
            String urlFileName = csUpload.getUrlFileName();
///输出到网页
            if ("0".equals(szFileSize)) {
                byte[] cFileData = this.getNoDate(szCoObject);
                this.isFile.read(cFileData);
                osOut.write(cFileData);
            } else {
                if(szMode.equals("Download")) {
                        response.setContentType("application/x-msdownload;");           //下载类型
                        response.setHeader("Content-Disposition","attachment; filename=" + szRightName);
                } else {
                        response.setContentType(csUpload.getContentType());
                        response.setHeader("Content-Disposition","filename=" + szRightName);
                }
                response.setHeader("content-length", szFileSize);
                //java.sql.Blob blob = csUpload.getBinaryData();
                File file = new File(FileStorageService.getRootPath() + urlFileName);
                if (!file.exists())
                    return;
               
                InputStream bis = new BufferedInputStream(new FileInputStream(file));
                byte[] buffer = new byte[2048];
                for (int i = bis.read(buffer); i > 0; i = bis.read(buffer))
                    osOut.write(buffer, 0, i);
                bis.close();
                   
            }
        }
       
        //防止超时
        //response.setStatus( response.SC_OK );
        response.flushBuffer();
    }
   
    private byte[] getNoDate (String szCoObject) throws IOException{
        String szFileName="";
        if(szCoObject.equals("Employee"))
            szFileName = getServletContext().getRealPath("/images/Employee/NoPhoto.gif");
        else
            szFileName = getServletContext().getRealPath("/images/Commodity/NoPhoto.gif");
        this.isFile= new FileInputStream(szFileName);

        int nSize = this.isFile.available();
        byte[] cFileData = new byte[nSize];
        return cFileData;
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
   
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
   
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}



大盘预测 国富论
posted on 2007-08-21 13:24 华梦行 阅读(108) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: