package com.landtool.lanbase.modules.sys.controller; import com.alibaba.fastjson.JSONObject; import com.landtool.lanbase.config.SysTemPropertyConfig; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import com.landtool.lanbase.common.annotation.LogAction; import com.landtool.lanbase.common.annotation.SysLog; import com.landtool.lanbase.common.exception.LanbaseException; import com.landtool.lanbase.common.utils.*; import com.landtool.lanbase.modules.sys.entity.SysAttachment; import com.landtool.lanbase.modules.sys.service.SysAttachmentService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.util.Date; import java.util.List; import java.util.Map; /** * @author lanbase * @Description: TODO(附件) * @date 2017-7-10 17:07 */ @Controller @RequestMapping("/sys/attachment") @Api(value = "", tags = {"系统附件"}) public class SysAttachmentController extends AbstractController { @Autowired private SysAttachmentService sysAttachmentService; @Autowired private SysTemPropertyConfig sysProps; /** * 所有附件列表 */ @RequestMapping("/list") // @RequiresPermissions("sys:attachment:list") @ResponseBody @ApiOperation( value = "附件列表", notes = "所有附件列表" ) // @LogAction("查询") public Result list(@RequestParam Map params){ //查询列表数据 Query query = new Query(params); List configList=sysAttachmentService.queryList(query); int total=sysAttachmentService.queryTotal(query); PageUtils pageUtil=new PageUtils(configList, total, query.getLimit(), query.getPage()); return Result.ok().put("page", pageUtil); } /** * 附件信息 */ @RequestMapping("/info/{id}") @ResponseBody // @RequiresPermissions("sys:attachment:list") @ApiOperation( value = "附件信息", notes = "" ) public Result info(@ApiParam(name="id",value="文件id",required=true) @PathVariable("id") Long id){ SysAttachment attachment = sysAttachmentService.queryObject(id); return Result.ok().put("attachment", attachment); } /** * 上传文件 */ @RequestMapping("/upload") // @RequiresPermissions("sys:attachment:edit") @ApiOperation( value = "上传文件", notes = "" ) public String upload(HttpServletRequest request) { String callBackUrl = request.getParameter("callBackUrl"); String category = request.getParameter("category"); String whereStore = ""; try { String root = sysProps.getUploadPath().replace("/","\\"); String LinShiUrl=root+"\\temp"; List files=((MultipartHttpServletRequest) request).getFiles("file"); if(files.isEmpty()){ throw new LanbaseException("上传文件不能为空"); } for(MultipartFile file:files){ String suffix=FileUtils.getSuffix(file.getOriginalFilename()); String newFileName=AttachmentUtils.newFileName(suffix,LinShiUrl,category); File newFile=new File(newFileName); //临时文件处理 file.transferTo(newFile); String path=FileUtils.removePrefix(newFile.getAbsolutePath(),root).replace("\\", "/"); SysAttachment sysAttachment=new SysAttachment(); sysAttachment.setUserId(getUserId()); sysAttachment.setTitle(file.getOriginalFilename()); sysAttachment.setPath(path); sysAttachment.setSuffix(suffix); sysAttachment.setMimeType(file.getContentType()); sysAttachment.setCreateTime(new Date()); sysAttachmentService.save(sysAttachment); whereStore = sysAttachment.getPath(); } System.out.println("redirect:" + callBackUrl + "?code=0&textpath=" + whereStore); return "redirect:" + callBackUrl + "?code=0&textpath=" + whereStore; } catch (IOException e) { throw new LanbaseException("系统异常"); } } /** * 下载文件 */ @RequestMapping("/download/{id}") @ResponseBody // @RequiresPermissions("sys:attachment:list") @ApiOperation( value = "下载文件", notes = "" ) public void download(@ApiParam(name="id",value="文件id",required=true) @PathVariable("id") Long id, HttpServletResponse response) { SysAttachment attachment=sysAttachmentService.queryObject(id); if(attachment==null){ throw new LanbaseException("文件不存在"); } String path=sysProps.getUploadPath()+attachment.getPath(); FileUtils.download(path, attachment.getTitle(), response); } /** * 删除配置 */ @SysLog("删除配置") @RequestMapping("/delete") @ResponseBody // @RequiresPermissions("sys:attachment:edit") @ApiOperation( value = "删除配置", notes = "" ) // @LogAction("删除") public Result delete(@RequestBody Long[] ids){ sysAttachmentService.deleteBatch(ids); return Result.ok(); } }