英文:
Error With BufferedOutputStream java :The constructor BufferedOutputStream(FileOutputStream) is undefined
问题
你好,大家好,请帮我解决一些错误。我尝试在 Spring Boot 和 React.js 中导入 Excel 文件,但在后端我遇到了错误。
错误信息是:
构造函数 BufferedOutputStream(FileOutputStream) 未定义。
出错的代码段如下:
@RequestMapping(value="/upload", method=RequestMethod.POST)
public @ResponseBody ResponseEntity<String> handleFileUpload(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file) throws Exception {
if (name.contains("/")) {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("不允许使用文件夹分隔符。");
} else if (name.contains("/")) {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("不允许使用相对路径。");
} else if (!name.endsWith(".jar")) {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("不允许的文件类型。必须是以 '.jar' 结尾的 Jar 文件类型。");
}
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name)));
stream.write(bytes);
stream.close();
return ResponseEntity.ok("文件 " + name + " 已上传。");
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(e.getMessage());
}
} else {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("上传失败,因为文件为空:" + name);
}
}
问题出现在以下代码行:
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name)));
英文:
hello everyone please i get some erorr so i try to import excel file with spring boot et React js
but in my backend i got error
The constructor BufferedOutputStream(FileOutputStream) is undefined
the method is :
@RequestMapping(value="/upload", method=RequestMethod.POST)
public @ResponseBody ResponseEntity<String> handleFileUpload(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file) throws Exception{
if (name.contains("/")) {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("Folder separators not allowed.");
} else if (name.contains("/")) {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("Relative pathnames not allowed.");
} else if (!name.endsWith(".jar")) {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("File type not allowed. Must be a Jar file type ending in '.jar'.");
}
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name)));
stream.write(bytes);
stream.close();
return ResponseEntity.ok("File " + name + " uploaded.");
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(e.getMessage());
}
} else {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("You failed to upload " + name + " because the file was empty.");
}
}
}
i get error in this line :
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name)));
答案1
得分: 0
唯一我能想到的原因是,你导入了一个与下面代码不同的类:
import java.io.BufferedOutputStream;
英文:
The only reason I can think of is, you have imported a different class than following
import java.io.BufferedOutputStream;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论