我有一个表,其中存储了用户上传的附件。附件可以是text/doc/jpg等类型,也可以有多个用户上传文件。因此,在某些情况下,文件名可能相同。因此,当发生这种情况时,将下载DB表中的第一个文件。因此,除了文件名之外,还可以添加一个参数来确保下载正确的文件。另一个参数可以是attachment_id,它在每种情况下都是唯一的。
这是用于根据文件名称下载文件的操作方法
public String downloadAttachFile() throws FileNotFoundException {
attachFileName = ServletActionContext.getRequest().getParameter("myFileFileName");
fileInputStream = new FileInputStream(new File(AttachFileName));
return SUCCESS;}提前谢谢。
发布于 2014-09-16 23:21:24
不确定您的文件在哪里(文件系统?)它们如何以相同的名称存储(不同的文件夹?),或者何时/如何一次下载多个文件,但您只需复制操作系统机制,以将多个文件以相同的名称重命名,并在末尾加上(n),如foo.txt和foo(1).txt。
请随意将我在answer中编写的函数修改为我自己的ZIP question
// Set-up a list of filenames to prevent duplicate entries
HashSet<String> entries = new HashSet<String>();
/* ====== ====== ====== ====== ====== ====== ====== ====== ====== ====== ======
then call getUniqueFileName() for each file passing "entries" as parameter
to get an unique file name.
====== ====== ====== ====== ====== ====== ====== ====== ====== ====== ====== */private String getUniqueFileName(HashSet<String> entries, String completeFileName){
if (entries.contains(completeFileName)){
int extPos = completeFileName.lastIndexOf('.');
String extension = extPos>0 ? completeFileName.substring(extPos) : "";
String partialFileName = extension.length()==0 ? completeFileName : completeFileName.substring(0,extPos);
int x=1;
while (entries.contains(completeFileName = partialFileName + "(" + x + ")" + extension))
x++;
}
entries.add(completeFileName);
return completeFileName;
}https://stackoverflow.com/questions/25872192
复制相似问题