[英]How to save Uploaded file to a folder with its name
嗨,我正在尝试将上传的文件保存到项目中的文件夹中,但我不确定如何指定路径。 我还希望将上载文件的相同名称作为存储在文件夹中的文件的名称。
public void handleFileUpload(FileUploadEvent event) throws IOException {
LOG.info("Entered into save action event");
FacesMessage message = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);
String filename = event.getFile().getFileName();
UploadedFile file = event.getFile();
InputStream input = file.getInputstream();
OutputStream output = new FileOutputStream(new File("src/main/resources/uploads/schema.xsd", filename));
try {
IOUtils.copy(input, output);
} catch (IOException e){
LOG.error("Error in copying file.", e);
}
finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(output);
}
}
最好的选择是从服务器的根目录创建一个绝对路径的文件夹。 这表示C:如果使用Windows或/如果使用Linux。 例如,在Linux Server中,您可以执行以下操作:
OutputStream output = new FileOutputStream(new File("/var/my_app/uploads/" + filename + "/" + filename + ".xsd"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.