cn.guoyukun.spring.web.controller.DownloadController.java Source code

Java tutorial

Introduction

Here is the source code for cn.guoyukun.spring.web.controller.DownloadController.java

Source

/**
 * Copyright (c) 2005-2012 https://github.com/zhangkaitao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package cn.guoyukun.spring.web.controller;

import cn.guoyukun.spring.jpa.Constants;
import cn.guoyukun.spring.web.upload.FileUploadUtils;
import cn.guoyukun.spring.web.utils.DownloadUtils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.net.URLDecoder;

/**
 * /
 * <p>User: 
 * <p>Date: 13-2-11 ?8:46
 * <p>Version: 1.0
 */
@Controller
public class DownloadController {

    /**
     * ??
     */
    private String prefixFilename = "[es]";

    @RequestMapping(value = "/download")
    public String download(HttpServletRequest request, HttpServletResponse response,
            @RequestParam(value = "filename") String filename) throws Exception {

        filename = filename.replace("/", "\\");

        if (StringUtils.isEmpty(filename) || filename.contains("\\.\\.")) {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("??");
            return null;
        }
        filename = URLDecoder.decode(filename, Constants.ENCODING);

        String filePath = FileUploadUtils.extractUploadDir(request) + "/" + filename;

        DownloadUtils.download(request, response, filePath, prefixFilename + filename);

        return null;
    }

}