gpt4 book ai didi

java - spring mvc生成的pdf文件生成后如何直接保存到数据库中?

转载 作者:行者123 更新时间:2023-11-30 02:42:22 26 4
gpt4 key购买 nike

这是我生成 pdf 的类(class)。当我提交表单时,它会生成一个 pdf 文件,但我不知道如何将其保存到数据库。

public class BookingPdf {

private static String FILE = "D:/Hotels/pdf/";
protected static Font font10;
protected static Font font10b;
protected static Font font12;
protected static Font font12b;
protected static Font font14;

public static String generateBookingPDF(Booking booking) throws DocumentException, FileNotFoundException, IOException {
Document document = new Document(PageSize.A4);
String bookingname = booking.getFirstName() + " " + booking.getLastName();
document.addHeader("HOTEL SWING", "Hotel Swing Booking confirmation");
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FILE + "Booking" + " " + booking.getCustomer().getFirstName() + ".pdf "));
writer.setViewerPreferences(PdfWriter.PageModeUseOC);
writer.setPdfVersion(PdfWriter.VERSION_1_7);
document.open();
String html = htmlTemplate(booking);

List unorderedList = new List(List.UNORDERED);
unorderedList.add(new ListItem("Name :" + booking.getFirstName() + " " + booking.getLastName()));
unorderedList.add(new ListItem("Room :" + booking.getRoom().getRoomNumber()));
unorderedList.add(new ListItem("Total Price :" + booking.getTotalPrice()));
unorderedList.add(new ListItem("check in :" + booking.getRoom().getRoomNumber()));
unorderedList.add(new ListItem("Booking:" + booking.getbId()));

document.add(unorderedList);
document.close();
return bookingname;

}

private static String htmlTemplate(Booking booking) {
StringBuilder html = new StringBuilder();

html.append("" + "<h1> Booking Confirmation</h1> ");
return html.toString();

}

public BookingPdf(Booking booking) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

public BookingPdf() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}

这是我的 Controller 类,我想从中保存 pdf 位置并将其保存到数据库

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@ModelAttribute("Booking") Booking booking, BindingResult result, HttpServletRequest request) throws SQLException, ParseException, DocumentException, IOException {
Customer customer = customerService.getById(booking.getCustomer().getC_id());
Room room = roomService.getById(booking.getRoom().getRo_id());
long totalNights = (booking.getCheckoutDate().getTime() - booking.getCheckinDate().getTime()) / (24 * 60 * 60 * 1000);
System.out.println(totalNights);
booking.setTotalNights((int) totalNights);
int totalPrice = (booking.getTotalNights() * booking.getRoom().getRoomPrice());
booking.setTotalPrice(totalPrice);
System.out.println(totalPrice);
System.out.println("check" + booking);

try {
booking.setCustomer(customer);
booking.setRoom(room);
if (booking.getbId() == 0) {
bookingService.insert(booking);
BookingPdf.generateBookingPDF(booking);

} else
{
bookingService.update(booking);
}
} catch (SQLException ex) {
}
return "redirect:";

最佳答案

您可以使用 BLOB 数据类型,将文件转换为 byte[] 数组。 https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/IOUtils.html

关于java - spring mvc生成的pdf文件生成后如何直接保存到数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41266471/

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