首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据附件id下载附件

根据附件id下载附件
EN

Stack Overflow用户
提问于 2014-09-16 23:00:27
回答 1查看 126关注 0票数 0

我有一个表,其中存储了用户上传的附件。附件可以是text/doc/jpg等类型,也可以有多个用户上传文件。因此,在某些情况下,文件名可能相同。因此,当发生这种情况时,将下载DB表中的第一个文件。因此,除了文件名之外,还可以添加一个参数来确保下载正确的文件。另一个参数可以是attachment_id,它在每种情况下都是唯一的。

这是用于根据文件名称下载文件的操作方法

代码语言:javascript
复制
public String downloadAttachFile() throws FileNotFoundException {
   attachFileName = ServletActionContext.getRequest().getParameter("myFileFileName");
   fileInputStream = new FileInputStream(new File(AttachFileName));
   return SUCCESS;

}提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2014-09-16 23:21:24

不确定您的文件在哪里(文件系统?)它们如何以相同的名称存储(不同的文件夹?),或者何时/如何一次下载多个文件,但您只需复制操作系统机制,以将多个文件以相同的名称重命名,并在末尾加上(n),如foo.txtfoo(1).txt

请随意将我在answer中编写的函数修改为我自己的ZIP question

代码语言:javascript
复制
// 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.
 ====== ====== ====== ====== ====== ====== ====== ====== ====== ====== ====== */
代码语言:javascript
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25872192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档