【发布时间】:2018-12-27 06:56:00
【问题描述】:
当我在 SpringBoot 中创建一个文件(我可以在我的桌面上获取带有数据的文件)时,我尝试将此文件发送到 Angular2,但是当我的文件到达时,我在 Angular2 中得到 NULL。
SpringBoot:
这里,我打电话给SpringBOOT下载Excel。
@RequestMapping(method = RequestMethod.GET, path = "/{download}", produces = MediaType.APPLICATION_JSON_VALUE)
public MultipartFile download() {
MultipartFile myFile = null;
myFile = downloadExcell();
return myFile;
}
在这个函数中我创建了myExcell,我的excell创建成功,我可以在我的桌面获取文件。
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;
}
现在我可以将 fileOut 分配给 myFile
myFile = (MultipartFile) fileOut;
错误 ->
java.lang.ClassCastException: java.io.FileOutputStream cannot be cast to org.springframework.web.multipart.MultipartFile
最后我在 Angular2 myFile 中得到 (false)。
Angular2->
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,但我不知道该怎么做。 类型
【问题讨论】:
标签: angular spring-boot