我有位置文件 (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 并保存在某处?有位置
最佳答案
在 Windows 中,您需要双斜杠“C://fakepath//Code.txt”
关于java - 如何使用位置文件(路径)创建MultipartFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45875602/