gpt4 book ai didi

java - 如何使用 spring boot 和 jpa 在数据库中上传和保存文件?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:31:40 24 4
gpt4 key购买 nike

我是 spring boot 的新手。我想使用 spring boot 上传一个小文件并使用 jpa 将其保存在 db 中。但是我没有很好的分辨率。我的程序是这样的:
数据库表:

CREATE TABLE `report` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`logo` BLOB NOT NULL,
`created_time` int(10) NOT NULL,
`updated_time` int(10) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8

jpa bean :
报表.java

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.Table;
import java.io.Serializable;

@Entity
@Table(name="mf_report")
public class Report implements Serializable{
@Column(name="id")
private int id;

@Column(name="name")
private String name;

@Lob
@Column(name="logo", length=100000)
private byte[] logo;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public byte[] getLogo() {
return logo;
}

public void setLogo(byte[] logo) {
this.logo = logo;
}
}

ReportReposity.java:

@Repository
public interface ReportRepository extends CrudRepository<Report,Long>{
}

报表服务.java:

@Service
public class ReportService extends CrudService<Report, ReportRepository> {

private final static Logger logger = LoggerFactory.getLogger(ReportService.class);

@Override
@Autowired
public void setRepo(ReportRepository repo) {
this.repo = repo;
}

@Override
public Report copy(Report from, Report to) {
to = from;
return to;
}

@Autowired
private ReportRepository reportRepository;

public boolean saveReportByRequestBean(ReportAddQueryRequest reportBeanQueryRequest){
try {
Report report = new Report();
report.setName(reportBeanQueryRequest.getName());
report.setLogo(reportBeanQueryRequest.getLogo());
long now = System.currentTimeMillis()/1000;
report.setCreateTime(now);
report.setUpdateTime(now);
this.save(report);
}catch (Exception e){
logger.error("save report error:", e);
return false;
}
return true;
}
}

ReportParamBean.java:

import org.hibernate.validator.constraints.NotEmpty;
import java.io.Serializable;

public class ReportParamBean extends AbsRequest implements Serializable {
private long reportId;
@NotEmpty(message = "Param 'name' can't be NULL")
private String name;
private String logo;// In fact, I don't know what type should logo be, File or ?
}

AbsRequest.java:

public class AbsRequest implements Serializable {
private static final long serialVersionUID = -8928786145900600868L;
@NotEmpty(message = "Param 'token' can't be NULL")
@NotNull
private String token;
@NotEmpty(message = "Param 'sign' can't be NULL")
private String sign;
@Min(value = 1, message = "Param 'time' is invalid")
private Long time;
@Min(value = -1, message = "Param 'nodeId' is invalid")
@NotNull(message = "Param 'nodeId' can't be NULL")
private Long nodeId;
private String nodeName;
@Override
public String toString() {
return new ToStringBuilder(this)
.append("token", token)
.append("sign", sign)
.append("time", time)
.append("nodeId", nodeId)
.append("nodeName", nodeName)
.toString();
}

public String getToken() {
return token;
}

public void setToken(String token) {
this.token = token;
}

public String getSign() {
return sign;
}

public void setSign(String sign) {
this.sign = sign;
}

public Long getTime() {
return time;
}

public void setTime(Long time) {
this.time = time;
}

public Long getNodeId() {
return nodeId;
}

public void setNodeId(Long nodeId) {
this.nodeId = nodeId;
}

public String getNodeName() {
return nodeName;
}

public void setNodeName(String nodeName) {
this.nodeName = nodeName;
}
}

ReportController.java:

@RestController
@RequestMapping("/api")
public class ReportController {

@Autowired
private ReportService reportService;

@RequestMapping(value = "/report", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
public JSONObject createReport(@RequestBody ReportAddQueryRequest reportBeanQueryRequest){
boolean result = reportService.saveReportByRequestBean(reportBeanQueryRequest);
if (!result){
return ResponseWrapper.buildResponse(RTCodeEnum.C_SERVICE_NOT_AVAILABLE, "add report failed");
}
return ResponseWrapper.buildResponse(RTCodeEnum.C_OK, "add report success");
}
}

我不知道我是否可以在一个post请求中将文件和其他参数发布到服务器,然后将数据保存在db中。你能给我解决方案吗?特别感谢。

最佳答案

使用 Spring 的 multipart file .在简单的实现中,您可以从中获取 InputStream,将文件内容(保存在硬盘上)读取到字节数组并将其保存到数据库。

关于java - 如何使用 spring boot 和 jpa 在数据库中上传和保存文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39416365/

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