我已经从我的系统上传了一个文件,我已经以字节为单位转换了文件。现在我想将该文件保存在服务器上。我怎样才能做到这一点。我通过互联网搜索但一无所获。有这个问题的解决方案吗? 我正在使用JSP上传文件。
答案 0 :(得分:1)
如果您正在谈论UploadedFile,以下是我在大量互联网搜索后实现这一目标的方法:
/**
* Save uploaded file to server
* @param path Location of the server to save file
* @param uploadedFile Current uploaded file
*/
public static void saveUploadedFile(String path, UploadedFile uploadedFile) {
try {
//First, Generate file to make directories
String savedFileName = path + "/" + uploadedFile.getFileName();
File fileToSave = new File(savedFileName);
fileToSave.getParentFile().mkdirs();
fileToSave.delete();
//Generate path file to copy file
Path folder = Paths.get(savedFileName);
Path fileToSavePath = Files.createFile(folder);
//Copy file to server
InputStream input = uploadedFile.getInputstream();
Files.copy(input, fileToSavePath, StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
logger.error(e.getMessage());
} finally {
}
}