cn.zhuqi.mavenssh.web.servlet.ShowPic.java Source code

Java tutorial

Introduction

Here is the source code for cn.zhuqi.mavenssh.web.servlet.ShowPic.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.zhuqi.mavenssh.web.servlet;

import cn.zhuqi.mavenssh.ext.util.PropertyConfigurer;
import cn.zhuqi.mavenssh.model.User;
import cn.zhuqi.mavenssh.service.IUserService;
import cn.zhuqi.mavenssh.web.util.FTPClientTemplate;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author Administrator
 */
public class ShowPic extends HttpServlet {

    private IUserService userService;

    private static final String ip;
    private static final String username;
    private static final String pwd;
    private static final String attachment;

    static {
        PropertyConfigurer config = new PropertyConfigurer("ftp");
        ip = config.getString("ip");
        username = config.getString("username");
        pwd = config.getString("pwd");
        attachment = config.getString("attachment");
    }

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setHeader("Pragma", "No-cache");// ?????
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        // ?Application
        ServletContext application = this.getServletContext();
        String sid = request.getParameter("id");
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(application);

        // ?spring IOC
        userService = (IUserService) ctx.getBean(IUserService.class);
        User user = userService.findById(Integer.valueOf(sid));
        String imgFile = user.getPicurl();
        response.setContentType(application.getMimeType(imgFile));

        FTPClientTemplate ftpClient = new FTPClientTemplate(ip, 21, username, pwd);
        ServletOutputStream sos = response.getOutputStream();
        try {
            ftpClient.get(attachment + "/" + imgFile, sos);
        } catch (Exception ex) {
        }
    }

    // <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
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    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
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}