如何使用位置文件(路径)创建MultipartFile

时间:2017-08-25 06:31:55

标签: java

我有位置文件(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 :(得分:0)

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