Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.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
使用SpringBoot将文件发送到Angular2_Angular_Spring Boot - Fatal编程技术网

使用SpringBoot将文件发送到Angular2

使用SpringBoot将文件发送到Angular2,angular,spring-boot,Angular,Spring Boot,当我在SpringBoot中创建一个文件时(我可以在我的桌面上获取包含数据的文件),我试图将该文件发送到Angular2,但当我的文件到达时,Angular2中的值为空 SpringBoot: 在这里,我调用SpringBOOT下载Excell @RequestMapping(method = RequestMethod.GET, path = "/{download}", produces = MediaType.APPLICATION_JSON_VALUE) public Multi

当我在SpringBoot中创建一个文件时(我可以在我的桌面上获取包含数据的文件),我试图将该文件发送到Angular2,但当我的文件到达时,Angular2中的值为空

SpringBoot:

在这里,我调用SpringBOOT下载Excell

@RequestMapping(method = RequestMethod.GET, path = "/{download}", produces = MediaType.APPLICATION_JSON_VALUE)
    public MultipartFile download() {

        MultipartFile myFile = null;
        myFile = downloadExcell();

        return myFile;
    }
在这个函数中我创建了myExcell,我的Excel创建成功,我可以在我的桌面上获取文件

private MultipartFile downloadExcell() {
        MultipartFile myFile = null;
        String eyelash = "People";

        try {
            String filename = pathTempdownloadFile;

            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet(eyelash);

            String [] header= {"YEAR","MONTH","NAME","SURNAME","TLF"} ;
            HSSFRow rowhead = sheet.createRow((short) 0);


            for (int i = 0; i < header.length; i++) {
                cell = rowhead.createCell(cellnum);
                cell.setCellValue(header[i]);
                cellnum++;
            }

            FileOutputStream fileOut = new FileOutputStream(filename);
            workbook.write(fileOut);
            fileOut.close();
            workbook.close();

        } catch (Exception ex) {
            System.out.println(ex);
        }

        return myFile;
    }
错误->

java.lang.ClassCastException: java.io.FileOutputStream cannot be cast to org.springframework.web.multipart.MultipartFile
最后我在Angular2 myFile中得到(false)

角度2->

  downloadFile() {
    this.service.downloadFile().subscribe(data => {
      console.log("FILE", data);
    });
  }

 downloadFile() {
     const url = "http://localhost:8080/ml/download";
     return this.http.get(url);
  }
我需要把Excell和SpringBoot送到Angular2,但我不知道怎么做。
Ty

您不应该直接从spring boot发送文件。这会干扰服务器上的通信。我建议您将该文件上传到任何静态文件宿主,如S3。然后将文件的URL发送到angular。让angular从S3获取它。

您不应该直接从spring boot发送文件。这会干扰服务器上的通信。我建议您将该文件上传到任何静态文件宿主,如S3。然后将文件的URL发送到angular。让angular从S3获取它。

正如您已经了解到的,
MultipartFile
FileOutputStream
是不相关的:从实例化中可以看出,输出流被创建并用于写入文件,而不是写入servlet响应,而
MultipartFile
通常用作请求参数,但不能用于响应

根据您的需求,您可以将Excel文件直接写入web请求输出流。但是,我认为最好先将其存储在文件系统中,然后再从那里提供服务

为了访问响应,您可以将
HttpServletResponse
注入控制器方法(Spring会注意正确填充此参数):

publicsvoid下载(HttpServletResponse)

还要注意,我已将返回类型更改为
void
,因为您将只使用
response
参数

存储文件后,现在可以将其写入响应。记住在响应中设置适当的内容类型,以便请求的客户端(例如浏览器)知道如何处理

    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setHeader("Content-disposition", "attachment; filename=result.xlsx");
    try (final InputStream is = new FileInputStream(filename)) {
        IOUtils.copy(is, response.getOutputStream());
        response.flushBuffer();
    } catch (Exception ex) {
        // deal with error, e.g. response.setStatus(500) to signal an internal server error
    }
注意,我使用了ApacheCommons IO库中的
IOUtils.copy
。它只需将输入字节从
文件inputstream
复制到响应
OutputStream
,您也可以手动实现:

    byte[] buffer = new byte[4096];
    OutputStream os = response.getOutputStream();
    for (long count = 0L; -1 != (n = is.read(buffer)); count += (long)n) {
        os.write(buffer, 0, n);
    }

还要注意,我指定了另一个标题,
contentdisposition
。这为浏览器提供了关于如何处理接收到的文件的附加指导<代码>附件通常会导致一个保存对话框,而
内联
会生成文件的内联显示(例如PDF)。浏览器将使用指定的文件名存储文件。

如您所知,
MultipartFile
FileOutputStream
是不相关的:从实例化中可以看出,输出流是创建的,用于写入文件,而不是写入servlet响应,而
MultipartFile
通常用作请求参数,但不能用于响应

根据您的需求,您可以将Excel文件直接写入web请求输出流。但是,我认为最好先将其存储在文件系统中,然后再从那里提供服务

为了访问响应,您可以将
HttpServletResponse
注入控制器方法(Spring会注意正确填充此参数):

publicsvoid下载(HttpServletResponse)

还要注意,我已将返回类型更改为
void
,因为您将只使用
response
参数

存储文件后,现在可以将其写入响应。记住在响应中设置适当的内容类型,以便请求的客户端(例如浏览器)知道如何处理

    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setHeader("Content-disposition", "attachment; filename=result.xlsx");
    try (final InputStream is = new FileInputStream(filename)) {
        IOUtils.copy(is, response.getOutputStream());
        response.flushBuffer();
    } catch (Exception ex) {
        // deal with error, e.g. response.setStatus(500) to signal an internal server error
    }
注意,我使用了ApacheCommons IO库中的
IOUtils.copy
。它只需将输入字节从
文件inputstream
复制到响应
OutputStream
,您也可以手动实现:

    byte[] buffer = new byte[4096];
    OutputStream os = response.getOutputStream();
    for (long count = 0L; -1 != (n = is.read(buffer)); count += (long)n) {
        os.write(buffer, 0, n);
    }

还要注意,我指定了另一个标题,
contentdisposition
。这为浏览器提供了关于如何处理接收到的文件的附加指导<代码>附件通常会导致一个保存对话框,而
内联
会生成文件的内联显示(例如PDF)。浏览器将使用指定的文件名存储文件。

@EduBw如果我的回答对您有帮助,请您将其标记为正确,我将不胜感激。如果您需要更多的指导,请随意添加其他问题。最后,我不得不添加标题来回答,但是thanks@EduBw如果我的回答对您有帮助,请您将我的回答标记为正确,我将不胜感激。如果您需要更多的指导,请随意添加其他问题。最后,我不得不在回复中添加标题,但谢谢