gpt4 book ai didi

java - FileUploader - 在项目中保存数据

转载 作者:行者123 更新时间:2023-12-01 18:50:08 24 4
gpt4 key购买 nike

我正在上传一个带有 PF 3.5 File Uploader 的文件

我的上传方法如下所示:

public void handleFileUpload(FileUploadEvent event) {  
log.info("Method handleFileUpload invoked");
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);

InputStream inputStream = null;
OutputStream out = null;

try {
File targetFolder = new File("\\resources\\uploads");
if(!targetFolder.exists()) {
targetFolder.mkdirs();
}

inputStream = event.getFile().getInputstream();
File outFile = new File(targetFolder, event.getFile().getFileName());
log.info("copy file stream to " + outFile.getAbsolutePath());
out = new FileOutputStream(outFile);
int read = 0;
byte[] bytes = new byte[size];
log.info("read file stream");
while ((read = inputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}

out.flush();
} catch (IOException e) {
log.error(e);
} finally {
...
}

目前我的文件已上传到\\resources\\uploads" 。那s the path to a folder on the C:`。

但是,我想将上传的内容上传到 Eclipse 项目中的路径。如何更改路径?我非常感谢您的回答!!!

最佳答案

However, I want to upload my uploads to a path in my eclipse project.

出于此答案中提到的原因,绝对不建议这样做:Uploaded image only available after refreshing the page。要点是:IDE 的工作区和服务器的部署文件夹绝对不是用作永久文件存储。上传的文件将无法访问和/或像魔术一样消失。

只需将它们保存在 IDE 工作区和服务器部署文件夹外部的路径中即可。你做得很好。我只会通过系统属性、环境变量或属性文件设置来配置路径,这样您就不需要每次更改上传位置时编辑、重新编译、重建等代码。

如果您的具体问题更多是上传文件的服务,那么只需将上传文件夹添加为服务器配置中的另一个上下文,或者为服务作业创建一个简单的 servlet,或者当您使用 PrimeFaces 时,只需使用 <p:fileDownload><p:graphicImage> ,其中 StreamedContent 指向所需的 FileInputStream

另请参阅:

关于java - FileUploader - 在项目中保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16152823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com