简体   繁体   中英

Large file upload using FileInputStream and 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();
          }
        }

I am tring to upload large video file as chunks .while doing the operation I am getting the following error.

groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.io.File(org.springframework.web.multipart.commons.CommonsMultipartFile)

any anyone guide me to solve this.

The error message descripes the failure perfectly. There is no constructor for the class File that accept a parameter of the type org.springframework.web.multipart.commons.CommonsMultipartFile .

Try using the path to the file you want to open. For example:

String path = "/path/to/your/file.txt";
File fileVal = new File(path);

Alternatively you can use the getInputStream() method from CommonsMultipartFile .

InputStream inVal = file.getInputStream();
File fileVal = new File(file);

Here file is org.springframework.web.multipart.commons.CommonsMultipartFile type and you are trying to create File object by passing CommonsMultipartFile object in constructor and File class does not have constructor of CommonsMultipartFile type.

Check here for File Class Constructor

You Need to get Bytes from file object and create a java.io.File object.

Convert MultiPartFile into File

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM