Error With BufferedOutputStream java :The constructor BufferedOutputStream(FileOutputStream) is undefined

huangapple go评论125阅读模式
英文:

Error With BufferedOutputStream java :The constructor BufferedOutputStream(FileOutputStream) is undefined

问题

你好,大家好,请帮我解决一些错误。我尝试在 Spring Boot 和 React.js 中导入 Excel 文件,但在后端我遇到了错误。

错误信息是:

构造函数 BufferedOutputStream(FileOutputStream) 未定义。

出错的代码段如下:

  1. @RequestMapping(value="/upload", method=RequestMethod.POST)
  2. public @ResponseBody ResponseEntity<String> handleFileUpload(@RequestParam("name") String name,
  3. @RequestParam("file") MultipartFile file) throws Exception {
  4. if (name.contains("/")) {
  5. return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("不允许使用文件夹分隔符。");
  6. } else if (name.contains("/")) {
  7. return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("不允许使用相对路径。");
  8. } else if (!name.endsWith(".jar")) {
  9. return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("不允许的文件类型。必须是以 '.jar' 结尾的 Jar 文件类型。");
  10. }
  11. if (!file.isEmpty()) {
  12. try {
  13. byte[] bytes = file.getBytes();
  14. BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name)));
  15. stream.write(bytes);
  16. stream.close();
  17. return ResponseEntity.ok("文件 " + name + " 已上传。");
  18. } catch (Exception e) {
  19. return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(e.getMessage());
  20. }
  21. } else {
  22. return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("上传失败,因为文件为空:" + name);
  23. }
  24. }

问题出现在以下代码行:

  1. 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 :

  1. @RequestMapping(value=&quot;/upload&quot;, method=RequestMethod.POST)
  2. public @ResponseBody ResponseEntity&lt;String&gt; handleFileUpload(@RequestParam(&quot;name&quot;) String name,
  3. @RequestParam(&quot;file&quot;) MultipartFile file) throws Exception{
  4. if (name.contains(&quot;/&quot;)) {
  5. return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(&quot;Folder separators not allowed.&quot;);
  6. } else if (name.contains(&quot;/&quot;)) {
  7. return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(&quot;Relative pathnames not allowed.&quot;);
  8. } else if (!name.endsWith(&quot;.jar&quot;)) {
  9. return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(&quot;File type not allowed. Must be a Jar file type ending in &#39;.jar&#39;.&quot;);
  10. }
  11. if (!file.isEmpty()) {
  12. try {
  13. byte[] bytes = file.getBytes();
  14. BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name)));
  15. stream.write(bytes);
  16. stream.close();
  17. return ResponseEntity.ok(&quot;File &quot; + name + &quot; uploaded.&quot;);
  18. } catch (Exception e) {
  19. return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(e.getMessage());
  20. }
  21. } else {
  22. return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(&quot;You failed to upload &quot; + name + &quot; because the file was empty.&quot;);
  23. }
  24. }
  25. }

i get error in this line :

  1. BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name)));

答案1

得分: 0

唯一我能想到的原因是,你导入了一个与下面代码不同的类:

  1. import java.io.BufferedOutputStream;
英文:

The only reason I can think of is, you have imported a different class than following

  1. import java.io.BufferedOutputStream;

huangapple
  • 本文由 发表于 2020年4月4日 02:08:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/61017929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定