2017-08-25 1576 views
1

我有位置文件(C:\ fakepath \ Code.txt)。我想用这个位置创建MultipartFile。我的代码:如何用位置文件(路径)创建MultipartFile

public void fileUpload(String locationFile) { 

    Path path = Paths.get(locationFile); 
    String name = "Code.txt"; 
    String originalFileName = "Code.txt"; 
    String contentType = "text/plain"; 
    byte[] content = null; 
    try { 
     content = Files.readAllBytes(path); 
    } catch (final IOException e) { 
    } 
    MultipartFile file = new MockMultipartFile(name, originalFileName, contentType, content); 

    try { 
     // Get the file and save it somewhere 
     byte[] bytes = file.getBytes(); 
     Path paths = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename()); 
     Files.write(paths, bytes); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

此外,在此代码中,我需要输入我的文件名,这对我的意见不正确。如何创建MultipartFile并保存在某个地方?与位置

+1

听起来像一个坏主意 –

回答

0

在Windows,你需要双斜杠“C://fakepath//Code.txt”