java - 使用FileInputStream和FileOutputStream上传大文件

标签 java grails fileinputstream fileoutputstream

        HttpExchange exchange;
        OutputStream responseBody = null;
        try{
          File fileVal = new File(file);
          InputStream inVal = new FileInputStream(fileVal);
          exchange.sendResponseHeaders(HTTP_OK, fileVal.length());
          responseBody = exchange.getResponseBody();
          int read;
          byte[] buffer = new byte[4096];
          while ((readVal = inVal.read(buffer)) != -1){
            responseBody.write(buffer, 0, readVal);
          }
        } catch (FileNotFoundException e){
          //uh-oh, the file doesn't exist
        } catch (IOException e){
          //uh-oh, there was a problem reading the file or sending the response
        } finally {
          if (responseBody != null){
            responseBody.close();
          }
        }
我正在尝试以块的形式上传大型视频文件。在执行该操作时,出现以下错误。
groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.io.File(org.springframework.web.multipart.commons.CommonsMultipartFile)
任何人指导我解决这个问题。

最佳答案

File fileVal = new File(file);

这里的文件是org.springframework.web.multipart.commons.CommonsMultipartFile类型,并且您试图通过在构造函数中传递CommonsMultipartFile对象来创建File对象,并且File类没有CommonsMultipartFile类型的构造函数。

Check here for File Class Constructor

您需要从文件对象获取字节并创建一个java.io.File对象。

Convert MultiPartFile into File

关于java - 使用FileInputStream和FileOutputStream上传大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44845351/

相关文章:

java - 无法解析符号 setOnTouchListener

java - 多个 toString 方法?

java - .put() 和 .accumulate() 追溯更改值的 JSON 行为

Grails:组织 i18n 包

java - 如何使用字节数组作为参数构建 FileInputStream 对象

java - 文件读取循环中的 FileInputStream 内存泄漏

java - 为什么 Spring 的 @Transactional 有助于性能

grails - 在Grails JUnit测试案例中记录信息的正确方法是什么?

java - 使用具有多对多关系的 Grails 的 withCriteria 函数的重复问题

java - 无缓冲的 FileInputStream 不支持 .mark(int)