package com.uas.report.controller; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import com.uas.report.core.exception.ReportException; import com.uas.report.service.FileService; import com.uas.report.support.SysConf; /** * 文件操作请求 * * @author sunyj * @since 2016年11月2日 下午2:05:04 */ @Controller @RequestMapping("/file") public class FileController { @Autowired private FileService fileService; @Autowired private SysConf sysConf; @RequestMapping("/autoDeploy") @ResponseBody public String autoDeploy(String userNames) { return fileService.autoDeploy(userNames); } @RequestMapping("/upload") @ResponseBody public String upload(String userName, String fileType, MultipartFile file) { return fileService.upload(userName, fileType, file); } /** * 下载文件 * * @param filePath * 文件路径 * @param isAbsolutePath * 文件路径是否为绝对路径 * @param response */ @RequestMapping("/download") public void download(String filePath, Boolean isAbsolutePath, HttpServletResponse response) { fileService.download(filePath, isAbsolutePath, response); } /** * 下载账套下的所有资源 * * @param userName * 账套名 * @param response */ @RequestMapping("/download/zip") public void downloadZip(String userName, HttpServletResponse response) { if (StringUtils.isEmpty(userName)) { throw new ReportException("未传入当前账套用户名!"); } String zipFilePath = fileService.getZip(userName); if (zipFilePath.isEmpty()) { throw new ReportException("压缩失败"); } fileService.download(zipFilePath, true, response); } /** * 下载模板 * * @param userName * 账套名称 * @param reportName * 模板名称 * @param response */ @RequestMapping("/download/jrxml") public void downloadJrxml(String userName, String reportName, HttpServletResponse response) { fileService.download(fileService.getJrxmlFilePath(userName, reportName), true, response); } /** * 下载标准模板 * * @param onlyData * 是否只获取数据 */ @RequestMapping("/standardJrxmls") @ResponseBody public Map standardJrxmls(String onlyData, HttpServletResponse response) { // 只返回字节数据,否则下载标准模板zip if (!StringUtils.isEmpty(onlyData) && (onlyData.equals("1") || onlyData.equals("true"))) { Map result = new HashMap<>(); // 不直接返回byte[],是因为客户端请求数据时,该请求可能会抛出异常, // 之后客户端获取byte[]数据,进行处理时难以判断数据是文件数据还是异常信息 result.put("data", fileService.getStandardJrxmls()); return result; } try { response.sendRedirect("download/zip?userName=" + sysConf.getStandardMaster()); return null; } catch (IOException e) { throw new ReportException(e).setDetailedMessage(e); } } @RequestMapping("/delete") @ResponseBody public String delete(String userName, String filePath) { // 账套为空,filePath为绝对路径,否则为账套下的相对路径 if (StringUtils.isEmpty(userName)) { return "Deleted... " + fileService.delete(filePath); } else { return "Deleted... " + fileService.delete(userName, filePath); } } @RequestMapping("/listFiles") @ResponseBody public List> listFiles(String fileRelativePath) { return fileService.listFiles(fileRelativePath); } }