Commit fa5f986d by 钱炳权

知识库完成,但下载文件存在问题

parent c91b88f6
package com.example.fuzzControll.controller.dataController;
import com.alibaba.fastjson.JSON;
import com.example.fuzzControll.domain.bo.FuzzLogTransEntity;
import com.example.fuzzControll.domain.po.KnowledgeBaseFiles;
import com.example.fuzzControll.domain.vo.AjaxResult;
import com.example.fuzzControll.exception.fileExcption.FileException;
import com.example.fuzzControll.exception.serverException.ServerException;
import com.example.fuzzControll.exception.testException.CmdException;
import com.example.fuzzControll.service.KnowledgeBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
/**
* 知识库控制器
*/
@RestController
@RequestMapping("/KnowledgeBase")
@Slf4j
public class KnowledgeBaseController {
@Autowired
KnowledgeBaseService knowledgeBaseService;
/**
* 知识库文件上传、默认文件不是复合文件
*/
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public AjaxResult upload(@RequestParam("file") MultipartFile file) {
try {
knowledgeBaseService.upload(file);
return AjaxResult.success("文件上传成功!");
} catch (ServerException e) {
log.error(e.getDefaultMessage());
return AjaxResult.error("文件上传失败!");
}
}
/**
* 知识库文件信息查看//todo有莫名其妙的错误
*/
@RequestMapping(value = "/getallmessage", method = RequestMethod.GET)
public AjaxResult getallmessage() {
try {
return AjaxResult.success(knowledgeBaseService.getAllMessage());
} catch (ServerException e) {
log.error(e.getDefaultMessage());
return AjaxResult.error("知识库信息获取失败!");
}
}
/**
* 跟据id获取知识库数据
*/
@RequestMapping(value = "/getById/{id}", method = RequestMethod.GET)
public KnowledgeBaseFiles getById(@PathVariable("id") int id) {
try {
return knowledgeBaseService.getKnowledgeBaseFilesById(id);
} catch (ServerException e) {
log.error(e.getDefaultMessage());
return null;
}
}
}
package com.example.fuzzControll.domain.po;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.util.Date;
/**
* 知识库类
*/
@Data
@Setter
@Getter
public class KnowledgeBaseFiles {
private int id;
@JsonIgnore
private byte[] fileBytes;
private Date createTime;
private String fileName;
public KnowledgeBaseFiles() {
}
public KnowledgeBaseFiles(int id, byte[] fileBytes, Date createTime, String fileName) {
this.id = id;
this.fileBytes = fileBytes;
this.createTime = createTime;
this.fileName = fileName;
}
public KnowledgeBaseFiles(byte[] fileBytes, Date createTime, String fileName) {
this.fileBytes = fileBytes;
this.createTime = createTime;
this.fileName = fileName;
}
public void setId(int id) {
this.id = id;
}
public void setFileBytes(byte[] fileBytes) {
this.fileBytes = fileBytes;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public int getId() {
return id;
}
public byte[] getFileBytes() {
return fileBytes;
}
public Date getCreateTime() {
return createTime;
}
public String getFileName() {
return fileName;
}
}
package com.example.fuzzControll.mapper;
import com.example.fuzzControll.domain.po.KnowledgeBaseFiles;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import java.util.List;
@Mapper
@Component("KnowledgeBaseMapper")
public interface KnowledgeBaseMapper {
KnowledgeBaseFiles getKnowledgeBaseFilesById(int id);
void insert(KnowledgeBaseFiles knowledgeBaseFiles);
List<KnowledgeBaseFiles> getAllMessage();
}
package com.example.fuzzControll.service;
import com.example.fuzzControll.domain.po.KnowledgeBaseFiles;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
public interface KnowledgeBaseService {
void upload(MultipartFile file);
List<KnowledgeBaseFiles> getAllMessage();
KnowledgeBaseFiles getKnowledgeBaseFilesById(int id);
}
package com.example.fuzzControll.service.impl;
import com.example.fuzzControll.domain.po.KnowledgeBaseFiles;
import com.example.fuzzControll.mapper.KnowledgeBaseMapper;
import com.example.fuzzControll.service.KnowledgeBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.Date;
import java.util.List;
@Service("KnowledgeBaseService")
@Slf4j
public class KnowledgeBaseServiceImpl implements KnowledgeBaseService {
@Autowired
KnowledgeBaseMapper knowledgeBaseMapper;
@Override
public void upload(MultipartFile file) {
try {
KnowledgeBaseFiles knowledgeBaseFiles = new KnowledgeBaseFiles(file.getBytes(), new Date(), file.getOriginalFilename());
knowledgeBaseMapper.insert(knowledgeBaseFiles);
} catch (IOException e) {
log.error("Creat and insert KnowledgeBaseFiles error:[{}] ", e.getMessage());
throw new RuntimeException(e);
}
}
@Override
public List<KnowledgeBaseFiles> getAllMessage() {
try {
return knowledgeBaseMapper.getAllMessage();
} catch (Exception e) {
log.error("GetAllMessage error:[{}] ", e.getMessage());
throw new RuntimeException(e);
}
}
@Override
public KnowledgeBaseFiles getKnowledgeBaseFilesById(int id) {
KnowledgeBaseFiles knowledgeBaseFiles = null;
try {
knowledgeBaseFiles = knowledgeBaseMapper.getKnowledgeBaseFilesById(id);
} catch (Exception e) {
log.error("GetKnowledgeBaseFilesById error:[{}] ", e.getMessage());
throw new RuntimeException(e);
}
return knowledgeBaseFiles;
}
}
......@@ -91,7 +91,12 @@ public class TestMissionServiceImpl implements TestMissionService {
@Override
public void insert(TestAndParams testAndParams) {
Test test = new Test(new Date(), new Date(), testAndParams.getTestName());
testMapper.insert(test);
try {
testMapper.insert(test);
} catch (Exception e) {
log.error("Insert error:{}", e.getMessage());
throw new RuntimeException(e);
}
}
@Transactional(rollbackFor = MysqlException.class)
......
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.fuzzControll.mapper.KnowledgeBaseMapper">
<resultMap type="com.example.fuzzControll.domain.po.KnowledgeBaseFiles" id="KnowledgeBaseFiles">
<result property="id" column="id" />
<result property="fileBytes" column="fileBytes" />
<result property="fileName" column="fileName" />
<result property="createTime" column="createTime" />
</resultMap>
<sql id="getAll">
select id,fileBytes,fileName,createTime from KnowledgeBase
</sql>
<insert id="insert" parameterType="com.example.fuzzControll.domain.po.KnowledgeBaseFiles">
insert into KnowledgeBase(fileBytes,fileName,createTime) values(#{fileBytes},#{fileName},#{createTime})
</insert>
<select id="getAllMessage" resultMap="KnowledgeBaseFiles">
<include refid="getAll"/>
</select>
<select id="getKnowledgeBaseFilesById" resultType="com.example.fuzzControll.domain.po.KnowledgeBaseFiles">
<include refid="getAll"/>
where id = #{id}
</select>
</mapper>
\ No newline at end of file
package com.example.fuzzbackendmaster.controller;
import com.alibaba.fastjson.JSON;
import com.example.fuzzbackendmaster.pojo.vo.AjaxResult;
import com.example.fuzzbackendmaster.pojo.vo.KnowledgeBaseFiles;
import com.example.fuzzbackendmaster.service.FileDownloadService;
import com.example.fuzzbackendmaster.service.FuzzIntegrationFileApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 知识库控制器
*/
@RestController
@RequestMapping("/KnowledgeBase")
@Slf4j
public class KnowledgeBaseController {
@Autowired
FileDownloadService fileDownloadService;
@Autowired
FuzzIntegrationFileApi fuzzIntegrationFileApi;
/**
* 知识库文件上传、默认文件不是复合文件
*/
@RequestMapping(value = "/upload", method = RequestMethod.GET)
public AjaxResult upload(@RequestPart("file") MultipartFile file) {
return fuzzIntegrationFileApi.KnowledgeBaseUpload(file);
}
/**
* 知识库文件信息查看
*/
@RequestMapping(value = "/getallmessage", method = RequestMethod.GET)
public AjaxResult getallmessage() {
return fuzzIntegrationFileApi.getallmessage();
}
/**
* 知识库文件下载
*/
//todo增加批量处理功能
@RequestMapping(value = "/download/{id}", method = RequestMethod.GET)
public void download(@PathVariable("id") int id,HttpServletResponse response) throws IOException {
try {
fileDownloadService.getFile(id,response);
} catch (Exception e) {
e.printStackTrace();
response.sendError(500, "Download failed!");
}
}
//todo增加批量处理功能
}
package com.example.fuzzbackendmaster.pojo.vo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.util.Date;
/**
* 知识库类
*/
@Data
@Setter
@Getter
public class KnowledgeBaseFiles {
private int id;
@JsonIgnore
private byte[] fileBytes;
private Date createTime;
private String fileName;
public KnowledgeBaseFiles() {
}
public KnowledgeBaseFiles(int id, byte[] fileBytes, Date createTime, String fileName) {
this.id = id;
this.fileBytes = fileBytes;
this.createTime = createTime;
this.fileName = fileName;
}
public KnowledgeBaseFiles(byte[] fileBytes, Date createTime, String fileName) {
this.fileBytes = fileBytes;
this.createTime = createTime;
this.fileName = fileName;
}
public void setId(int id) {
this.id = id;
}
public void setFileBytes(byte[] fileBytes) {
this.fileBytes = fileBytes;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public int getId() {
return id;
}
public byte[] getFileBytes() {
return fileBytes;
}
public Date getCreateTime() {
return createTime;
}
public String getFileName() {
return fileName;
}
}
package com.example.fuzzbackendmaster.service;
import javax.servlet.http.HttpServletResponse;
public interface FileDownloadService {
void getFile(int id, HttpServletResponse response);
}
......@@ -193,4 +193,19 @@ public interface FuzzIntegrationFileApi {
*/
@RequestMapping(value = "/mission/delMissionInfo", method = RequestMethod.GET)
AjaxResult delMissionInfoById(@RequestParam int id);
/**
*
* @param file
* @return
*/
@RequestMapping(value = "/KnowledgeBase/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
AjaxResult KnowledgeBaseUpload(@RequestPart("file") MultipartFile file);
@RequestMapping(value = "/KnowledgeBase/getallmessage", method = RequestMethod.GET)
AjaxResult getallmessage();
/**
* 获取文件二进制文件
*/
@RequestMapping(value = "/KnowledgeBase/getById/{id}", method = RequestMethod.GET)
KnowledgeBaseFiles getKnowledgeBaseFilesBytes(@PathVariable("id") int id);
}
package com.example.fuzzbackendmaster.service.impl;
import com.example.fuzzbackendmaster.pojo.vo.FuzzLogTransEntity;
import com.example.fuzzbackendmaster.pojo.vo.KnowledgeBaseFiles;
import com.example.fuzzbackendmaster.service.FileDownloadService;
import com.example.fuzzbackendmaster.service.FuzzIntegrationFileApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;
@Service
@Slf4j
public class FileDownloadServiceImpl implements FileDownloadService {
@Autowired
FuzzIntegrationFileApi fuzzIntegrationFileApi;
@Override
public void getFile(int id, HttpServletResponse response) {
KnowledgeBaseFiles knowledgeBaseFiles = fuzzIntegrationFileApi.getKnowledgeBaseFilesBytes(id);
if (knowledgeBaseFiles == null || knowledgeBaseFiles.getFileBytes() == null) {
log.error("File is null");
throw new IllegalStateException();
}
downloadFile(knowledgeBaseFiles.getFileName(), knowledgeBaseFiles.getFileBytes(), response);
}
/**
* 浏览器前端下载后台文件,文件为二进制格式
*/
public void downloadFile(String fileName, byte[] file, HttpServletResponse response) {
if (fileName != null && !"".equals(fileName)) {
if (file != null && file.length != 0) {
response.setContentType("application/force-download");// 设置强制下载不打开
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
try (OutputStream os = response.getOutputStream();) {
os.write(file, 0, file.length);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment