package cn.cetc54.platform.base.controller.common;
|
|
import cn.cetc54.platform.base.entity.File;
|
import cn.cetc54.platform.base.manager.impl.LocalFileManage;
|
import cn.cetc54.platform.base.service.FileService;
|
import cn.cetc54.platform.core.common.exception.PlatformException;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiOperation;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.IOException;
|
import java.net.URLEncoder;
|
|
|
/**
|
* @author x
|
*/
|
@Slf4j
|
@Controller
|
@Api(description = "文件管理管理接口")
|
@RequestMapping("/platform/file")
|
@Transactional
|
public class FileController {
|
|
@Autowired
|
private FileService fileService;
|
|
|
|
|
@RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
|
@ApiOperation(value = "本地存储预览文件")
|
public void view(@PathVariable String id, HttpServletResponse response) throws IOException {
|
|
File file = fileService.get(id);
|
if(file==null){
|
throw new PlatformException("文件ID:"+id+"不存在");
|
}
|
response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(file.getFKey(), "UTF-8"));
|
response.setContentLengthLong(file.getSize());
|
response.setContentType(file.getType());
|
response.addHeader("Accept-Ranges", "bytes");
|
if(file.getSize()!=null&&file.getSize()>0){
|
response.addHeader("Content-Range", "bytes " + 0 + "-" + (file.getSize()-1) + "/" + file.getSize());
|
}
|
LocalFileManage.view(file.getUrl(), response);
|
}
|
}
|