com.aboutdata.web.controller.AvatarsController.java Source code

Java tutorial

Introduction

Here is the source code for com.aboutdata.web.controller.AvatarsController.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 com.aboutdata.web.controller;

import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @1 ??????
 * @2 nginx ?tomcat
 * @3 ?fastdfs ??????mysql?
 * @author Administrator
 */
@Controller("shopAvatarsController")
@RequestMapping("/avatars")
public class AvatarsController {

    Logger logger = LoggerFactory.getLogger(AvatarsController.class);

    /**
     * Get download from file-system
     *
     * @param avatar
     * @param t type png jpg
     * @param v ? v=1
     * @param s
     * @param response {@link HttpServletResponse}
     */
    @RequestMapping(value = "/{avatar}", method = RequestMethod.GET)
    public void download(@PathVariable("avatar") String avatar, String t, String v, String s,
            HttpServletResponse response) {
        if (StringUtils.isEmpty(s)) {
            File file = new File("/var/avatars/" + avatar + "." + t);
            try {
                FileUtils.copyFile(file, response.getOutputStream());
            } catch (IOException ex) {
                logger.error("error {}", ex);
                response.setHeader("message", "?");
                response.setStatus(404);
            }
        } else {
            File file = new File("/var/avatars/" + avatar + "-" + s + "." + t);
            try {
                FileUtils.copyFile(file, response.getOutputStream());
            } catch (IOException ex) {
                logger.error("error {}", ex);
                response.setHeader("message", "?");
                response.setStatus(404);
            }
        }

    }

}