Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Ajax将json发送到spring引导服务器,并在新选项卡中打开生成的pdf_Javascript_Java_Ajax_Spring Boot_Pdfbox - Fatal编程技术网

Javascript Ajax将json发送到spring引导服务器,并在新选项卡中打开生成的pdf

Javascript Ajax将json发送到spring引导服务器,并在新选项卡中打开生成的pdf,javascript,java,ajax,spring-boot,pdfbox,Javascript,Java,Ajax,Spring Boot,Pdfbox,我有一个小型web应用程序,它从表单收集数据,通过Ajax将数据发送到spring boot服务器。服务器生成pdf文件并将其作为字节数组发送回。然后我想在新标签页中打开一个PDF,但我在那里看到空的PDF,这就是我的问题。 这是Ajax方法: function sendJson(json) { $.ajax({ url: "/pdf/create", type: "POST", data: json, contentTyp

我有一个小型web应用程序,它从表单收集数据,通过Ajax将数据发送到spring boot服务器。服务器生成pdf文件并将其作为字节数组发送回。然后我想在新标签页中打开一个PDF,但我在那里看到空的PDF,这就是我的问题。 这是Ajax方法:

function sendJson(json) {
$.ajax({
    url: "/pdf/create",
    type: "POST",
    data: json,
    contentType: "application/json",
    success: function (data) {
        alert('Success');

        openFile(data)
    },
    error: function (e) {
        console.log('Was trying to send data, but error occurred. ' + json);
        alert('Error sending data to server');
    }
});
}
然后我根据接收到的数据创建一个blob并打开它

function openFile(byte) {
    var file = new Blob([byte], {type: "application/pdf"});

    var fileURL = URL.createObjectURL(file);
    window.open(fileURL);
}
这是我的spring引导控制器:

@PostMapping(value = "/pdf/create")
public ResponseEntity<?> preparePdf(@RequestBody String data) throws IOException {
    System.out.println("Data input: " + data);

    ObjectMapper mapper = new ObjectMapper();
    Map<String, Object> dataObject = mapper.readValue(data, Map.class);

    try {
        return pdfService.createPdfFromTemplate(dataObject);
    } catch (Exception e) {
        e.printStackTrace();
        return ResponseEntity.badRequest().body(HttpStatus.BAD_REQUEST);
    }
}
@PostMapping(value=“/pdf/create”)
公共响应属性preparePdf(@RequestBody String data)引发IOException{
System.out.println(“数据输入:“+数据”);
ObjectMapper mapper=新的ObjectMapper();
Map dataObject=mapper.readValue(数据,Map.class);
试一试{
返回pdfService.createPdfFromTemplate(dataObject);
}捕获(例外e){
e、 printStackTrace();
返回ResponseEntity.badRequest().body(HttpStatus.BAD_请求);
}
}
处理过程如下:

public ResponseEntity<Resource> createPdfFromTemplate(Map<String, Object> dataObject) throws IOException {

// data saved to DB. PDF is generated and saved locally

    String filename = "generated_file.pdf";

    File pdf = new File(filename);
    byte[] fileBytes = FileUtils.readFileToByteArray(pdf);

    ResponseEntity.BodyBuilder res = ResponseEntity.ok()
            .contentType(MediaType.APPLICATION_OCTET_STREAM);

            return res.body(new ByteArrayResource(fileBytes));
}
public ResponseEntity createPdfFromTemplate(映射数据对象)引发IOException{
//保存到DB.PDF的数据在本地生成和保存
String filename=“generated_file.pdf”;
文件pdf=新文件(文件名);
byte[]fileBytes=FileUtils.readFileToByteArray(pdf);
ResponseEntity.BodyBuilder res=ResponseEntity.ok()
.contentType(MediaType.APPLICATION\u八位字节\u流);
返回res.body(新的ByteArrayResource(fileBytes));
}
生成的pdf中的文本是西里尔文,但我认为问题不在于此,也不在于编码。
我想我错过了一些重要的事情。有时我看到一些意见,认为不能通过ajax或POST方法完成,这不是一个好方法,但发送json数据并接收生成的文件似乎是合乎逻辑的。任何想法都很受欢迎。

希望对某人有用。我在服务器端使用
base64.getEncoder().encodeToString(fileBytes)
作为响应的主体,将字节数组编码为base64字符串。在JS中打开文件之前,我将其转换为ArrayBuffer,如下所示:

function base64ToArrayBuffer(base64) {
    var binaryString = window.atob(base64);
    var binaryLen = binaryString.length;
    var bytes = new Uint8Array(binaryLen);
    for (var i = 0; i < binaryLen; i++) {
        bytes[i] = binaryString.charCodeAt(i);
    }
    return bytes;
}
函数base64ToArrayBuffer(base64){
var binaryString=window.atob(base64);
var binaryLen=binaryString.length;
var bytes=新的Uint8Array(二进制数);
对于(var i=0;i
和我预期的一样,文件在一个新选项卡中打开