com.iiifi.kite.boot.common.support.BaseController Maven / Gradle / Ivy
package com.iiifi.kite.boot.common.support;
import java.io.File;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.util.UriUtils;
import com.iiifi.kite.common.result.IResultCode;
import com.iiifi.kite.common.result.Result;
import com.iiifi.kite.common.result.SystemCode;
import com.iiifi.kite.common.util.Charsets;
import org.springframework.core.io.FileSystemResource;
/**
* 基础 controller
* @author [email protected] 花朝
* @date 2018/8/18 12:59
*/
public abstract class BaseController {
/**
* redirect跳转
*
* @param url 目标url
* @return 跳转地址
*/
protected String redirect(String url) {
return "redirect:".concat(url);
}
/**
* 返回成功
*
* @param 泛型标记
* @return Result
*/
protected Result success() {
return Result.success();
}
/**
* 成功-携带数据
*
* @param data 数据
* @param 泛型标记
* @return Result
*/
protected Result success(T data) {
return Result.success(data);
}
/**
* 根据状态返回成功或者失败
*
* @param status 状态
* @param msg 异常msg
* @param 泛型标记
* @return Result
*/
protected Result status(boolean status, String msg) {
return Result.status(status, msg);
}
/**
* 根据状态返回成功或者失败
*
* @param status 状态
* @param sCode 异常code码
* @param 泛型标记
* @return Result
*/
protected Result status(boolean status, IResultCode sCode) {
return Result.status(status, sCode);
}
/**
* 返回失败信息,用于 web
*
* @param msg 失败信息
* @param 泛型标记
* @return {Result}
*/
protected Result fail(String msg) {
return Result.fail(SystemCode.FAILURE, msg);
}
/**
* 返回失败信息
*
* @param rCode 异常枚举
* @param 泛型标记
* @return {Result}
*/
protected Result fail(IResultCode rCode) {
return Result.fail(rCode);
}
/**
* 返回失败信息
*
* @param rCode 异常枚举
* @param msg 失败信息
* @param 泛型标记
* @return {Result}
*/
protected Result fail(IResultCode rCode, String msg) {
return Result.fail(rCode, msg);
}
/**
* 下载文件
*
* @param file 文件
* @return {ResponseEntity}
*/
protected ResponseEntity download(File file) {
String fileName = file.getName();
return download(file, fileName);
}
/**
* 下载
*
* @param file 文件
* @param fileName 生成的文件名
* @return {ResponseEntity}
*/
protected ResponseEntity download(File file, String fileName) {
Resource resource = new FileSystemResource(file);
return download(resource, fileName);
}
/**
* 下载
*
* @param resource 资源
* @param fileName 生成的文件名
* @return {ResponseEntity}
*/
protected ResponseEntity download(Resource resource, String fileName) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
String encodeFileName = UriUtils.encode(fileName, Charsets.UTF_8);
// 兼容各种浏览器下载:
// https://blog.robotshell.org/2012/deal-with-http-header-encoding-for-file-download/
String disposition = "attachment;" +
"filename=\"" + encodeFileName + "\";" +
"filename*=utf-8''" + encodeFileName;
headers.set(HttpHeaders.CONTENT_DISPOSITION, disposition);
return new ResponseEntity<>(resource, headers, HttpStatus.OK);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy