gpt4 book ai didi

java - 在数据库和 postman 上无法看到实际上传的文件(.pdf 格式)格式?

转载 作者:行者123 更新时间:2023-12-01 17:33:34 30 4
gpt4 key购买 nike

我正在尝试创建一个用户实体以及数据/文件(pdf格式)。上传并保存到数据库很好,但是当我让用户进入 postman 时尝试发送获取请求方法,然后在数据字段中显示一些糟糕的数据,而且我无法在数据库中看到我的 pdf 文件。

pom.xml

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

模型类

@Entity(name = "employee")

公共(public)类员工{

@Id
@GeneratedValue
private Integer id;

@Column(nullable = false, length = 30)
private String name;

@Column(nullable = false, length = 30)
private String university;

@Column(nullable = false, length = 30)
private String department;

@Column(nullable = false, length = 2)
private Integer year_of_experience;

@Lob
private byte[] data;

2. Controller 类

@GetMapping
public List<Employee> getAllEmployee(){
return employeeService.getAllEmployee();
}

@GetMapping("{id}")
public Employee getEmployeeById(@PathVariable Integer id){
return employeeService.getEmployeeById(id);
}

@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public void addEmployee(@RequestParam("file") MultipartFile file, @RequestParam("emp") String emp ) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
Employee employee = objectMapper.readValue(emp,Employee.class);
employeeService.addEmployee(employee,file);
}

服务实现类

  @Override
public void addEmployee(Employee employee, MultipartFile file) throws IOException {

for (int i = 0; i < employee.getExperienceList().size(); i++) {
Experience experience = employee.getExperienceList().get(i);
experienceService.addExperience(experience);
}
byte[] temp = file.getBytes();
InputStream inputStream = new ByteArrayInputStream(temp);
employee.setData(temp);

employeeRepostitory.save(employee);
}

postman View 获取员工。

在标题中设置“form-data”的帖子方法,并选择 key 作为文件,下一个 key 是我的用户实体 key 作为文本。我也尝试使用内容类型(applicaiton/json)但不起作用。我该如何将这些意外数据转换为 pdf 文件或其他搁浅格式以供查看。

"id": 62,
"name": "raj",
"university": "ewu",
"department": "bba",
"year_of_experience": 3,
"data": "JVBERi0xLjcNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFuZyhlbi1VUykgL1N0cnVjdFRyZWVSb290IDkgMCBSL01hcmtJbmZvPDwvTWFya2VkIHRydWU+Pi9NZXRhZGF0YSAyNSAwIFIvVmlld2VyUHJlZmVyZW5jZXMgMjYgMCBSPj4NCmVuZG9iag0KMiAwIG9iag0KPDwvVHlwZS9QYWdlcy9Db3VudCAxL0tpZHNbIDMgMCBSXSA+Pg0KZW5kb2JqDQozIDAgb2JqDQo8PC9UeXBlL1BhZ2UvUGFyZW50IDIgMCBSL1Jlc291cmNlczw8L0ZvbnQ8PC9GMSA1IDAgUj4+L0V4dEdTdGF0ZTw8L0dTNyA3IDAgUi9HUzggOCAwIFI+Pi9Qcm9jU2V0Wy9QREYvVGV4dC9JbWFnZUIvSW1hZ2VDL0ltYWdlSV0gPj4vTWVkaWFCb3hbIDAgMCA2MTIgNzkyXSAvQ29udGVudHMgNCAwIFIvR3JvdXA8PC9UeXBlL0dyb3VwL1MvVHJhbnNwYXJlbmN5L0NTL0RldmljZVJHQj4+L1RhYnMvUy9TdHJ1Y3RQYXJlbnRzI

最佳答案

您应该看看社区项目 Spring Content 。该项目为您提供了一种类似于 Spring Data 的内容方法。它适用于非结构化数据(文档、图像、视频等),就像 Spring Data 适用于结构化数据一样。

您可以将其添加到您的项目中,如下所示:-

pom.xml

   <!-- Java API -->
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-jpa-boot-starter</artifactId>
<version>1.0.0.M9</version>
</dependency>
<!-- REST API -->
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-rest-boot-starter</artifactId>
<version>1.0.0.M9</version>
</dependency>

Configuration

@Configuration
@EnableJpaStores
// enable REST API
@Import("org.springframework.content.rest.config.RestConfiguration.class")
public class ContentConfig {
}

NB: technically this configuration is not needed when using the Spring Boot starters, but included for clarity

要关联内容,请将 Spring Content 注释添加到您的帐户实体。

Enployee.java

@Entity
public class Employee {

// replace @Lob field with:

@ContentId
private String contentId;

@ContentLength
private long contentLength = 0L;

@MimeType
private String mimeType = "application/pdf";

创建“商店”:

EmployeeContentStore.java

@StoreRestResource
public interface EmployeeContentStore extends ContentStore<Employee, String> {
}

这就是创建 REST 端点以处理 URI /employees 下的员工内容所需的全部内容。当您的应用程序启动时,Spring Content 将查看您的依赖项(请参阅 Spring Content JPA 和 REST),查看您的 UserContentStore 接口(interface)并为 JPA 注入(inject)该接口(interface)的实现。它还将注入(inject)一个将 http 请求转发到该实现的 @Controller。这使您不必自己实现任何这些。因此,在编程模型和操作上与 Spring Data 非常相似。

然后...

curl -X POST/employees/{employeeId} -H "Content-Type: application/pdf"-F "file=@/path/to/local/file.pdf"

会将/path/to/local/file.pdf的内容存储到数据库中,并将其与id为employeeId的员工实体关联起来。

curl/employees/{employeeId} -H“接受:application/pdf”

将再次获取它等等...支持完整的 CRUD。

由于您将内容与您的员工实体相关联,因此当您从 Spring Data 端点返回 json 响应时,它们将提供与内容相关的元数据,而不是您当前看到的“糟糕数据”方法。

有一些入门指南和视频 here 。引用指南是here

HTH

关于java - 在数据库和 postman 上无法看到实际上传的文件(.pdf 格式)格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61079999/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com