2017-06-30 108 views
0
 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(); 
      } 
     } 

我正在尝试上传大视频文件的块。同时执行此操作时出现以下错误。使用FileInputStream和FileOutputStream进行大文件上传

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

任何人都指导我解决这个问题。

回答

0

错误消息完美地描述了故障。没有类File的构造函数接受org.springframework.web.multipart.commons.CommonsMultipartFile类型的参数。

尝试使用要打开的文件的路径。例如:

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

另外,您可以从CommonsMultipartFile使用getInputStream()方法。

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

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

Check here for File Class Constructor

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

Convert MultiPartFile into File

相关问题