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

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

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=&quot;/upload&quot;, method=RequestMethod.POST)
public @ResponseBody ResponseEntity&lt;String&gt;  handleFileUpload(@RequestParam(&quot;name&quot;) String name,
@RequestParam(&quot;file&quot;) MultipartFile file) throws Exception{
if (name.contains(&quot;/&quot;)) {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(&quot;Folder separators not allowed.&quot;);
} else if (name.contains(&quot;/&quot;)) {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(&quot;Relative pathnames not allowed.&quot;);
} else if (!name.endsWith(&quot;.jar&quot;)) {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(&quot;File type not allowed.  Must be a Jar file type ending in &#39;.jar&#39;.&quot;);
}
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(&quot;File &quot; + name + &quot; uploaded.&quot;);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(e.getMessage());
}
} else {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(&quot;You failed to upload &quot; + name + &quot; because the file was empty.&quot;);
}
}
}

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;

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:

确定