gpt4 book ai didi

java - 如何在文件类中动态传递字符串值(文件路径)?

转载 作者:行者123 更新时间:2023-12-01 10:38:59 24 4
gpt4 key购买 nike

String filepath=null;
public void dirScan()
{
File root = new File("/tmp/");
FilenameFilter beginswithm = new FilenameFilter()
{
public boolean accept(File directory, String filename) {
return filename.startsWith("201");
}
};

File[] files = root.listFiles(beginswithm);
for (File f: files)
{
filepath=f.toString();
System.out.println(filepath);
}
}

public void prepDownload() throws Exception {

File file = new File(filepath);
FileInputStream input = new FileInputStream(file);
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
setDownload(new DefaultStreamedContent(input, externalContext.getMimeType(file.getName()), file.getName()));
System.out.println("PREP = " + download.getName());

}
public DefaultStreamedContent getDownload() throws Exception {
System.out.println("GET = " + download.getName());

return download;
}

I have a tmp folder in my system. This folder having some dynamic generated files. I want to pass filepath as dynamically preDownload() method. But in my method only last value of filepath is passed to preDownlaod method. while accessing getDownload method it fetches last filepath value. I want to download file from generated rows.Each row having unique file but in my case all rows having same file .

任何帮助或建议将不胜感激。提前致谢。

最佳答案

您的意思是,对于找到的与您的过滤器匹配的每个文件,您要传递给 prepDownload() 吗?如果是这样,那么您可以尝试进行以下更改:

...
for (File f: files){
filepath=f.toString();
prepDownload(filepath);
}
...
public void prepDownload(String filePath) throws Exception {
...
}

这将保证找到每个文件后您将调用 prepDownload。

替代方法:

使用集合来跟踪事物。即:

Set<String> filepaths = new HashSet<String>();
public void dirScan(){
...
File[] files = root.listFiles(beginswithm);
for (File f: files)
{
filepath=f.toString();
filepaths.add(filepath);
}
}
public void prepDownload() throws Exception {
for(String filepath: filepaths){
File file = new File(filepath);
....
}
}

关于java - 如何在文件类中动态传递字符串值(文件路径)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34524762/

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