package zr.zrpower.web.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import zr.zrpower.common.util.SysPreperty;


public class ServerFile
    extends HttpServlet {

  //Initialize global variables
  public void init() throws ServletException {
  }

  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
       request.setCharacterEncoding("GBK");
       response.setContentType("text/html; charset=GBK");


       int iType = 0;
       String path = SysPreperty.getProperty().UploadFilePath+"/";
       if(request.getParameter("type")!=null){
          iType = Integer.parseInt(request.getParameter("type"));
       }

       String name1 = request.getParameter("name");
       String file = request.getParameter("file");
       String name = file.substring(file.indexOf("/")+1);
       String ext = file.substring(file.indexOf("."));




       switch(iType){
          case 0 : path = SysPreperty.getProperty().UploadFilePath +"/";
                   break;
          case 1 : path = SysPreperty.getProperty().UploadImagePath +"/";
                   break;
          case 2 : path = SysPreperty.getProperty().IcoPath +"/";
                   break;
          case 4 : path = SysPreperty.getProperty().ShowTempletPath+"/";
                   break;
          case 5 : path = SysPreperty.getProperty().PrintTempletPath+"/";
                   break;
          case 6 :
                   path = SysPreperty.getProperty().UploadFilePath +"/AttManage/";
                   name = name1;
                   break;
          case 7 :
                   path = SysPreperty.getProperty().LogFilePath;
                   path = path.substring(0,path.length()-3)+"StatExcel/";
                   break;
       }



       try
       {
         path=path+file;

         java.io.OutputStream os = response.getOutputStream();
         java.io.FileInputStream fis = new java.io.FileInputStream(path);


         if(ext.toLowerCase().equals("jpg")|ext.toLowerCase().equals("jpeg")){
           response.setContentType("image/jpeg");
         }else if(ext.toLowerCase().equals("gif")){
           response.setContentType("image/gif");
         }else{
           response.setContentType("unknown");
         }

         response.addHeader("Content-Disposition", "filename=\"" + name + "\"");
         //有的web服务器可能用下面的代码才正常。
         //response.addHeader("Content-Disposition","attachment; filename=\"" + new String(name.getBytes("gb2312"), "iso8859-1") + "\"");



         byte[] b = new byte[1024];
         int i = 0;

         while ( (i = fis.read(b)) > 0 )
         {
           os.write(b, 0, i);
         }

         fis.close();
         os.flush();
         os.close();

         os = null;
         fis = null;
       }
       catch ( Exception e )
       {

       }

  }

  //Process the HTTP Post request
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    doGet(request, response);
  }

  //Clean up resources
  public void destroy() {
  }
}
