英文:
Not able to see actual uploaded file(.pdf format) format on database and postman?
问题
i am trying to do create a user entity along with a data/file (pdf format).Uploaded and save to database is fine but when i get the user into postman try to send get request method then in the data field show some terrible data and also i can not see my pdf file into my database.
#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>
#Model Class
@Entity(name = "employee")
public class Employee {
@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;
}
#Controller Class
@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);
}
#ServiceImplementation Class
@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);
employeeRepository.save(employee);
}
#Postman view to get employee.
post method set with "form-data" in header and select key as a file and next key is my user entity key as a text. I also try with content type (applicaiton/json) but not working. How can i convert to this unexpected data to a pdf file or something stranded format to see.
{
"id": 62,
"name": "raj",
"university": "ewu",
"department": "bba",
"year_of_experience": 3,
"data": "JVBERi0xLjcNCiW1tbW1DQoxIDAgb2JqDQo8PC9U..."
}
英文:
i am trying to do create a user entity along with a data/file (pdf format).Uploaded and save to database is fine but when i get the user into postman try to send get request method then in the data field show some terrible data and also i can not see my pdf file into my database.
#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>
#Model Class
@Entity(name = "employee")
public class Employee {
@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;
-
Contoller Class
@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);
}
#ServiceImplemention Class@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 to get employee.
post method set with "form-data" in header and select key as a file and next key is my user entiy key as a text.i also try with content type (applicaiton/json) but not working.How can i convert to this unexpected data to a pdf file or something stranded format to see.
"id": 62,
"name": "raj",
"university": "ewu",
"department": "bba",
"year_of_experience": 3,
"data": "JVBERi0xLjcNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFuZyhlbi1VUykgL1N0cnVjdFRyZWVSb290IDkgMCBSL01hcmtJbmZvPDwvTWFya2VkIHRydWU+Pi9NZXRhZGF0YSAyNSAwIFIvVmlld2VyUHJlZmVyZW5jZXMgMjYgMCBSPj4NCmVuZG9iag0KMiAwIG9iag0KPDwvVHlwZS9QYWdlcy9Db3VudCAxL0tpZHNbIDMgMCBSXSA+Pg0KZW5kb2JqDQozIDAgb2JqDQo8PC9UeXBlL1BhZ2UvUGFyZW50IDIgMCBSL1Jlc291cmNlczw8L0ZvbnQ8PC9GMSA1IDAgUj4+L0V4dEdTdGF0ZTw8L0dTNyA3IDAgUi9HUzggOCAwIFI+Pi9Qcm9jU2V0Wy9QREYvVGV4dC9JbWFnZUIvSW1hZ2VDL0ltYWdlSV0gPj4vTWVkaWFCb3hbIDAgMCA2MTIgNzkyXSAvQ29udGVudHMgNCAwIFIvR3JvdXA8PC9UeXBlL0dyb3VwL1MvVHJhbnNwYXJlbmN5L0NTL0RldmljZVJHQj4+L1RhYnMvUy9TdHJ1Y3RQYXJlbnRzI
答案1
得分: 0
你应该看看社区项目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
@EnableJpaStores
// enable REST API
@Import("org.springframework.content.rest.config.RestConfiguration.class")
public class ContentConfig {
}
注意:在使用Spring Boot启动器时,技术上不需要这个配置,但为了清晰起见,包括了它。
要关联内容,将Spring Content注解添加到你的账户实体。
Employee.java
@Entity
public class Employee {
// 用以下注解替换 @Lob 字段:
@ContentId
private String contentId;
@ContentLength
private long contentLength = 0L;
@MimeType
private String mimeType = "application/pdf";
}
创建一个"store":
EmployeeContentStore.java
@StoreRestResource
public interface EmployeeContentStore extends ContentStore<Employee, String> {
}
这就是你需要创建处理员工内容的REST端点的全部内容,位于URI /employees
下。当你的应用程序启动时,Spring Content会查看你的依赖项(查看Spring Content JPA和REST),查看你的EmployeeContentStore
接口,并注入该接口的JPA实现。它还会注入一个@Controller
,将http请求转发到该实现。这样你就无需自己实现任何东西了。所以,编程模型和操作方式与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 "Accept: application/pdf"
将再次获取它,依此类推...支持完整的CRUD。
由于你将内容与员工实体关联,所以当你从Spring Data端点获取JSON响应时,它们将提供与内容相关的元数据,但不包括你当前方法中看到的"terrible data"。
希望有所帮助。
英文:
You should take a look at the community project Spring Content. This project gives you a Spring Data-like approach to content. It is for unstructured data (documents, images, videos, etc), what Spring Data is for structured data.
You could add it to your project with something like this:-
> 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
To associate content, add Spring Content annotations to your account entity.
> 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";
Create a "store":
> EmployeeContentStore.java
@StoreRestResource
public interface EmployeeContentStore extends ContentStore<Employee, String> {
}
This is all you need to create REST endpoints for handling your employee's content under the URI /employees
. When your application starts, Spring Content will look at your dependencies (seeing Spring Content JPA and REST), look at your UserContentStore
interface and inject an implementation of that interface for JPA. It will also inject an @Controller
that forwards http requests to that implementation. This saves you having to implement any of this yourself. So, very similar in programming model and operation to Spring Data.
Then...
curl -X POST /employees/{employeeId} -H "Content-Type: application/pdf" -F "file=@/path/to/local/file.pdf"
will store the content of /path/to/local/file.pdf
in the database and associate it with the employee entity whose id is employeeId
.
curl /employees/{employeeId} -H "Accept: application/pdf"
will fetch it again and so on...supports full CRUD.
Since you are associating the content with your employee entity, when you get your json responses back from the Spring Data endpoints, they will provide the content-related metadata, but NOT the "terrible data" that you are seeing with your current approach.
There are a couple of getting started guides and videos here. The reference guide is here.
HTH
专注分享java语言的经验与见解,让所有开发者获益!
评论