123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- package com.idea.util;
- import com.rockstar.common.domain.AjaxResult;
- import com.rockstar.common.file.FileUtils;
- import com.rockstar.frame.model.FrameData;
- import com.rockstar.frame.model.FrameDataExample;
- import com.rockstar.frame.service.FrameDataService;
- import com.rockstar.util.StringUtils;
- import io.swagger.annotations.Api;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.*;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.*;
- import java.util.*;
- /**
- * 自用文件相关的方法
- */
- @Controller
- @RequestMapping(value = "FileZtController")
- @Api(value = "文件相关信息")
- public class FileZtController {
- @Autowired
- private FrameDataService frameDataService;
- /**
- * 通过文件id获取文件的文件名,创建时间的信息
- *
- * @param ids
- * @param response
- * @param request
- */
- @PostMapping(value = "findFileInfoByIds/{ids}",produces = {"application/json;charset=UTF-8"})
- @ResponseBody
- public AjaxResult findFileInfoByIds(@PathVariable("ids") String ids, HttpServletResponse response, HttpServletRequest request) {
- System.out.println("findFileInfoByIds start");
- // for (String id : split) {
- // FrameData frameData = frameDataService.selectByPrimaryKey(id);
- // HashMap<String,Object> map=new HashMap<>();
- // map.put("fileName", frameData.getFileName());
- // map.put("createdAt",frameData.getCreatedAt());
- // map.put("id",id);
- // }
- if (StringUtils.isEmpty(ids)) {
- return AjaxResult.error("请传入文件id");
- }
- String[] split = ids.split(",");
- FrameDataExample example = new FrameDataExample();
- example.setOrderByClause("created_at");
- example.createCriteria().andIdIn(Arrays.asList(split));
- List<FrameData> frameDatas = frameDataService.selectByExample(example);
- ArrayList<Map<String, Object>> list_map = new ArrayList<>();
- Map<String, Object> map=null;
- for (FrameData frameData : frameDatas) {
- map = new HashMap<>();
- map.put("fileName", frameData.getFileName());
- map.put("createdAt", frameData.getCreatedAt());
- map.put("id", frameData.getId());
- list_map.add(map);
- }
- return AjaxResult.success(list_map);
- }
- @GetMapping({"/viewImg/{id}"})
- public void viewIMG(@PathVariable("id") String id, HttpServletRequest request, HttpServletResponse response) {
- FrameData frameData = this.frameDataService.selectByPrimaryKey(id);
- try {
- if (null != frameData) {
- String contentType = frameData.getFileContentType();
- if (StringUtils.isNotEmpty(contentType) && contentType.indexOf("image/") == -1) {
- FileUtils.downFile(response, frameData.getFileName(), frameData.getFilePath(), frameData.getFileContentType());
- } else {
- readIMGToHtml(request, response, frameData.getFilePath());
- }
- } else {
- readIMGToHtml(request, response, "error");
- }
- } catch (IOException var6) {
- var6.printStackTrace();
- }
- }
- public static void readIMGToHtml(HttpServletRequest request, HttpServletResponse response, String fileUrl) throws IOException {
- File desc = new File(fileUrl);
- InputStream inputStream = null;
- if (!desc.exists()) {
- inputStream = FileUtils.class.getResourceAsStream("/static/no_image.jpg");
- } else {
- inputStream = new FileInputStream(fileUrl);
- }
- readIMGToHtml(response, (InputStream)inputStream);
- }
- public static void readIMGToHtml(HttpServletResponse response, InputStream inputStream) throws IOException {
- int i = inputStream.available();
- byte[] buff = new byte[i];
- inputStream.read(buff);
- inputStream.close();
- response.reset();
- response.setContentType("image/jpg");
- OutputStream out = response.getOutputStream();
- out.write(buff);
- out.close();
- }
- }
|