2017-08-25 1 views
1

私はロケーションファイル(C:\ fakepath \ Code.txt)を持っています。私はこの場所で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"

関連する問題